
💾 Need persistent cache in Python without spinning up Redis or Memcached? DiskCache does it in one line.
diskcache is an open-source Python library (Apache 2) that uses SQLite + memory-mapped files to deliver disk-based caching that outperforms classic in-memory solutions.
⚡ Faster than Memcached?
# Memcached
%timeit client[b'key']
# → 25.4 µs per loop
# DiskCache
%timeit cache[b'key']
# → 11.8 µs per loopYes, DiskCache is faster than Memcached for local reads.
🔧 Basic usage:
import diskcache as dc
cache = dc.Cache('tmp')
cache['key'] = 'value'
print(cache['key']) # 'value'
# With stampede-prevention memoization
from diskcache import memoize_stampede
@memoize_stampede(cache, expire=60)
def expensive(param):
return compute_something(param)✨ Features:
- Thread-safe and process-safe (great for multiprocessing)
- Eviction policies: LRU, LFU, and more
- Compatible with Django as a cache backend
- Persistent Deque and Dict across processes
- Cross-process Lock and throttle
💡 Explanation in a nutshell#
DiskCache stores values in SQLite (for small keys) or on the filesystem (for large values). Since it uses memory-mapped files, reads don’t require copying data between processes. This makes it surprisingly fast for a disk cache, and fully persistent across restarts — no separate server required.
More information at the link 👇
Also published on LinkedIn.
