-
Notifications
You must be signed in to change notification settings - Fork 74
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
Is there a good way to evict multiple caches at once by some conditions? #192
Comments
Hi! You can evict a key or multiple keys with the decorators
Now, according to what you are trying to achieve perhaps the |
hm.. I don't think I understood it. import Ex2ms
@decorate cache_put(cache: MyCache, key_condition: fn key -> key_condition(key, org_id) end)
def update_org_default_value(org_id, default_value) do
...
end
defp key_condition({__MODULE__, :get_value_of_user, [org_id, user_id]}, org_id) do
true
end
defp key_condition(_key, _org_id) do
false
end |
Oh it's in the docs. Can I use that with |
Unfortunately, you can't, at least not directly in the decorator, this is a more specific use case. However, there might be a workaround using the @decorate cache_evict(cache: MyCache, key_generator: CustomKeyGenerator)
def update_org_default_value(org_id, default_value) do
...
end The key generator: defmodule CustomKeyGenerator do
@behaviour Nebulex.Caching.KeyGenerator
@impl true
def generate(_module, :update_org_default_value, [org_id, default_value]) do
# Here you can run the logic you need, it is not ideal because the idea is to return the keys you want to evict
MyCache.delete_all(....) #=> your match-spec goes here
# Return `nil` because you are already deleting the keys, so the decorator won't make any effect
nil
end
end Let me know if that helps. On the other hand, this makes me think of a new feature, I've created a new ticket for it, check it out: #193 |
Cool! I'll try that. |
When
update_org_default_value/2
is called, I want to evict all caches ofget_value_of_user/2
associated with theorg_id
.ex) when
update_org_default_value(1, true)
is called, caches below are evicted.not below
Can I do this with nebulex?
The text was updated successfully, but these errors were encountered: