I just tried a Python shelve module as the persistent cache for data fetched from the external service. The complete example is here.
I was wondering what would the best
I wrote a locking (thread- and mulitprocess-safe) wrapper around the standard shelve
module with no external dependencies:
https://github.com/cristoper/shelfcache
It meets many of your requirements, but it does not have any sort of backoff strategy to prevent thundering herds, and if you want Reader-Writer lock (so that multiple threads can read, but only one write) you have to provide you own RW lock.
However, if I were to do it again I'd probably "just use sqlite". The shelve
module which abstracts over several different dbm implementations, which themselves abstract over various OS locking mechanisms, is a pain (using the shelfcache flock
option with gdbm on Mac OS X (or busybox), for example, results in a deadlock).
There are several python projects which try to provide a standard dict interface to sqlite or other persistent stores, ex: https://github.com/RaRe-Technologies/sqlitedict
(Note that sqldict
is thread safe even for the same database connection, but it is not safe to share the same database connection between processes.)