Simple lru cache?

Ype Kingma ykingma at accessforall.nl
Sat Oct 5 08:54:57 EDT 2002


Guido Goldstein schreef:

> 
> Hi!
> 
> On Fri, 04 Oct 2002 22:52:06 +0200
>   Ype Kingma <ykingma at accessforall.nl> wrote:
>> Hello,
>> 
>> Does anyone know of a good lru cache implementation in python?
>> I tried google, and found some things in zope, but these are
>> more complicated than what I need.
> 
> You might have a look at
>   http://www.a-nugget.org/downloads/LRUCache.py
> 
> It's a time based lru-cache with manual or automatic shrink.
> I think it's commented enough to get a rough understanding of how it
> works.
> 
> Of course, comments and suggestions are welcome.


In my first attempt with the doubly linked list I store a doubly linked
list element together with the user data. Such an element can be
removed quite easily from the (lru) list it is in, after which
it can be inserted in front. Also the final element can be removed
when the cache fills up. For this the key needs to be stored in
the doubly linked list element.

You store a timestamp together with the user data in a dictionary so
shrinking can be based on age. Such shrinking needs an extra effort
periodically but it's probably more efficient.

Thanks,
Ype




More information about the Python-list mailing list