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

Garrett Smith <>
Tue Nov 10 18:45:51 CET 2009


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


More information about the erlang-questions mailing list