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

Garrett Smith g@REDACTED
Wed Nov 11 17:23:01 CET 2009


> On Nov 11, 2009, at 1:45 AM, Garrett Smith wrote:
>
>> On Tue, Nov 10, 2009 at 11:25 AM, Jachym Holecek <freza@REDACTED> 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.

On Wed, Nov 11, 2009 at 9:41 AM, Jan Vincent <jvliwanag@REDACTED> wrote:
> Why wouldn't an ETS be good for this?

I'm sure it'd be fine for it :)

I personally like to avoid using the "global" structures for data
management whenever I can get by with a process. I like being able to
look at a function header and know that I'm seeing everything that
could effect the result.

That said, I'm just not experienced with the various patterns in
Erlang to have strong opinions either way. Just my inclination.

Garrett


More information about the erlang-questions mailing list