Implementation:AUTOMATIC1111 Stable diffusion webui Webui Disk Cache
| Knowledge Sources | |
|---|---|
| Domains | Caching, Performance |
| Last Updated | 2025-05-15 00:00 GMT |
Overview
Provides a thread-safe disk-based caching system using the diskcache library to persist and retrieve computed data across sessions.
Description
The Cache System module manages persistent caching for the Stable Diffusion WebUI. It uses the diskcache library to store key-value data organized by subsections on disk, with a 4 GB size limit per subsection and automatic eviction of oldest entries. The module supports a thread-safe initialization pattern using a global lock and maintains a registry of open cache objects in memory. It also provides a migration path from the legacy JSON-based cache format (cache.json) to the new disk-based format, converting old entries with a progress bar. The cached_data_for_file function implements file-modification-time-aware caching, automatically invalidating cached entries when the source file changes on disk.
Usage
Use this module when you need to persist computed data (such as file hashes, model metadata, or extension information) across application restarts. Call cache(subsection) to obtain a cache object for a given subsection, or use cached_data_for_file to automatically cache results of expensive computations tied to specific files with modification-time-based invalidation.
Code Reference
Source Location
- Repository: AUTOMATIC1111_Stable_diffusion_webui
- File: modules/cache.py
- Lines: 1-123
Signature
def dump_cache() -> None
def make_cache(subsection: str) -> diskcache.Cache
def convert_old_cached_data() -> None
def cache(subsection: str) -> diskcache.Cache
def cached_data_for_file(subsection: str, title: str, filename: str, func: callable) -> dict | None
Import
from modules.cache import cache, cached_data_for_file
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| subsection | str | Yes | Identifier for the cache subsection (e.g., "hashes", "extensions-git") |
| title | str | Yes | Key for the cached data entry within the subsection (used by cached_data_for_file) |
| filename | str | Yes | Path to the file whose modification time is tracked (used by cached_data_for_file) |
| func | callable | Yes | Function that generates the data if not found in cache (used by cached_data_for_file) |
Outputs
| Name | Type | Description |
|---|---|---|
| cache_obj | diskcache.Cache | A diskcache.Cache object for the requested subsection |
| value | dict or None | The cached or freshly generated data, or None if generation fails |
Usage Examples
from modules.cache import cache, cached_data_for_file
# Direct cache access
hashes_cache = cache("hashes")
hashes_cache["my_model"] = {"sha256": "abc123", "mtime": 1234567890}
result = hashes_cache.get("my_model")
# File-based caching with automatic invalidation
def compute_metadata():
return {"tags": ["landscape", "photo"], "size": 1024}
metadata = cached_data_for_file("metadata", "image_001", "/path/to/image.png", compute_metadata)