Question

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

  1. the cache shall be developed (no external libraries) and run Python 2.6/2.7 envs
  2. 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
  3. the cache should be thread-safe
  4. the content to be cached will be the JSON payload of the web API responses.
  5. 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!

Was it helpful?

Solution

You could just copy and adapt the source from lru_cache (see http://hg.python.org/cpython/file/default/Lib/functools.py).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top