Skip to content

Add Multiton.clear_cache with type and key filters - #11

Merged
sjperkins merged 4 commits into
mainfrom
add-clear-cache-method
Sep 28, 2026
Merged

sjperkins merged 4 commits into
mainfrom
add-clear-cache-method

Conversation

@sjperkins

@sjperkins sjperkins commented Sep 28, 2026 •

Copy link
Copy Markdown
Member

Adds a public way to evict cached Multiton instances, motivated by ratt-ru/xarray-ms#177 (part 2: no public, per-measurement-set way to evict cached tables).

Changes

  • Multiton.clear_cache(instance_type=None, *, where=None) -> int
    • With no filters, clears the whole cache and the expiry heap.
    • instance_type matches with isinstance (a type, tuple of types or union, subclasses included).
    • where(instance) matches on the cached instance itself, so callers can evict only instances tied to a particular resource, e.g. one dataset's table handles, while leaving other entries (such as an expensive derived structure) cached.
    • Filters combine with AND. Returns the number of entries evicted.

Like release(), clear_cache doesn't wait for constructions already in progress.

Intended use in xarray-ms

def _ms_tables(obj):
  return isinstance(obj, Table) and os.path.abspath(obj.name()) == os.path.abspath(ms)

Multiton.clear_cache(where=_ms_tables)

This is independent of part 1 of that issue (memory retained per MSv2Structure build). That was a reference leak in arcae's merge_np_partitions, fixed in ska-sa/arcae#244. release(ms) still avoids an unnecessary structure rebuild when only the table handles need reopening.

Tests

New tests cover full and filtered clears, subclass/tuple/union matching, where with AND semantics, the return count, stale heap entries after a clear, and a per-dataset eviction that mirrors xarray-ms's table/subtable/structure layout.

🤖 Generated with Claude Code

sjperkins and others added 4 commits September 23, 2026 14:52
Evicts the entire cache (and expiry heap), or only entries whose cached
instance matches a type, tuple of types or union via isinstance.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Add FrozenKey.factory, args and kwargs accessors and a where(key,
instance) predicate to Multiton.clear_cache, so callers can evict entries
by what created them (e.g. one dataset's tables) rather than only by
instance type. clear_cache now returns the number of entries evicted.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
The where predicate now receives only the cached instance, which is the
supported way to decide what to evict. FrozenKey no longer exposes
factory, args and kwargs reconstructed from its frozen tuple.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@sjperkins
sjperkins merged commit 63bbd54 into main Sep 28, 2026
10 checks passed
@sjperkins
sjperkins deleted the add-clear-cache-method branch September 28, 2026 09:12

This branch was successfully deployed

1 active deployment
pypi — 9e493f63 Deployed Sep 28, 2026 by sjperkins via deploy #69
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant