catena.cache
Caching primitives for memoising partial-quotient generator calls.
This module provides:
- :class:
Cache— an immutable-key :class:~collections.UserDictthat prevents accidental overwriting of stored results. - :class:
OrdinalCache— a :class:Cachespecialised for integer keys, tracking the smallest and largest key seen. - :class:
CacheHandler— a bookkeeping wrapper that owns a :class:Cacheinstance and records call/read statistics. - :func:
SetCache— a decorator / decorator-factory that memoises a function using arbitrary(args, kwargs)keys. - :func:
SetLightCache— a lighter variant of :func:SetCachefor single-argument functions, using the argument directly as the cache key.
BaseCache
Bases: UserDict[int, Any]
A self-computing, append-only cache that unifies the roles of the old
:class:OrdinalCache and :class:CacheHandler into a single object.
On a cache miss, the bound function func is called automatically
and the result stored; on a hit, the stored value is returned directly.
Both paths are counted separately via :attr:call_count and
:attr:read_count. Integer keys additionally maintain :attr:smallest_key
and :attr:largest_key without iterating the dictionary.
Stored entries are immutable: attempting to overwrite an existing key
raises :exc:KeyError. The mutating :class:~collections.UserDict
methods (pop, popitem, __delitem__, update, setdefault,
fromkeys, __or__) are disabled and raise
:exc:NotImplementedError. Use :meth:prune or :meth:reset to
reduce the cache size.
When maxsize is set, :meth:prune is called automatically before
each new insertion that would exceed the limit, using prune_key to
determine how many entries to retain.
Note
func must accept a single argument; the argument itself is used
as the cache key.
Warning
This class is experimental. Its interface is subject to change without notice.
Source code in catena/cache.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 | |
cache
property
Returns self for backward compatibility.
call_count
property
Number of cache misses (underlying function calls) since initialisation.
func
property
The function whose results are being cached.
func_name
property
The name of the cached function, if available.
largest_key
property
The largest key stored so far, or None if the cache is empty.
maxsize
property
The maximum size of the cache, or None if unlimited.
read_count
property
Number of cache hits since initialisation.
smallest_key
property
The smallest key stored so far, or None if the cache is empty.
stats
property
A summary of the cache's current statistics as a dictionary.
write_count
property
Number of cache writes since initialisation.
__delitem__(key)
Not supported. Raises :exc:NotImplementedError.
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
Always. |
Source code in catena/cache.py
388 389 390 391 392 393 394 395 396 | |
__getitem__(key)
Returns the cached value for key, computing and storing it on a miss.
A hit (key already in the cache) increments :attr:read_count.
A miss delegates to :meth:__missing__, which calls func,
stores the result, and increments :attr:call_count.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Any
|
The cache key. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The cached (or freshly computed) value for |
Source code in catena/cache.py
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | |
__init__(*args, func, maxsize=None, prune_key=None, seed=None, **kwargs)
Initialises the cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable
|
The single-argument function whose results are memoised. Called automatically on a cache miss. |
required |
maxsize
|
int
|
Maximum number of entries before an
automatic :meth: |
None
|
prune_key
|
Callable[[int], tuple]
|
A callable that
receives the current |
None
|
seed
|
Optional[dict[int, Any]]
|
Pre-computed |
None
|
*args
|
Any
|
Forwarded to :class: |
()
|
**kwargs
|
Any
|
Forwarded to :class: |
{}
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in catena/cache.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | |
__missing__(key)
Called by :meth:__getitem__ on a cache miss.
Invokes func(key), stores the result via :meth:__setitem__
(which enforces maxsize and updates the key-range trackers), and
increments :attr:call_count.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Any
|
The missing cache key. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The computed value |
Source code in catena/cache.py
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | |
__or__(other)
Not supported. Raises :exc:NotImplementedError.
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
Always. |
Source code in catena/cache.py
408 409 410 411 412 413 414 415 416 | |
__repr__()
Returns a concise string representation showing the bound function, call/read counts, and current size.
Source code in catena/cache.py
172 173 174 175 176 177 | |
__setitem__(key, value)
Stores value under key.
If maxsize is set and the cache is at capacity, :meth:prune is
called first to make room. Integer keys update :attr:smallest_key
and :attr:largest_key automatically.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Any
|
The cache key. |
required |
value
|
Any
|
The value to store. |
required |
Raises:
| Type | Description |
|---|---|
KeyError
|
If |
Source code in catena/cache.py
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | |
clear()
Clears all cached entries and resets :attr:smallest_key and
:attr:largest_key, but preserves :attr:call_count and
:attr:read_count.
To reset statistics as well, use :meth:reset.
Source code in catena/cache.py
292 293 294 295 296 297 298 299 300 301 302 303 | |
copy()
Returns a shallow copy of the cache.
The new instance shares the same func, maxsize, and
prune_key, and carries over the current data, statistics
(:attr:call_count, :attr:read_count), and key-range sentinels
(:attr:smallest_key, :attr:largest_key).
Returns:
| Name | Type | Description |
|---|---|---|
BaseCache |
BaseCache
|
A new :class: |
Source code in catena/cache.py
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 | |
fromkeys(iterable, value=None)
classmethod
Not supported. Raises :exc:NotImplementedError.
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
Always. |
Source code in catena/cache.py
377 378 379 380 381 382 383 384 385 386 | |
get(key, default=None)
Returns the cached value for key, or default on a miss.
Unlike :meth:__getitem__, a missing key does not trigger a call
to func and is not stored. A hit increments :attr:read_count.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Any
|
The cache key to look up. |
required |
default
|
Any
|
Value to return when |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
The stored value, or |
Source code in catena/cache.py
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 | |
pop(key, *args)
Not supported. Raises :exc:NotImplementedError.
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
Always. |
Source code in catena/cache.py
367 368 369 370 371 372 373 374 375 | |
popitem()
Not supported. Raises :exc:NotImplementedError.
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
Always. |
Source code in catena/cache.py
398 399 400 401 402 403 404 405 406 | |
prune(n=2, order='asc')
Retains only the n largest-keyed entries, discarding the rest.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Number of entries to keep. Defaults to |
2
|
order
|
str
|
Whether to keep the entries with the
largest keys ( |
'asc'
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in catena/cache.py
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | |
reset()
Clears all cached entries and resets :attr:call_count,
:attr:read_count, :attr:smallest_key, and :attr:largest_key
to their initial values.
The bound func and maxsize are preserved.
Source code in catena/cache.py
280 281 282 283 284 285 286 287 288 289 290 | |
setdefault(key, default=None)
Not supported. Raises :exc:NotImplementedError.
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
Always. |
Source code in catena/cache.py
347 348 349 350 351 352 353 354 355 | |
update(*args, **kwargs)
Not supported. Raises :exc:NotImplementedError.
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
Always. |
Source code in catena/cache.py
357 358 359 360 361 362 363 364 365 | |