I developed a Python client library, which is a client wrapper for a web API: a nice-to-have feature would be a local cache mechanism: this would save time and bandwidth when the library clients invoke the very same request on the web API at different times.
I have very few requirements
- the cache shall be developed (no external libraries) and run Python 2.6/2.7 envs
- the library shall provide a uniform interface, so that it is possible to use external cache mechanism (eg: memcached) wrappers, which will be dependency-injected
- the cache should be thread-safe
- the content to be cached will be the JSON payload of the web API responses.
- data provided by the web API vary at different time rats: in example, the currently observed weather over a city may vary every hour, while 15 days weather forecast may vary every 5 days or so.
What is the most simple algorithm that can be used?
I was thinking about a LRU (Least Recently Used) algorithm but I guess that other alternatives can be assessed - I'm not expert about caches at all!
You could just copy and adapt the source from lru_cache (see http://hg.python.org/cpython/file/default/Lib/functools.py).