spey.system.cache._PerInstanceCacheDescriptor#
- class spey.system.cache._PerInstanceCacheDescriptor(fn: Callable, maxsize: int, copy_on_return: bool)[source]#
Non-data descriptor that provides per-instance caching for instance methods.
On the first
instance.methodattribute lookup the descriptor builds a dedicated cached callable whose internal cache storage is private to that instance. The wrapper is stored ininstance.__dict__[method_name]so that Python’s attribute-lookup rules (instance dict beats non-data descriptor) bypass the descriptor on all subsequent accesses – zero overhead after the first call.For classes that use
__slots__without__dict__, the descriptor falls back to a descriptor-level mapping keyed byid(obj). If the instance supports weak references a callback is registered to clean up the mapping entry when the instance is garbage-collected; otherwise the entry persists (bounded, and equivalent to the prior behaviour whereselfwas serialised into a closure-level cache key).Two distinct instances therefore maintain completely independent caches even when they invoke the method with identical arguments, and instances of different subclasses that happen to share the same method name are equally isolated.
selfis never serialised into cache keys. The cache dict holds only non-self argument tokens as keys and computed results as values; no reference to the instance is stored in the cache dictionary.This class is an implementation detail of
cache_results(); obtain one viacache_results(..., per_instance=True)or by decorating a method whose first parameter isself(auto-detected).Methods
__init__(fn, maxsize, copy_on_return)