archivist (version 1.2)

cache: Enable Caching of a Function Results with the use of Archivist

Description

cache function stores all results of function calls in local Repository. All results are stored together with md5 hashes of the function calls. If function has been called with same arguments as in the past, then results can be loaded from repository.

One may specify expiration date for live objects. It may be useful for objects that can be changed externally (like queries to database).

Usage

cache(cacheRepo, FUN, ..., notOlderThan = NULL)

Arguments

cacheRepo
An object repository used for storing cached objects.
FUN
A function to be called.
...
Arguments for function FUN.
notOlderThan
Restore an artifact from database only if it was created after notOlderThan.

Value

  • Result of function call with added additional argument - md5 hash of function call.

Details

cache function stores all results of function calls in local Repository specified by the cacheRepo argument. The md5 hash of FUN and it's arguments is added as an tag to the repository. If objects are big and calculations are easy, then disk input-output operations may take more time than calculations itself.

See Also

For more detailed information check the archivist package https://github.com/pbiecek/archivist#-the-list-of-use-cases-{Use Cases}.

Other archivist: Repository; Tags; addTagsRepo; archivist-package; copyGithubRepo, copyLocalRepo; createEmptyRepo; deleteRepo; getTagsGithub, getTagsLocal; loadFromGithubRepo, loadFromLocalRepo; md5hash; multiSearchInGithubRepo, multiSearchInLocalRepo, searchInGithubRepo, searchInLocalRepo; rmFromRepo; saveToRepo; shinySearchInLocalRepo; showGithubRepo, showLocalRepo; summaryGithubRepo, summaryLocalRepo; zipGithubRepo, zipLocalRepo

Examples

Run this code
# objects preparation
cacheRepo <- tempdir()
createEmptyRepo( cacheRepo )
fun <- function(n) {replicate(n, summary(lm(Sepal.Length~Species, iris))$r.squared)}
system.time( res <- cache(cacheRepo, fun, 1000) )
system.time( res <- cache(cacheRepo, fun, 1000) )

testFun <- function(x) {cat(x);x}
# will be executed
tmp <- cache(cacheRepo, testFun, "Say hallo!")
# will loaded from repository
tmp <- cache(cacheRepo, testFun, "Say hallo!")
# will be executed, fails with expiration date
tmp <- cache(cacheRepo, testFun, "Say hallo!", notOlderThan = now())
# will be executed, passes with expiration date [within hour]
tmp <- cache(cacheRepo, testFun, "Say hallo!", notOlderThan = now() - hours(1))

deleteRepo( cacheRepo )
rm( cacheRepo )

Run the code above in your browser using DataCamp Workspace