Skip to content
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

Closed
nallwhy opened this issue Feb 20, 2023 · 6 comments
Closed

Comments

@nallwhy
Copy link

nallwhy commented Feb 20, 2023

When update_org_default_value/2 is called, I want to evict all caches of get_value_of_user/2associated with the org_id.

defmodule MyModule do
  @decorate cacheable(cache: MyCache, key: {__MODULE__, :get_value_of_user, [org_id, user_id]})
  def get_value_of_user(org_id, user_id) do
    ...
  end

  @decorate cache_put(cache: MyCache, key: ?)
  def update_org_default_value(org_id, default_value) do
    ...
  end
end

ex) when update_org_default_value(1, true) is called, caches below are evicted.

{__MODULE__, :get_value_of_user, [1, 1]})
{__MODULE__, :get_value_of_user, [1, 2]})
{__MODULE__, :get_value_of_user, [1, 3]})
...

not below

{__MODULE__, :get_value_of_user, [2, 4]})
{__MODULE__, :get_value_of_user, [3, 5]})
...

Can I do this with nebulex?

@nallwhy nallwhy changed the title Is there a good way to delete multiple caches at once? Is there a good way to evict multiple caches at once? Feb 20, 2023
@nallwhy nallwhy changed the title Is there a good way to evict multiple caches at once? Is there a good way to evict multiple caches at once by some conditions? Feb 20, 2023
@cabol
Copy link
Owner

cabol commented Feb 20, 2023

Hi!

You can evict a key or multiple keys with the decorators cache_evict or cache_put by means of:

  • Option keys:, but you have to explicitly tell what keys you want to evict.
  • You can also use key_generator: option, which allows you to write your own key generation; the :key_generator option can be used in all decorators.

Now, according to what you are trying to achieve perhaps the :key_generator may be more useful because I see you want to dynamically generate the keys to remove, don't you? Anyway, you can see more details and examples in the Nebulex Caching Docs, let me know if that helps.

@nallwhy
Copy link
Author

nallwhy commented Feb 21, 2023

hm.. I don't think I understood it.
What I thought was evicting cache with a function like

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

@nallwhy
Copy link
Author

nallwhy commented Feb 21, 2023

or connecting multiple cache keys to a value

image

@nallwhy
Copy link
Author

nallwhy commented Feb 21, 2023

Oh it's in the docs.

https://hexdocs.pm/nebulex/getting-started.html#delete-all-entries-from-cache-matching-the-given-query

Can I use that with cache_put decorator?

@cabol
Copy link
Owner

cabol commented Feb 21, 2023

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 key_generator option.

@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

@nallwhy
Copy link
Author

nallwhy commented Feb 22, 2023

Cool! I'll try that.
Thanks!

@cabol cabol closed this as completed Mar 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants