| Cache {rotor} | R Documentation |
An R6 class for managing a persistent file-based cache
Description
Cache provides an R6 API for managing an on-disk key-value
store for R objects. The objects are serialized to a single folder as
.rds files and the key of the object equals the name of the file.
Cache supports automatic removal of old files if the cache folder exceeds a
predetermined number of files, total size, or if the individual files exceed
a certain age.
Details
This class is part of the R6 API of rotor which is
intended for developers that want to extend this package. For normal usage,
the simpler functional API is recommended (see rotate()).
Super class
rotor::DirectoryQueue -> Cache
Public fields
dira
characterscalar. path of the directory in which to store the cache filesnintegerscalar: number of files in the cachemax_filessee the
compressargument ofbase::saveRDS(). Note: this differs from the$compressargument ofrotate().max_filesintegerscalar: maximum number of files to keep in the cache
Active bindings
dira
characterscalar. path of the directory in which to store the cache filesnintegerscalar: number of files in the cachemax_filessee the
compressargument ofbase::saveRDS(). Note: this differs from the$compressargument ofrotate().max_filesintegerscalar: maximum number of files to keep in the cachemax_sizescalar
integer,characterorInf. Delete cached files (starting with the oldest) until the total size of the cache is belowmax_size.Integersare interpreted as bytes. You can passcharactervectors that contain a file size suffix like1k(kilobytes),3M(megabytes),4G(gigabytes),5T(terabytes). Instead of these short forms you can also be explicit and use the IEC suffixesKiB,MiB,GiB,TiB. In Both cases1kilobyte is1024bytes, 1megabyteis1024kilobytes, etc... .max_age-
a
Datescalar: Remove all backups before this datea
characterscalar representing a Date in ISO format (e.g."2019-12-31")a
characterscalar representing an Interval in the form"<number> <interval>"(seerotate())
hashfunNULLor afunctionto generate a unique hash from the object to be cached (see example). The hash must be a text string that is a valid filename on the target system. If$hashfunisNULL, a storage key must be supplied manually incache$push(). If a new object is added with the same key as an existing object, the existing object will be overwritten without warning. All cached files
Methods
Public methods
Inherited 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_dirlogicalscalar. IfTRUEdiris 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
xany R object
keya
characterscalar. 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
keycharacterscalar. key of the cached file to read.
Method remove()
remove a single file from the cache
Usage
Cache$remove(key)
Arguments
keycharacterscalar. key of the cached file to remove
Method pop()
Read and remove a single file from the cache
Usage
Cache$pop(key)
Arguments
keycharacterscalar. key of the cached file to read/remove
Method prune()
Prune the cache
Delete cached objects that match certain criteria. max_files and
max_size deletes the oldest cached objects first; however, this is
dependent on accuracy of the file modification timestamps on your system.
For example, ext3 only supports second-accuracy, and some windows
version only support timestamps at a resolution of two seconds.
If two files have the same timestamp, they are deleted in the lexical
sort order of their key. This means that by using a function that
generates lexically sortable keys as hashfun (such as
ulid::generate()) you can enforce the correct deletion order. There
is no such workaround if you use a real hash function.
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_agesee section Active Bindings.
nowa
POSIXctdatetime 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)
See Also
Other R6 Classes:
BackupQueueDateTime,
BackupQueueDate,
BackupQueueIndex,
BackupQueue,
DirectoryQueue
Examples
## ------------------------------------------------
## Method `Cache$new`
## ------------------------------------------------
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)