# Invalidating groups of keys A user changes their display name, and for the next five minutes half the site still uses the old one: the profile page, the sidebar widget, three API responses, each cached on its own schedule and each wrong in its own way. A single change often makes many cache entries stale, and deleting them one by one means knowing every key that was ever derived from that user. There are three good ways to handle this: a naming convention and prefix deletes, tags, or generation numbers. # Name keys hierarchically Put the thing a key depends on at the front of the key, from the general to the specific: user:42:profile user:42:sidebar user:42:api:/v1/me episode:512:page episode:512:transcript Everything about user 42 now shares the prefix `user:42:`. Note the trailing colon: without it, `user:4` would also match `user:42`. Keys can also be tuples, like `('user', 42, 'profile')`, which saves formatting strings and leaves no separator to get wrong. A tuple key has no prefix to scan, though, so a group of tuple keys is invalidated with tags (option 2) or generation numbers (option 3). The keys [memoize](../reference/Cache.memoize.md#diskcache_oxide.Cache.memoize) builds are tuples too. # Option 1: delete by prefix `iterkeys(prefix=...)` is an indexed range scan, so finding a user's keys costs time in proportion to how many there are, not the size of the cache. It fetches keys a page at a time with no lock held in between, so deleting while iterating is safe. ``` python import tempfile from diskcache_oxide import Cache cache = Cache(tempfile.mkdtemp()) for key in ['user:42:profile', 'user:42:sidebar', 'user:420:profile', 'episode:512:page']: cache.set(key, '...', expire=3600) def invalidate_prefix(prefix: str) -> int: removed = 0 for key in cache.iterkeys(prefix=prefix): removed += cache.delete(key) return removed print(invalidate_prefix('user:42:'), 'removed') print(sorted(cache)) ``` 2 removed ['episode:512:page', 'user:420:profile'] This is simple and frees the space right away. It costs one small write per key, so it's best when a group has tens or hundreds of keys, not hundreds of thousands. A prefix matches `str` keys only. `iterkeys(prefix='user:42:')` skips a `bytes` key `b'user:42:x'` and every tuple key, and a tuple as the prefix raises `TypeError`: ``` python cache.set(('user', 42, 'orders'), [1, 2], expire=3600) cache.set('user:42:profile', '...', expire=3600) print(list(cache.iterkeys(prefix='user:42:'))) ``` ['user:42:profile'] # Option 2: tags A tag groups entries without a naming convention: pass `tag=` when you store, and `evict(tag)` removes every entry that has it. Entries for one user can live under unrelated keys, an API response keyed by URL, a rendered fragment keyed by template, and still go together. ``` python cache.set('page:/settings', '...', expire=3600, tag='user:7') cache.set(('api', '/v1/me', 7), {'name': 'Sam'}, expire=3600, tag='user:7') cache.set('page:/about', '...', expire=3600) print(cache.get('page:/settings', tag=True)[1]) print(cache.evict('user:7'), 'removed') print(sorted(cache, key=repr)) ``` user:7 2 removed ['episode:512:page', 'page:/about', 'user:420:profile', 'user:42:profile', ('user', 42, 'orders')] [evict](../reference/Cache.evict.md#diskcache_oxide.Cache.evict) works in chunks over an index, like [clear()](../reference/Cache.clear.md#diskcache_oxide.Cache.clear), letting other writers in between, and removes only entries stored before it started, so a fresh entry written for the user while it runs survives. A tag is any key type and is compared by exact type, so `tag=7` and `tag='7'` are different tags. An overwrite replaces the tag, and one without `tag=` leaves the entry untagged, so tag every write in the group. `memoize(tag=...)` tags every result it stores. Like prefix deletes, it frees the space at once, one row per entry. # Option 3: generation numbers Instead of deleting anything, keep a generation number per group and put it in every key. To invalidate the group, change the generation. Every old key becomes unreachable at once, in a single write, however many keys there were. The old entries are never read again, and they leave the cache when they expire or are evicted. ``` python import time def generation(user_id: int) -> int: key = f'gen:user:{user_id}' gen = cache.get(key) if gen is None: cache.add(key, time.time_ns()) # if several workers race, one value wins gen = cache.get(key) return gen def user_key(user_id: int, name: str) -> str: return f'user:{user_id}:g{generation(user_id)}:{name}' def invalidate_user(user_id: int) -> None: cache.set(f'gen:user:{user_id}', time.time_ns()) cache.set(user_key(7, 'profile'), {'name': 'Sam'}, expire=3600) print(cache.get(user_key(7, 'profile'))) invalidate_user(7) print(cache.get(user_key(7, 'profile'))) ``` {'name': 'Sam'} None Generation numbers cost one extra read per lookup (the generation, which is tiny and hot) and make invalidation free. Give the real entries an [expire](../reference/Cache.expire.md#diskcache_oxide.Cache.expire), since nothing deletes them. The generations are timestamps rather than a counter starting at 0 for a reason. A generation key is written rarely, and the cache evicts the least recently *stored* entries first, so under size pressure it can be evicted. A counter would then restart at 0 and bring old entries back to life. A new timestamp is always larger than any generation used before, so a lost generation only costs a few cache misses. # Which to use - **Prefix deletes** when groups are small, the keys are strings, and you want the space back immediately. - **Tags** when a group's keys don't share a prefix, are tuples, or come from code (like [memoize](../reference/Cache.memoize.md#diskcache_oxide.Cache.memoize)) whose keys you don't choose. - **Generation numbers** when groups are large, or invalidation is frequent, or many workers might invalidate the same group at the same time. # While caching is switched off If you turn caching off with [NullCache](../reference/NullCache.md#diskcache_oxide.NullCache), pass the real cache as `invalidate=`. Prefix deletes and `evict(tag)` still reach the real cache ([iterkeys](../reference/Cache.iterkeys.md#diskcache_oxide.Cache.iterkeys), [delete](../reference/Cache.delete.md#diskcache_oxide.Cache.delete) and [evict](../reference/Cache.evict.md#diskcache_oxide.Cache.evict) are all forwarded), so nothing stale is waiting when caching comes back on. See [NullCache](../user-guide/null-cache.md).