Skip to content

Commit

Permalink
Merge pull request #43 from databio/dev
Browse files Browse the repository at this point in the history
Release 0.4.1
  • Loading branch information
nsheff committed Feb 26, 2019
2 parents e4f3aa7 + a4b93e0 commit a38d71b
Show file tree
Hide file tree
Showing 20 changed files with 152 additions and 51 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: simpleCache
Version: 0.4.1
Date: 2018-03-01
Date: 2019-02-26
Title: Simply Caching R Objects
Description: Provides intuitive functions for caching R objects, encouraging
reproducible, restartable, and distributed R analysis. The user selects a
Expand All @@ -22,4 +22,4 @@ License: BSD_2_clause + file LICENSE
Encoding: UTF-8
URL: https://www.github.com/databio/simpleCache
BugReports: https://www.github.com/databio/simpleCache
RoxygenNote: 6.0.1.9000
RoxygenNote: 6.1.0
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

export(addCacheSearchEnvironment)
export(deleteCaches)
export(getCacheDir)
export(listCaches)
export(loadCaches)
export(resetCacheSearchEnvironment)
Expand Down
18 changes: 12 additions & 6 deletions NEWS → NEWS.md
Original file line number Diff line number Diff line change
@@ -1,37 +1,43 @@
# Change log
All notable changes to this project will be documented in this file.

## [0.4.0] -- 2017-12-20
## simpleCache [0.4.1] -- 2019-02-26

- fixes unit tests on windows
- fixes lifespan bug that used creation time instead of modification time
- allow arg-less directory setting to default to current working dir

## simpleCache [0.4.0] -- 2017-12-20

- adds a lifespan arg to simpleCache() to create auto-expiring caches
- remove unnecessary parse argument to simpleCache()
- viewCacheDirs() renamed to simpleCacheOptions()

## [0.3.1] -- 2017-08-21
## simpleCache [0.3.1] -- 2017-08-21

- fixed a bug in unit tests that left behind a test cache in user home dir.
- changes cache building to happen in parent.frame()
- repaired vignette so R code is displayed properly
- added deleteCaches() function and docs
- reduced size of unit test cache for speed increase

## [0.3.0] -- 2017-08-21
## simpleCache [0.3.0] -- 2017-08-21

- switched default cache dir to tempdir()
- changed availCaches() to listCaches()
- changes cache building to happen in globalenv(), so that any loaded
packages are available for cache building


## [0.2.1] -- 2017-07-30
## simpleCache [0.2.1] -- 2017-07-30

- added examples

## [0.2.0] -- 2017-07-30
## simpleCache [0.2.0] -- 2017-07-30

- support for batchjobs parallel processing
- docs, prep for submission to CRAN

## [0.0.1]
## simpleCache [0.0.1]

- long-term stable version
29 changes: 19 additions & 10 deletions R/cacheDirectories.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,16 @@
#' @export
#' @example
#' R/examples/example.R
setCacheDir = function(cacheDir) {
options(RCACHE.DIR=cacheDir)
}
setCacheDir = function(cacheDir=NULL) { .setDir("RCACHE.DIR", cacheDir) }

#' Fetcher of the currently set cache directory.
#'
#' \code{getCacheDir} retrieves the value of the option that stores the currently
#' set cache directory path.
#'
#' @return If the option is set, the path to the currently set cache directory; otherwise, \code{NULL}.
#' @export
getCacheDir = function() { getOption("RCACHE.DIR") }

