Simple lru cache?

Steve Holden sholden at holdenweb.com
Fri Oct 4 18:04:23 EDT 2002


"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.
>
> 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?
>
What, you mean like

    mylst = mylst[:n] + mylst[n+1:] + [mylst[n]]

for example?

[...java...]

regards
-----------------------------------------------------------------------
Steve Holden                                  http://www.holdenweb.com/
Python Web Programming                 http://pydish.holdenweb.com/pwp/
Previous .sig file retired to                    www.homeforoldsigs.com
-----------------------------------------------------------------------






More information about the Python-list mailing list