I'm looking for a Python caching library but can't find anything so far. I need a simple dict
-like interface where I can set keys and their expiration and get them back cached. Sort of something like:
cache.get(myfunction, duration=300)
which will give me the item from the cache if it exists or call the function and store it if it doesn't or has expired. Does anyone know something like this?
I think the python memcached API is the prevalent tool, but I haven't used it myself and am not sure whether it supports the features you need.
Take a look at Beaker:
You might also take a look at the Memoize decorator. You could probably get it to do what you want without too much modification.
You can use my simple solution to the problem. It is really straightforward, nothing fancy:
It indeed lacks expiration funcionality, but you can easily extend it with specifying a particular rule in MemCache c-tor.
Hope code is enough self-explanatory, but if not, just to mention, that cache is being passed a translation function as one of its c-tor params. It's used in turn to generate cached output regarding the input.
Hope it helps
Joblib http://packages.python.org/joblib/ supports caching functions in the Memoize pattern. Mostly, the idea is to cache computationally expensive functions.
You can also do fancy things like using the @memory.cache decorator on functions. The documentation is here: http://packages.python.org/joblib/memory.html