Skip to content

diskcache-oxide 0.3.5

Choose a tag to compare

@mikeckennedy mikeckennedy released this 17 Nov 03:56
· 79 commits to main since this release

Upgrades across a format change no longer fail. A cache directory that holds a cache in another on-disk format (made by an older release, or by a newer one before a rollback) now opens a new, empty cache in its subdirectory diskcache-oxide-format-N, N being this release's format (3), instead of raising FormatMismatchError, and logs a warning on the diskcache_oxide logger once per process. Of the other format's cache, only its format is read, and its contents are never changed (if that read is the last connection to close on it, SQLite may checkpoint its write-ahead log into the database file, which changes no data). Once the subdirectory exists, every later open uses it. A FanoutCache moves as a whole. This release keeps format 3, so caches made by 0.2.3 open where they are, warm; a cache still at format 2 (0.1.2) opens diskcache-oxide-format-3 beside it. See "Upgrading across a format change" in the recipes.

Caches look after themselves. One small thread per process now does each open cache's housekeeping in the background, every 5 minutes by default: it removes expired entries and their files, trims a cache left over size_limit (cull_limit entries at a time; never under eviction_policy='none' or with cull_limit=0), checkpoints a write-ahead log grown past half of wal_size_limit, and hourly clears temporary files a killed writer left and empty value folders, FanoutCache shards included. There's no longer an expire() to schedule. The caches in a process take turns across the interval, and the processes sharing a cache take turns through two new rows in its settings table (no format change: older releases ignore them). It never waits for a lock and holds the write lock only for short chunks, so requests don't notice it, and it stops for every os.fork() and starts again after, so Python never warns about forking a multi-threaded process. A cache opened with clock= gets none. Pass maintenance_interval=0 for a cache this process must never write, such as a read-only replica: even a tidy cache is written once an hour to coordinate the processes (D-061).

Added

  • maintenance_interval= on Cache, FanoutCache, Caches and Caches.register: seconds between background maintenance turns for the cache in this process (default 300, 0 for off, 1 second to a day). Per process, not stored in the cache; the shortest any open handle asked for wins.

  • maintenance_stats() on Cache, FanoutCache and NullCache, returning a MaintenanceStats dict (interval, turns, removed, failures, last_error); Caches.stats() carries it as CacheStats.maintenance. A failure is also logged once per streak, as a WARNING on the diskcache_oxide logger, by the next single-key call on the cache.

  • Caches.get(name, default=None): the named plain cache as caches[name] returns it, or default when no cache by that name is registered, so a check and a lookup are one call (if (pages := caches.get('pages')) is None: ...). A registered cache that can't open or is disabled still comes back as a NullCache.

  • remove_old_formats() on Cache, FanoutCache and NullCache (where it returns []), and on Caches for every registered cache: removes caches in an older format from the cache's directory, only files diskcache-oxide wrote, never following links, never a newer or the current format, and returns what it removed. It doesn't check whether an older worker still uses those files: call it once they've all stopped.

  • Windows on x86_64, with wheels for CPython 3.14, 3.14t, 3.15 and 3.15t. The API and the on-disk format are the same; caches made on macOS or Linux aren't affected. Windows has no fork(), so the fork guarantees are Unix-only. Getting started has a new Windows section: an open cache's directory can't be deleted, new directories inherit their parent's permissions, and a value file another program holds past about 50 ms of retries is left until check(fix=True) removes it, an hour after it was written. SQLite uses its own Win32 mutexes there (_sqlite_mutex_status() reports 'native').

Changed

  • The CacheLike protocol gained maintenance_stats(), so a hand-written fake checked with isinstance(x, CacheLike) needs it too.

  • An explicit expire() or cull() can return less than it did, since background maintenance may have removed the rows first, and volume() and the write-ahead log's size can change between calls.

  • A cache directory holding another on-disk format opens the diskcache-oxide-format-N subdirectory with a warning instead of raising FormatMismatchError (D-059). FormatMismatchError is now raised only for another format inside that subdirectory. directory on Cache and FanoutCache reports where the cache really is, the subdirectory after such a move; a pickled handle keeps the directory as passed and resolves it again. A Caches registry no longer hands out a NullCache for a format change.

  • A plain Cache opened on a FanoutCache shard directory in an older format raises LayoutMismatchError, as it always did for a current one, rather than FormatMismatchError.

  • FanoutCache(directory, shards=n) on a root that records another count no longer raises: it opens with the stored count and logs a WARNING once per process. The new keyword raise_on_mismatch=True restores the LayoutMismatchError, and Caches.register(..., raise_on_mismatch=True) turns it into an open failure (a reported NullCache). A root in another on-disk format redirects to its versioned subdirectory either way. The LayoutMismatchError for settings that don't fit the stored count now says shards=n was asked for rather than is registered with, since a plain FanoutCache raises it too.

  • len(), iterkeys(), iter() and reversed() leave out an entry the moment it expires, as get and in already did, on Cache and FanoutCache (D-060). Before, an expired entry was counted and listed until a set(), expire() or cull() removed its row, so a cache that's mostly read could report entries nobody could read. Nothing is written by the check; the rows and their space, in volume(), still go only when something removes them, so a scheduled expire() is still how a mostly-read cache frees space. len() now also counts the expired rows not yet removed, through the expiry index, so it costs a little more while such rows are waiting.

Fixed

  • import diskcache_oxide no longer calls os.register_at_fork where it doesn't exist.
  • A value file that hits a full quota (EDQUOT) now raises OSError with errno.ENOSPC, as a full disk does, since a full disk is recognized by the error's kind rather than one Unix error number.
  • iter(cache) (and a FanoutCache's, shard by shard) could yield a key twice: one deleted and stored again while the walk ran got a newer row, which the walk met again at its end. It now stops at the newest row when it began, as diskcache's does, so a key stored meanwhile isn't yielded and a re-stored one is yielded once. Found by the 24-hour soak, 8.5 hours in.
  • iter(cache) and reversed(cache) no longer sort the whole table for every page of keys. Without SQLite statistics, the planner read insertion order through the key index and sorted it, so walking a large cache took time in proportion to its size for every 512 keys (about 2 s for 200,000 entries on an M5 Max, now 22 ms); each page now seeks on the row id.

Documentation

  • An example app in examples/webapp: a FastAPI app with gunicorn, a background worker and Docker Compose, built the way the docs recommend, with tests that run it as real processes. Every page shows which cache reads served it.
  • One recommended production setup throughout the docs: a durable Caches registry with timeout=1.0 for data that can be rebuilt, and state (sessions, job claims, locks, queues) in plain caches opened directly, with eviction off, under one /var/lib/my-app directory. The Caches guide explains why state never goes through the registry.
  • New pages: Docker and Compose (volumes, a fixed UID, ownership, Kubernetes, disk sizing, backups, cleanup after a format change) and Troubleshooting (start from the symptom).
  • Corrected: the docs no longer claim network filesystems are detected at open. SQLite can't tell, so keep caches on a local disk.
  • Also new: a keyword table and the state-copying loop in Switching from diskcache, Windows notes in Getting started, a Prometheus collector in Monitoring, a dictConfig that keeps the diskcache_oxide logger, and API reference sections for types, memoize, coordination and every error.

Wheels: macOS arm64, Linux (manylinux_2_28) aarch64 and x86_64, and Windows x86_64, for CPython 3.14, 3.14t, 3.15 and 3.15t, profile-guided. Checksums are in SHA256SUMS. Full changes: CHANGELOG.md, v0.2.3...v0.3.5.