LRU cache (and other things missing from the standard library ...)

Josiah Carlson jcarlson at uci.edu
Mon Nov 1 21:55:26 CET 2004


Gerhard Haering <gh at ghaering.de> wrote:
> I recently implemented a LRU cache. And I wondered why such a useful class is
> not already in the standard library. The only thing I found was a cookbook
> entry but the comments said it was problematic.

The comments were in regards to it not updating a cache entry on read. 
It has since been changed to update on read, as mentioned in the
comments, and an underlying bug has also been fixed.


> Btw. my implementation is currently at
> http://initd.org/svn/initd/pysqlite/trunk/misc/lru.py Does anybody see any
> problems with this code?

I have not run your code, but it only offers get and display methods. 
While getting with your provided factory is certainly sufficient for
adding items to the LRU, you do not offer a mechanism for removing items
from the cache (if an item changes), nor iterating through items in the
cache (which may be necessary).


> I've already started translating it into C (*) , which speeds it up by between
> factor 5 to 8. Maybe a candidate for the standard library once it's finished?

Maybe, maybe not.  There hasn't been much demand for the original recipe
that mirrors a dictionary interface, and I wouldn't imagine that this
new implementation would be of high demand either.


 - Josiah




More information about the Python-list mailing list