[erlang-questions] How would you implement TTL for ETS based cache?
Thu Mar 14 15:50:56 CET 2013
We have implemented something similar in the past -- we called it a generational cache (hope no-one accuses me of a plagiarism, for English is but a finite language after all. And so is the problem/solution space...).
The gist of it is that instead of using a single ETS table for a cache, you would use two or more "disposable" ETS tables.
The generation management is implemented as a process that, upon some criteria are met, destroys the oldest table, and creates a new one in its stead.
The youngest cache (the newest ETS table) is always queried first. If the value is not found, the older tables are queried and if entry with a corresponding key is found -- it is moved to the youngest cache.
If none of the ETS tables in a cache set contains the key, then the key is loaded from some external source and inserted into the youngest (the newest ETS table) cache.
This method worked quite well for us in the past, and we've implemented a several variants of it, e.g. time based, size based, and combination of the two.
Bear in mind that when you drop a very large ETS table (and I mean very large, as in Gigabytes of memory), it may take some time to release the allocated memory, but that may be a problem for another day.
The downside, this does not really correspond to your requirement to keep a TTL per single cache entry.
The upside, it is much faster and cheaper than traversing a single cache periodically. Also, it end up keeping only entries that are most frequently used.
Hope this helps.
On 14 Mar 2013, at 4:15 PM, Max Bourinov wrote:
> Hi Erlangers,
> How would you implement TTL with callback for ETS based cache?
> I want when TTL expires callback is triggered and cached value goes to DB.
> For key I use 32 byte long binary(), value is not bigger than 8 KB.
> Any suggestions?
> p.s. elang:send_after/3 is a most brutal approach, but how would it be when cache is under load?
> Best regards,
> erlang-questions mailing list
More information about the erlang-questions