Simple lru cache?

Paul Rubin phr-n2002b at NOSPAMnightsong.com
Fri Oct 4 18:47:30 EDT 2002


Ype Kingma <ykingma at accessforall.nl> writes:
> 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?

        index = random() % len(cache)
        entry = cache[index]
        del cache[index]

    then

        cache.append(entry)   # append to end

    or

        cache.insert(0, entry)  # insert at beginning

These move all the cache elements around but will be much faster than
using Python loops to do it.  It's probably the best way if the cache
size is less than a few thousand.



More information about the Python-list mailing list