README.md

memoisetools

memoisetools is a collection of additional caches and helper functions to work alongside the memoise package.

This package introduces new caches, new memoise() alternatives and functions for interrogating caches and expiring old objects from a cache.

Installation

devtools::install_github('coolbutuseless/memoisetools')

get_memoise_info()

get_memoise_info() returns information about the caches used by a memoised function.

Note: because memoised functions could have multiple caches (e.g. memoise_with_mixed_backend), this function returns a list of info for each cache.

memoised_rnorm <- memoise::memoise(rnorm)

x <- memoised_rnorm(1000)
y <- memoised_rnorm(12)
z <- memoised_rnorm(1)

memoisetools::get_memoise_info(memoised_rnorm)
#> cache: memory, env: <environment>, algo: sha512, bytes: 9728, n: 3, has_timestamp: FALSE, compress: FALSE

cache_filesystem2()

This is a replacement for memoise::cache_filesystem() with the following changes:

cache_memory2()

This is a replacement for memoise::cache_memory() with the following changes:

Expiring objects from the cache

With cache_filesystem2() and cache_memory2(), objects older than a specified age can be retired from the cache. I.e. if they have not been read or written more recently than the specified time, they will be deleted.

memoised_rnorm <- memoise::memoise(rnorm, cache = memoisetools::cache_memory2()) 

memoised_rnorm(1) # stored in cache. 
#> [1] -0.4321563
memoised_rnorm(2) # stored in cache
#> [1] 0.4748322 0.4878174
Sys.sleep(1)      # wait a little bit
memoised_rnorm(1) # recent access to this cached data means it won't be expired
#> [1] -0.4321563

# The following expiry will only delete the cached result for `memoised_rnorm(2)`
# as it has not been read/written in over 1 second
memoisetools::expire_cache(memoised_rnorm, age_in_seconds = 1, verbose = TRUE)
#> cache_memory2: Expired 1 objects

memoised_rnorm(1) # this result is still in the cache
#> [1] -0.4321563
memoised_rnorm(2) # this is a fresh result as the cached version was removed
#> [1] -0.276410  0.410349

memoise_with_result_size_limit()

This is a replacement for memoise::memoise() which places a limit on how large an object can be before it is no longer stored in the cache (but simply recalculated each time).

By default, memoise::memoise() will store all results regardless of size. This works for the majority of cases where you have enough memory and results are never too large.

For the problem I was working on, the function produced many small results and a few very very large results (greater than 2GB is size). If all the big results were cached I’d run out of memory!

In the following example, results over 1000 bytes will not be cached.

memoised_rnorm <- memoisetools::memoise_with_result_size_limit(rnorm, result_size_limit = 1000)

memoised_rnorm(1) # small enough to cache
#> [1] -1.531591
memoised_rnorm(1) # getting cached result
#> [1] -1.531591

head(memoised_rnorm(1000)) # too big to be cached
#> [1]  1.1455911  1.4399165  1.1183446 -0.9559477  0.4344566  0.2471136
head(memoised_rnorm(1000)) # so each run produces fresh result
#> [1]  0.63660944  1.87380649 -0.72163613  0.57059968  0.73701482  0.01714318

memoise_with_mixed_backend()

This is an adjusted version of memoise::memoise() which requires two caches to be set, along with a size limit. Objects smaller than the size limit go to the first cache, and objects larger than the size limit go to the second cache.

This allows you to cache small results in memory, and send large results to the filesystem, s3 or google cloud storage.

memoised_rnorm <- memoisetools::memoise_with_mixed_backend(
  rnorm,
  cache1 = memoisetools::cache_memory2(),
  cache2 = memoisetools::cache_filesystem2(),
  result_size_limit = 1000
)

a <- memoised_rnorm(1) # These 3 results cached to memory
b <- memoised_rnorm(2)
c <- memoised_rnorm(3)

x <- memoised_rnorm(1000)  # These 2 results cached to filesystem
y <- memoised_rnorm(2000)

memoisetools::get_memoise_info(memoised_rnorm)
#> cache: memory, env: <environment>, algo: xxhash64, bytes: 1648, n: 3, has_timestamp: TRUE, compress: FALSE
#> cache: filesystem, path: /private/var/folders/5p/78cv9fvn4xn_rbgxpx51q5n80000gn/T/RtmpM6MZnb, algo: xxhash64, bytes: 23284, n: 2, has_timestamp: TRUE, compress: TRUE


coolbutuseless/memoisetools documentation built on May 31, 2019, 12:45 a.m.