A class to handle caching of objects, files, or directories. It generates a signature from the provided object and creates cached versions based on that signature.
Public fields
cache_dirDirectory where cached files are stored
sourcepath to the original file/directory/prefix
Methods
Method new()
Initialize a new Cache object
Arguments
sig_objectObject used to generate the cache signature
prefixPrefix for the cache filename
cache_dirDirectory where cached files are stored
save_sigWhether to save the signature to a file
kindType of cache: "object", "file", "dir" or "prefix"
object: cache an R object
file: cache a file
dir: cache a directory
prefix: cache files/directories with the same prefix
pathPath to the file or directory to cache This is required when
kindis "file", "dir", or "prefix"
Details
The sig_object is used to generate a unique signature for the cache.
The signature is based on an expanded utils::str() representation of the object,
which helps in determining if the cached version is still valid.
The prefix is used to create a unique identifier for the cached files.
The cache_dir is the directory where the cached files will be stored.
If save_sig is TRUE, the signature will be saved to a file in the cache directory.
The kind parameter determines how the cache will be handled:
"object": The cache will store an R object.
"file": The cache will store a file.
"dir": The cache will store a directory.
"prefix": The cache will store files/directories with the same prefix. The
pathparameter is required whenkindis "file", "dir", or "prefix". It specifies the path to the file or directory to cache. Ifcache_diris NULL or an empty string, caching will not be performed. Ifcache_diris FALSE, caching will not be performed.
Examples
\dontrun{
# Create a Cache object for an R object
cache <- Cache$new(sig_object = mtcars, prefix = "mtcars_cache",
cache_dir = tempdir(), save_sig = TRUE, kind = "object")
# Save the object to cache
cache$save(mtcars)
# Restore the cached object
cached_mtcars <- cache$restore()
# Check if the object is cached
cache$is_cached() # Should return TRUE
# Clear the cache
cache$clear()
# Clear all cached objects in the cache directory
cache$clear_all()
}
Method clear_all()
Clear all cached objects in the cache directory Be careful with this operation as it will remove all cached files in the cache directory
Method restore()
Retrieve the cached object/file/directory When NULL is returned, it means the cache does not exist
Method save()
Save an object/file/directory/prefix to cache
Examples
## ------------------------------------------------
## Method `Cache$new`
## ------------------------------------------------
if (FALSE) { # \dontrun{
# Create a Cache object for an R object
cache <- Cache$new(sig_object = mtcars, prefix = "mtcars_cache",
cache_dir = tempdir(), save_sig = TRUE, kind = "object")
# Save the object to cache
cache$save(mtcars)
# Restore the cached object
cached_mtcars <- cache$restore()
# Check if the object is cached
cache$is_cached() # Should return TRUE
# Clear the cache
cache$clear()
# Clear all cached objects in the cache directory
cache$clear_all()
} # }