-
Notifications
You must be signed in to change notification settings - Fork 59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for storr caches? #68
Comments
Couldn't that go into a separate package? But happy to implement it whereever sensible. |
This proof of concept seems to work: library(memoise)
library(storr)
library(cachem)
library(rlang)
st <- storr_environment()
wrap_storr_cache <- function(st, missing = cachem::key_missing(), namespace = st$default_namespace) {
missing_ <- enquo(missing)
structure(
list(
get = function(key, missing = missing_) {
tryCatch(
st$get(key, namespace = namespace),
error = function(e) eval_tidy(as_quosure(missing))
)
},
set = function(key, value) st$set(key, value, namespace = namespace),
exists = function(key) st$exists(key, namespace = namespace),
remove = function(key) st$del(key, namespace = namespace),
reset = function() st$clear(namespace = namespace),
keys = function() st$list(namespace = namespace),
prune = st$gc, # gc is not namespaced
size = function() st$list(namespace = namespace),
## Hack for debugging: keep a reference to the storr object
## itself as well
.st = st
),
## Not sure if this is correct, but it makes it print nicely
class = c("cachem")
)
}
st_cache <- wrap_storr_cache(st)
mysqrt <- function(x) {
message("Computing sqrt of ", deparse1(x))
sqrt(x)
}
mysqrt_memo <- memoise(mysqrt, cache = st_cache)
mysqrt_memo(1:10)
mysqrt_memo(1:10)
mysqrt_memo(1:10) |
That seems like a reasonable implementation. I don't know which package it should live in, though. A few other notes:
|
I don't know about the rds backend, but I believe the more interesting storr backends all use databases that provide proper atomicity guarantees, even under concurrent access from multiple R processes. |
Oh, it looks like storr specifically allows for the possibility of a key being stored but the corresponding data being deleted from the object store: in this case, it returns HashError as described here: https://richfitz.github.io/storr/articles/storr.html#classed-exceptions |
As for where this code should live, perhaps it should be added to cachem, along with additional code to implement the appropriate cache size/age expiry logic. |
I love @richfitz's
storr
API, and it opens up all sorts of storage backends without requiring extra work frommemoise
itself.The text was updated successfully, but these errors were encountered: