Development

NAVIGATION
CATEGORIES
REFERRENCE
LINKS
  • Optimize a cache

    5 answers - 1046 bytes - related search similar search Add To My Delicious Add To My Stumble Upon Add To My Google Mark Add To My Facebook Add To My Digg Add To My Reddit

    Hello,
    I am building a object cache in python, The cache has a maximum size and the
    items have expiration dates.
    At the moment I'm doing like that:
    cache = {} # create dictionary
    cache[ID] = (object, timestamp) # Save a tuple with the object itself and a
    timestamp (from datetime.datetime.now()) under a unique object ID. This
    make looking up a certain item a cheap operation (AFAIK). After looking up
    my program checks if the expiration date and returns cache[ID][1] or
    retrieve a new object and overwrite the one saved in the cache.
    My problem now:
    When len(cache) cacheSize I need to remove the oldest objects (the ones
    with the smalest timestamp). For that I need to iterate through all items
    and get the oldest one: (pseudo code)
    oldest = datetime.datetime.now()
    for i in cache:
    if i[1] < a:
    id = i
    n = i[0]
    del cache[id]
    What possible you see to optimize this lookup? anything else you see to
    make it better?
    Thanks,
    Florian
  • No.1 | | 183 bytes | |

    Florian Lindner <Florian.Lindner (AT) xgm (DOT) dewrites:
    What possible you see to optimize this lookup? anything else you see to
    make it better?
    Use the heapq module.
  • No.2 | | 19 bytes | |

    i think ring buffer
  • No.3 | | 332 bytes | |

    Florian Lindner <Florian.Lindner (AT) xgm (DOT) dewrites:
    What possible you see to optimize this lookup? anything else you see to
    make it better?

    Do you need to update the timestamp of cached entries when you access
    them? If yes, use the heapq module. If no, is the cache something
    different from a simple FIF?
  • No.4 | | 14 bytes | |

    a ring buffer?
  • No.5 | | 454 bytes | |

    Message
    From: "Florian Lindner" <Florian.Lindner (AT) xgm (DOT) de>

    Hello,
    I am building a object cache in python, The cache has a maximum size and
    the
    items have expiration dates.
    At the moment I'm doing like that:

    What possible you see to optimize this lookup? anything else you see to
    make it better?

    Have a look at
    it already has some of your requirements, and can be used as a building
    block.

Re: Optimize a cache


max 4000 letters.
Your nickname that display:
In order to stop the spam: 1 + 0 =
QUESTION ON "Development"

EMSDN.COM