#' Set shared cache directory
#'
Expand All @@ -24,25 +31,21 @@ setCacheDir = function(cacheDir) {
#'
#' @param sharedCacheDir Directory where shared caches should be stored
#' @export
setSharedCacheDir = function(sharedCacheDir) {
options(RESOURCES.RCACHE=sharedCacheDir)
}
setSharedCacheDir = function(sharedCacheDir=NULL) { .setDir("RESOURCES.RCACHE", sharedCacheDir) }

#' Sets local cache build directory with scripts for building files.
#'
#' @param cacheBuildDir Directory where build scripts are stored.
#' @export
setCacheBuildDir = function(cacheBuildDir) {
options(RBUILD.DIR=cacheBuildDir)
}
setCacheBuildDir = function(cacheBuildDir=NULL) { .setDir("RBUILD.DIR", cacheBuildDir) }

#' View simpleCache options
#'
#' Views simpleCache global variables
#' @export
simpleCacheOptions = function() {
message("RESOURCES.RCACHE:\t", getOption("RESOURCES.RCACHE"))
message("RCACHE.DIR:\t", getOption("RCACHE.DIR"))
message("RCACHE.DIR:\t", getCacheDir())
message("RBUILD.DIR:\t", getOption("RBUILD.DIR"))
message("SIMPLECACHE.ENV:\t", getOption("SIMPLECACHE.ENV"))
}
Expand All @@ -66,3 +69,9 @@ resetCacheSearchEnvironment = function() {
options(SIMPLECACHE.ENV=NULL)
}


.setDir = function(optname, dirpath=NULL) {
diropts = list(ifelse(is.null(dirpath), getwd(), dirpath))
names(diropts) = optname
do.call(options, diropts)
}
2 changes: 1 addition & 1 deletion R/deleteCache.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#' @export
#' @example
#' R/examples/example.R
deleteCaches = function(cacheNames, cacheDir=getOption("RCACHE.DIR"),
deleteCaches = function(cacheNames, cacheDir=getCacheDir(),
force=FALSE) {

if (force) {
Expand Down
3 changes: 2 additions & 1 deletion R/listCaches.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#' @example
#' R/examples/example.R
listCaches = function(cacheSubDir="") {
list.files(paste0(getOption("RCACHE.DIR"), cacheSubDir))
cacheDirFiles = list.files(paste0(getCacheDir(), cacheSubDir))
cacheDirFiles[which(sapply(cacheDirFiles, function(f) endsWith(f, ".RData")))]
}

6 changes: 4 additions & 2 deletions R/simpleCache.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
#' R/examples/example.R
simpleCache = function(cacheName, instruction=NULL, buildEnvir=NULL,
reload=FALSE, recreate=FALSE, noload=FALSE,
cacheDir=getOption("RCACHE.DIR"), cacheSubDir=NULL, timer=FALSE,
cacheDir=getCacheDir(), cacheSubDir=NULL, timer=FALSE,
buildDir=getOption("RBUILD.DIR"), assignToVariable=NULL,
loadEnvir=parent.frame(), searchEnvir=getOption("SIMPLECACHE.ENV"),
nofail=FALSE, batchRegistry=NULL, batchResources=NULL, pepSettings=NULL,
Expand Down Expand Up @@ -134,7 +134,9 @@ simpleCache = function(cacheName, instruction=NULL, buildEnvir=NULL,
cacheWhere = NULL

for ( curEnv in searchEnvir ) {
if(exists(cacheName, where=get(curEnv))) {
if(! ( exists(curEnv) && is.environment(get(curEnv))) ) {
warning(curEnv, " is not an environment.")
} else if( exists(cacheName, where=get(curEnv))) {
cacheExists = TRUE
cacheWhere = curEnv
break
Expand Down
2 changes: 1 addition & 1 deletion R/storeCache.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#' @export
#' @example
#' R/examples/example.R
storeCache = function(cacheName, cacheDir = getOption("RCACHE.DIR"),
storeCache = function(cacheName, cacheDir = getCacheDir(),
cacheSubDir = NULL, recreate=FALSE) {

if(!is.null(cacheSubDir)) {
Expand Down
2 changes: 1 addition & 1 deletion R/utility.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
if (!utils::file_test("-f", pathCacheFile)) { return(FALSE) }
if (is.null(lifespan)) { lifespan = getOption("MAX.CACHE.AGE") }
if (is.null(lifespan)) { return(FALSE) }
cacheTime = file.info(pathCacheFile)$ctime
cacheTime = file.info(pathCacheFile)$mtime
cacheAge = difftime(Sys.time(), cacheTime, units="days")
as.numeric(cacheAge) > as.numeric(lifespan)
}
Expand Down
2 changes: 1 addition & 1 deletion man/deleteCaches.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions man/dot-tooOld.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions man/getCacheDir.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/setCacheBuildDir.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/setCacheDir.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/setSharedCacheDir.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions man/simpleCache-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions man/simpleCache.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/storeCache.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 35 additions & 3 deletions tests/testthat/test_all.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@ library(simpleCache)

context("error checking")


# Map option name to its setter.
kSetters = list(RCACHE.DIR=setCacheDir, RESOURCES.RCACHE=setSharedCacheDir, RBUILD.DIR=setCacheBuildDir)


# Test a cache dir setting in managed context fashion, resetting before and after test.
test_dir_default = function(cacheDirOptname) {
resetCacheSearchEnvironment()
test_that(sprintf("%s setter uses current folder for argument-less call", cacheDirOptname), {
do.call(kSetters[[cacheDirOptname]], args=list())
expect_equal(getwd(), getOption(cacheDirOptname))
})
resetCacheSearchEnvironment()
}


test_that("notifications and messages as expected", {

# message if cache exists
Expand Down Expand Up @@ -106,12 +122,16 @@ test_that("option setting works", {
setCacheBuildDir(tempdir())
addCacheSearchEnvironment("cacheEnv")

# Windows uses double slashes, which get consumed weirdly by grep;
# This command will replace double slashes with quadruple slashes,
# which behave correctly in grep.
grep_tempdir = gsub("\\\\", "\\\\\\\\", tempdir())
# capture output and check
options_out <- capture_messages(simpleCacheOptions())

expect_true(grepl(tempdir(), options_out[1]))
expect_true(grepl(tempdir(), options_out[2]))
expect_true(grepl(tempdir(), options_out[3]))
expect_true(grepl(grep_tempdir, options_out[1]))
expect_true(grepl(grep_tempdir, options_out[2]))
expect_true(grepl(grep_tempdir, options_out[3]))
expect_true(grepl("cacheEnv", options_out[4]))

# reset the cache search option
Expand All @@ -123,6 +143,18 @@ test_that("option setting works", {

})

test_that("Cache dir fetch works", {
options(RCACHE.DIR = NULL)
expect_true(is.null(getCacheDir()))
setCacheDir(tempdir())
expect_false(is.null(getCacheDir()))
expect_equal(getCacheDir(), tempdir())
})

# Test each cache directory option setter.
for (optname in names(kSetters)) { test_dir_default(optname) }


test_that("objects pass through in buildEnvir", {

setCacheDir(tempdir())
Expand Down

0 comments on commit a38d71b

Please sign in to comment.