Public methods
Method new()
Usage
Cache$new(
dir = dirname(file),
max_files = Inf,
max_size = Inf,
max_age = Inf,
compression = TRUE,
hashfun = digest::digest,
create_dir = TRUE
)
Arguments
create_dir
logical
scalar. If TRUE
dir
is created if it
does not exist.
Examples
td <- file.path(tempdir(), "cache-test")
# When using a real hash function as hashfun, identical objects will only
# be added to the cache once
cache_hash <- Cache$new(td, hashfun = digest::digest)
cache_hash$push(iris)
cache_hash$push(iris)
cache_hash$files
cache_hash$purge()
# To override this behaviour use a generator for unique ids, such as uuid
if (requireNamespace("uuid")){
cache_uid <- Cache$new(td, hashfun = function(x) uuid::UUIDgenerate())
cache_uid$push(iris)
cache_uid$push(iris)
cache_uid$files
cache_uid$purge()
}
unlink(td, recursive = TRUE)
Method push()
push a new object to the cache
Usage
Cache$push(x, key = self$hashfun(x))
Arguments
x
any R object
key
a character
scalar. Key under which to store the cached
object. Must be a valid filename. Defaults to being generated by
$hashfun()
but may also be supplied manually.
Returns
a character
scalar: the key of the newly added object
Method read()
read a cached file
Usage
Cache$read(key)
Arguments
key
character
scalar. key of the cached file to read.
Method remove()
remove a single file from the cache
Usage
Cache$remove(key)
Arguments
key
character
scalar. key of the cached file to remove
Method pop()
Read and remove a single file from the cache
Usage
Cache$pop(key)
Arguments
key
character
scalar. key of the cached file to read/remove
Method prune()
Prune the cache
Usage
Cache$prune(
max_files = self$max_files,
max_size = self$max_size,
max_age = self$max_age,
now = Sys.time()
)
Arguments
max_files, max_size, max_age
see section Active Bindings.
now
a POSIXct
datetime scalar. The current time (for max_age)
Method purge()
purge the cache (remove all cached files)
Usage
Cache$purge()
Method destroy()
purge the cache (remove all cached files)
Usage
Cache$destroy()
Method print()
Usage
Cache$print()
Method set_max_files()
Usage
Cache$set_max_files(x)
Method set_max_age()
Usage
Cache$set_max_age(x)
Method set_max_size()
Usage
Cache$set_max_size(x)
Method set_compression()
Usage
Cache$set_compression(x)
Method set_hashfun()
Usage
Cache$set_hashfun(x)