Welcome! Please see the About page for a little more info on how this works.

–1 vote
in core.cache by
closed by

Was misled by the example in the README.md the other day.

I did the equivalent of the following
(w/lookup-or-miss C3 :b (constantly (do (Thread/sleep 1000) 42)))

instead of

(w/lookup-or-miss C3 :b (fn [& args] (do (Thread/sleep 1000) 42)))

Resulting in the IO call I was doing which takes the place of (Thread/sleep 1000) to always be evaluated. This resulted in a "cache" that returns the value from the cache but will always trigger the call

closed with the note: Question answered in the comments
by
For background:

Hey Sean, wanted to ask how do I go about filling an issue to core.cache. I would like to contribute this following amendment to the README.md. Of course hearing thought on whether any changes need to be made. https://github.com/zackteo/core.cache/commit/e4600ca53a85b080bad55f1fe7576621e28a6fef

But understand after trying to create a PR that I need to go through a different process? I have signed the CA previously to amend some bits of the Clojure site but was still able to do a PR for that.

I tried to login to https://clojure.atlassian.net/jira/software/c/projects/CCACHE/issues to create an issue but think that I do not have access rights for that which makes sense. Is there right pathway to raise a question https://ask.clojure.org/index.php/contrib-libs which someone will eventually create an issue in JIRA for?

Do I follow this https://clojure.org/community/contributing#_reporting_problems_and_requesting_enhancements?

1 Answer

0 votes
by

I don't understand your question. lookup-or-miss accepts a function of one argument -- the key -- which is invoked when the cache does not contain the requested value.

Can you provide a specific repro case and explanation of your perceived bug? I don't see what the issue is based on your example.

by
Oh, I think I see... this has nothing to do with core.cache but to do with your understanding of the core constantly function: it always evaluates its argument (since it is a function), before returning a function of one argument (that returns that precomputed value).

This is just how Clojure works.
by
I had a discussion with Sean about what I was working on

I had something like this in a let binding like so (where s3-path->file-size!  is a network call)

```
(let [s3-size (w/lookup-or-miss cache file-path (constantly (s3-path->file-size! file-path))]
;; do stuff
)
```

So what I ended up realising was that I would get the value of the file-path from the cache but due to constantly I would unintentionally force the evaluation of s3-path->file-size!
 .
Understand that this was a misunderstanding on my part on how evaluation works. But was supposing this could be an error someone else also encounters in the future.
by
Sean responded:

(let [s3-size (w/lookup-or-miss cache file-path s3-path->file-size!)]
  ...)
That's what you want. You pass a function, it will be invoked on the key (file-path) if the cache entry needs to be computed.
by
Sean has also updated the docs with an explicit partial example with w/lookup-or-miss on top of the one for cache/through-cache

https://github.com/clojure/core.cache/commit/803d1c0b060ac0ad14c0cae6e6bcefa8c907199b
...