GithubHelp home page GithubHelp logo

Post purge hooks? about cachex HOT 2 CLOSED

lostboy avatar lostboy commented on July 21, 2024
Post purge hooks?

from cachex.

Comments (2)

whitfin avatar whitfin commented on July 21, 2024

@lostboy you should realistically be able to receive a { :purge, _opts } message... but I just checked and it appears it's not working. Let me take a quick look at it!

Edit: ha, I knew that this was broken previously but for different reasons... I fixed it but only ever tested it with the internal Stats hook so I totally missed that it wouldn't fire the rest of the hooks. Fix incoming!

from cachex.

whitfin avatar whitfin commented on July 21, 2024

Ok, this is now resolved in the main branch so you can pick it up from here.

The next version will be released on Hex pretty soon, just gotta review a few things. For what it's worth, I always intend master to be stable enough to use (otherwise I keep changes in PR until it is stable), so it should be ok to just point your projects to the GitHub.

For reference, here's the test case I used to make sure the hooks were receiving:

Erlang/OTP 18 [erts-7.3] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]

Interactive Elixir (1.2.4) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> defmodule MyModule.PostPurgeHook do
...(1)>   use Cachex.Hook
...(1)> 
...(1)>   def handle_notify({ :purge, _opts }, state) do
...(1)>     IO.puts("Purge received!")
...(1)>     { :ok, state }
...(1)>   end
...(1)> 
...(1)>   def handle_notify(_action, state) do
...(1)>     { :ok, state }
...(1)>   end
...(1)> end
{:module, MyModule.PostPurgeHook,
 <<70, 79, 82, 49, 0, 0, 13, 96, 66, 69, 65, 77, 69, 120, 68, 99, 0, 0, 2, 221, 131, 104, 2, 100, 0, 14, 101, 108, 105, 120, 105, 114, 95, 100, 111, 99, 115, 95, 118, 49, 108, 0, 0, 0, 4, 104, 2, ...>>,
 {:handle_notify, 2}}
iex(2)> Cachex.start_link([ name: :my_cache, hooks: %Cachex.Hook { module: MyModule.PostPurgeHook, type: :post }, default_ttl: :timer.seconds(3), ttl_interval: :timer.seconds(5) ])
{:ok, #PID<0.149.0>}
iex(3)> Cachex.set(:my_cache, "key", "value")
{:ok, true}
Purge received!
iex(4)> 

If you want to know how many were purged, you need to add results: true to the hook declaration, and change your handler to def handle_notify({ :purge, _opts }, { :ok, count }, state).

I hope this solves your issue, and thank you very much for the kind words 😃 Please let me know if you come across anything else, or have further questions about this!

from cachex.

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.