[erlang-questions] Best way to implement a simple cache

Jan Vincent <>
Wed Nov 11 16:41:49 CET 2009


Why wouldn't an ETS be good for this?

On Nov 11, 2009, at 1:45 AM, Garrett Smith wrote:

> On Tue, Nov 10, 2009 at 11:25 AM, Jachym Holecek <> wrote:
>> Hello,
>> 
>> # Jan Vincent 2009-11-10:
>>> I wanted your take on how to implement a simple small key-value
>>> cache, maybe holding around a few hundred tuples. The thing is, I
>>> don't want the cache to be consuming all my memory so that entries
>>> in the cache expires if it isn't read for some time or some maximum
>>> size is met.
>> 
>> An ETS table owned by a gen_server that runs periodic cleanup on it?
>> 
> 
> I find myself writing purpose built gen_servers that maintain the
> cache their state for this sort of thing. To 'expire' items in the
> cache, you could run another process as a timer that calls an expire
> method on the gen_server. There are more moving parts here, but
> they're decoupled and avoid using ETS for what's a pretty simple
> caching requirement.
> 
> Garrett

Jan Vincent Liwanag







More information about the erlang-questions mailing list