Code Monkey home page Code Monkey logo

Comments (6)

cabol avatar cabol commented on July 17, 2024 1

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

from nebulex.

cabol avatar cabol commented on July 17, 2024

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.

from nebulex.

nallwhy avatar nallwhy commented on July 17, 2024

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

from nebulex.

nallwhy avatar nallwhy commented on July 17, 2024

or connecting multiple cache keys to a value

image

from nebulex.

nallwhy avatar nallwhy commented on July 17, 2024

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?

from nebulex.

nallwhy avatar nallwhy commented on July 17, 2024

Cool! I'll try that.
Thanks!

from nebulex.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.