How to get the value out of the cache?
Closed this issue · 2 comments
Hi Mr. Ashford,
I'm attempting to use this library, but I'm struggling to get a value other than
#object[Object [object Object]]
out of the cache... Is there a specific way I need to pull out the results, like those of core.cache
or can I just treat it like any other chan
and <!
the value out?
Hello,
The goal is the function which caches results is a direct drop-in replacement for the uncached function that it's based on. So if you had:
(let [a (<! (my-function 123)] ...
You could have:
(def my-cached-function (lru my-function))
(let [a (<! (my-cached-function 123))] ...)
And you'd get the same result. The difference between the two is that the second one, if called multiple times, would use the cached value.
As such the cache itself is intended to be hidden and invisible from calling code, the data is only accessible by calling the function with the same parameters again.
However, the cache itself can be obtained (e.g. for debugging purposes) as it's attached as metadata to the new function. So in my previous example you could do:
(-> my-cached-function meta :cache)
which would return the underlying core.cache
cache. But this should really be reserved for debugging/experimenting via the REPL rather then being used directly.
Thank you!