Simple lru cache?

Ype Kingma ykingma at accessforall.nl
Fri Oct 4 16:52:06 EDT 2002


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.

I need a cache to avoid moving the disk head around in a large database.
For this I'd like a dictionary with a maximum number of items in which
the least recently used item is discared when the maximum
size would be exceeded.

I made one from a dictionary and a special purpose doubly linked
list that allows moving any list element to the front.
It works, but I'm not really happy with all the list elements in this list.

The alternative is a normal python list, but it would probably take
too much time for shifting the aging elements.
Or is there a way to pick one element randomly from a list
and move it to front or back and shift the rest to make place,
all in a single python statement, ie. without using a for statement?

Java 1.4 has a linked hash table that does much of what I need,
but I can't take java/jython for granted all the time.
See:
http://java.sun.com/j2se/1.4.1/docs/api/java/util/LinkedHashMap.html
which has a removeEldestEntry() method and allows to rejuvenate
an entry by deletion and reinsertion.

Regards,
Ype

-- 

email at xs4all.nl



More information about the Python-list mailing list