A memory cache object is a key-value store that saves the values in an
environment. Objects can be stored and retrieved using the
set() methods. Objects are automatically pruned from the cache according to
cache_mem( max_size = 512 * 1024^2, max_age = Inf, max_n = Inf, evict = c("lru", "fifo"), missing = key_missing(), logfile = NULL )
Maximum size of the cache, in bytes. If the cache exceeds
this size, cached objects will be removed according to the value of the
Inf for no size limit. The default is 512 megabytes.
Maximum age of files in cache before they are evicted, in
Inf for no age limit.
Maximum number of objects in the cache. If the number of objects
exceeds this value, then cached objects will be removed according to the
Inf for no limit of number of items.
The eviction policy to use to decide which objects are removed
when a cache pruning occurs. Currently,
"fifo" are supported.
A value to return when
get(key) is called but the key is not
present in the cache. The default is a
key_missing() object. It is
actually an expression that is evaluated each time there is a cache miss.
See section Missing keys for more information.
An optional filename or connection object to where logging
information will be written. To log to the console, use
A memory caching object, with class
cache_mem, R objects are stored directly in the cache; they are not
not serialized before being stored in the cache. This contrasts with other
cache types, like
cache_disk(), where objects are serialized, and the
serialized object is cached. This can result in some differences of behavior.
For example, as long as an object is stored in a cache_mem, it will not be
missing parameter controls what happens when
get() is called with a
key that is not in the cache (a cache miss). The default behavior is to
key_missing() object. This is a sentinel value that indicates
that the key was not present in the cache. You can test if the returned
value represents a missing key by using the
You can also have
get() return a different sentinel value, like
If you want to throw an error on a cache miss, you can do so by providing
an expression for
missing, as in
missing = stop("Missing key").
When the cache is created, you can supply a value for
missing, which sets
the default value to be returned for missing values. It can also be
get() is called, by supplying a
missing argument. For
example, if you use
cache$get("mykey", missing = NULL), it will return
NULL if the key is not in the cache.
missing parameter is actually an expression which is evaluated each
time there is a cache miss. A quosure (from the rlang package) can be used.
If you use this, the code that calls
get() should be wrapped with
tryCatch() to gracefully handle missing keys.
@section Cache pruning:
Cache pruning occurs when
set() is called, or it can be invoked manually
When a pruning occurs, if there are any objects that are older than
max_age, they will be removed.
max_n parameters are applied to the cache as a whole,
in contrast to
max_age, which is applied to each object individually.
If the number of objects in the cache exceeds
max_n, then objects will be
removed from the cache according to the eviction policy, which is set with
evict parameter. Objects will be removed so that the number of items
If the size of the objects in the cache exceeds
max_size, then objects
will be removed from the cache. Objects will be removed from the cache so
that the total size remains under
Another time that objects can be removed from the cache is when
called. If the target object is older than
max_age, it will be removed
and the cache will report it as a missing value.
max_size are used, then objects will be removed
from the cache according to an eviction policy. The available eviction
Least Recently Used. The least recently used objects will be removed.
First-in-first-out. The oldest objects will be removed.
A disk cache object has the following methods:
Returns the value associated with
key. If the key is not in the
cache, then it evaluates the expression specified by
returns the value. If
missing is specified here, then it will
override the default that was set when the
cache_mem object was
created. See section Missing Keys for more information.
value pair in the cache.
TRUE if the cache contains the key, otherwise
key from the cache, if it exists in the cache. If the key is
not in the cache, this does nothing.
Returns the number of items currently in the cache.
Returns a character vector of all keys currently in the cache.
Clears all objects from the cache.
Clears all objects in the cache, and removes the cache directory from disk.
Prunes the cache, using the parameters specified by