Skip to content

DiskCache #
Find similar titles

Structured data

Category
Programming
Database

Overview #

DiskCache is disk and file based cache library, written in pure-Python. Authors of this library claim that DiskCache performs faster than Memcache and Redis libraries. And, they have shown benchmark results with comparison.

Features #

DiskCache has several features which are:

  • Pure-Python
  • Fully documented
  • 100% test coverage
  • Django compatible API
  • Thread-safe and process-safe
  • Supports multiple eviction policies (LRU and LFU included)
  • Keys support “tag” metadata and eviction

How to install #

Installing DiskCache can be done with using pip:

$ pip install diskcache

Usage Example #

Save some keys into cache.

>>> import diskcache as dc
>>> cache = dc.Cache('tmp')
>>> cache.set(b'foo1') = b'bar1'
>>> cache.set(b'foo2') = b'bar2'
>>> cache.close()

Retrieve values from cache.

>>> import diskcache as dc
>>> cache = dc.Cache('tmp')
>>> value = cache.get(b'foo1')

There’s also a set method with additional keyword parameters: expire, read, and tag.

>>> from io import BytesIO
>>> cache.set(b'key', BytesIO('value'), expire=5, read=True, tag=u'data')
True

In the example above: the key expires in 5 seconds, the value is read as a file-like object, and tag metadata is stored with the key. Another method, get supports querying extra information with default, read, expire_time, and tag keyword parameters.

>>> cache.get(b'key', default=b'', read=True, expire_time=True, tag=True)
(<_io.BufferedReader
  name=u'/tmp/mycachedir/1d/6e/128a921c3b8a9027c1f69989f3ac.val'>,
 1457066214.784396,
 u'data')

Benchmarks #

In Python, we can do better. And we can do it in pure-Python!

In [1]: import pylibmc
In [2]: client = pylibmc.Client(['127.0.0.1'], binary=True)
In [3]: client[b'key'] = b'value'
In [4]: %timeit client[b'key']

10000 loops, best of 3: 25.4 µs per loop

In [5]: import diskcache as dc
In [6]: cache = dc.Cache('tmp')
In [7]: cache[b'key'] = b'value'
In [8]: %timeit cache[b'key']

100000 loops, best of 3: 11.8 µs per loop

References #

  1. http://www.grantjenks.com/docs/diskcache/
  2. https://github.com/grantjenks/python-diskcache/
0.0.1_20140628_0