You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There is a use that I believe is fairly common that I do not think fits in the Nebulex.Caching macro DSL.
Sometimes one wants to conditionally cache a value based on the result of the underlying function logic. This is easy to achieve by using the lower-level APIs Cache.get and Cache.set but it is fairly verbose and repeatable. Also, I was really enjoying the Nebulex.Caching DSL.
In my specific use case, I have a cache-through layer in front of a REST API and I only want to cache the results when the REST query underneath succeeds. Sometimes there are transient error responses or short periods of time where this API is down.
I added an optional function argument called match (for lack of better naming at the moment) to defcacheable that receives the result of the logic and returns a boolean value. Based on that, the value will be cached or not cached. For example:
defcacheableget_user(id),cache: Cache,key: {User,id},match: &match?/1doTesla.get("users/#{id}")enddefcacheableget_users(id),cache: Cache,key: {User,:list},match: &match?/1doTesla.get("users")end# Successful requests will be cacheddefmatch?({:ok,_}),do: true# Failed requests will not be cacheddefmatch?(_),do: true
If you think this would be worth adding I would be happy to write a PR, I already have something I can base it off. It is fairly straight-forward.
By the way, thanks for such a great library, cheers 🙇 !
The text was updated successfully, but these errors were encountered:
There is a use that I believe is fairly common that I do not think fits in the
Nebulex.Caching
macro DSL.Sometimes one wants to conditionally cache a value based on the result of the underlying function logic. This is easy to achieve by using the lower-level APIs
Cache.get
andCache.set
but it is fairly verbose and repeatable. Also, I was really enjoying theNebulex.Caching
DSL.In my specific use case, I have a cache-through layer in front of a REST API and I only want to cache the results when the REST query underneath succeeds. Sometimes there are transient error responses or short periods of time where this API is down.
I added an optional function argument called
match
(for lack of better naming at the moment) todefcacheable
that receives the result of the logic and returns a boolean value. Based on that, the value will be cached or not cached. For example:If you think this would be worth adding I would be happy to write a PR, I already have something I can base it off. It is fairly straight-forward.
By the way, thanks for such a great library, cheers 🙇 !
The text was updated successfully, but these errors were encountered: