Skip to content

Commit

Permalink
Try to improve resliancy to race conditions resulting in duplicate ca…
Browse files Browse the repository at this point in the history
…che entries (#76)
  • Loading branch information
grimbough committed Mar 23, 2023
1 parent 82b13fe commit 94a1f92
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
21 changes: 18 additions & 3 deletions R/caching.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,25 @@
#' BiocFileCache::BiocFileCache()
#' @param hash unique hash representing a query.
.addToCache <- function(bfc, result, hash) {
tf <- tempfile()
saveRDS(result, file = tf)
bfcadd(bfc, rname = hash, fpath = tf, action = "copy")

if(!dir.exists(.biomartCacheLocation()))
dir.create(.biomartCacheLocation())

## write our file to the biomart cache location directly
tf <- tempfile(tmpdir = biomaRt:::.biomartCacheLocation())
saveRDS(result, file = tf)

## check once more that there isn't an entry with this hash
## if its free add our new file
## if there's a clash don't add anything and tidy up
if(!.checkInCache(bfc, hash = hash)) {
bfcadd(bfc, rname = hash, fpath = tf, action = "asis")
res <- TRUE
} else {
file.remove(tf)
res <- FALSE
}
return(invisible(res))
}

#' @param bfc Object of class BiocFileCache, created by a call to
Expand Down
12 changes: 11 additions & 1 deletion tests/testthat/test_z_cache.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
library(biomaRt)

cache <- file.path(tempdir(), "biomart_cache_test")
Sys.setenv(BIOMART_CACHE = cache)

Expand Down Expand Up @@ -43,7 +45,8 @@ test_that("We find cache for previous query", {
expect_identical(result, .readFromCache(bfc, hash = hash))
})

## add an invalid file
## add an invalid file. We are only expecting .rds files, and this is a
## text file
tf <- tempfile()
writeLines(LETTERS, con = tf)
BiocFileCache::bfcadd(bfc, rname = "invalid-file", fpath = tf)
Expand All @@ -55,6 +58,13 @@ test_that("invalid cache entry detected", {
})


## check we can't write multiple entries with the same hash
test_that("we can't create multiple entries with the same hash", {
expect_true(.addToCache(bfc = bfc, result = 1:100, hash = "testing"))
expect_false(.addToCache(bfc = bfc, result = 1:100, hash = "testing"))
})



test_that("Cache details are printed", {
expect_message( biomartCacheInfo(),
Expand Down

0 comments on commit 94a1f92

Please sign in to comment.