Registry / database / diskcache

diskcache

JSON →
library5.6.3.post1pypiunverified

DiskCache is a pure-Python, thread-safe, and process-safe disk-backed persistent cache. It supports various data types, cache eviction policies, and can be used in web servers, for caching slow function results, and in batch jobs. The library is actively maintained with regular updates.

databaseserialization
Install & Compatibility
Where this runs
tested against v5.6.3 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.925 runs
installs and imports cleanly · install 0.0s · import 0.062s · 18.1MB
glibc
py 3.103.925 runs
installs and imports cleanly · install 1.7s · import 0.051s · 19MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

from diskcache import Cache
from diskcache import json_serializer

This quickstart demonstrates how to initialize a DiskCache instance, store and retrieve data, and clean up the cache directory. It uses a context manager (`with Cache(...)`) for proper cache management and shows basic `set` and `get` operations. A `size_limit` is included as a best practice.

from diskcache import Cache import os # Create a cache in a specified directory # It's good practice to manage cache directories, e.g., in a temp folder or user data dir. cache_dir = os.path.join(os.getcwd(), 'my_app_cache') # Use a context manager to ensure the cache is properly closed with Cache(cache_dir, size_limit=1e9) as cache: # Set a key-value pair cache.set('greeting', 'Hello, DiskCache!') # Get a value by key value = cache.get('greeting') print(f"Retrieved value: {value}") # Check if a key exists if 'another_key' not in cache: cache.set('another_key', 123) print(f"Another key's value: {cache.get('another_key')}") # The cache persists after the 'with' block, and can be reopened. with Cache(cache_dir) as cache: print(f"Reopened cache value: {cache.get('greeting')}") # Clean up the cache directory for repeated runs in examples # In a real application, you would manage its lifecycle. import shutil if os.path.exists(cache_dir): shutil.rmtree(cache_dir)
diskcache --version
Debug
Known footguns
breakingDiskCache v5.0.0 introduced a breaking change in the internal cache format (pickle_version). Upgrading from pre-5.0 versions may render existing caches unreadable.
gotchaWhen using `json_serializer` (e.g., `Cache(serializer=json_serializer)`), all objects stored must be JSON serializable. Custom objects, datetime objects, or non-serializable types will raise a `TypeError`.
gotchaNot setting a `size_limit` or `cull_limit` can lead to the cache growing indefinitely, consuming large amounts of disk space, especially in long-running applications or with large data items.
gotchaDiskCache relies on file locking for thread and process safety. This mechanism can be unreliable or lead to performance issues when the cache directory is located on Network File Systems (NFS), especially across different operating systems.
Upgrade
Version history

Breaking-change detection hasn't run for this library yet.

Audit
Security & dependencies

CVE tracking and dependency tree are planned for a later release.

Agent activity
18 hits · last 30 days
gptbot
4
amazonbot
4
claudebot
4
ahrefsbot
3
chatgpt-user
3
Resources