whitfin / cachex

A powerful caching library for Elixir with support for transactions, fallbacks and expirations
https://hexdocs.pm/cachex/
MIT License
1.59k stars 101 forks source link

Manually trigger cache warmers #299

Closed aselder closed 7 months ago

aselder commented 2 years ago

Is it possible to trigger a rebuild of proactively warmed cache manually, and if so how.

Also, should I be using Cachex.fetch in conjuction with a proactively warmed cached to provide fallback for times when the warmer may have missed a key or that key has expired, etc..?

whitfin commented 1 year ago

Hi @aselder!

Sorry, I seem to have missed this.

What do you mean by rebuild? Warmers are running on an interval, so they will re-run often based on how you configure them - so I'm not sure I understand your question.

You can use fetch if you want, it depends on the behaviour you want. Generally people would pick one or the other, but there's nothing wrong with using both if you need to!

elvanja commented 1 year ago

For a use case when refresh interval is long-ish (e.g. couple of days or so), one might want to refresh the entire cache explicitly, via manual execution. E.g. we have a case where due to some business changes we can cache customer related data and we don't expect it to change at all. Hence, refresh interval is set to once a day. But, there might be some changes done to that data in the background (DB) and instead of waiting for one day for those changes to be reflected, it would be nice to have a mechanism to just refresh the cache.

If there was a way to trigger warmer execution, that would do exactly that. But, as far as I can tell, there is no easy way to find out warmer process and do something like send(warmer_pid, :cachex_warmer). I also checked out Cachex.reset/2 but I think it does not trigger warmer.

Is there some other mechanism we can use for this?

whitfin commented 9 months ago

@elvanja this is a pretty good suggestion, I'm going to look into it as a potential for v3.7.0. There's no reason we can't just send that message to the warmer manually, we just need to track the PIDs properly.

Edit: not sure exactly how I think this should be done. Maybe just a Cachex.warm/1 which will notify all warmers attached to a cache. The only other option I can think of is storing the PIDs on the cache state but I'd rather avoid that, I think.

elvanja commented 9 months ago

Maybe just a Cachex.warm/1 which will notify all warmers attached to a cache

That should work, yes. I guess the point is to just to be able to somehow refresh the cache. Any mechanism you can find to do that is great, doesn't have to be through warmer(s), this was just an idea.