Cache Core API¶
Core cache classes and helper functions providing synchronous and asynchronous APIs, a singleton-aware manager, and context helpers.
Factory Functions¶
get_cache¶
get_cache_manager¶
get_cache_manager
¶
get_cache_manager(config: CacheManagerConfig | None = None) -> CacheManager
Return the singleton CacheManager, optionally reconfiguring with a new config. Reconfiguration is applied if a non-None config is passed.
Source code in src/jinpy_utils/cache/core.py
Core Classes¶
CacheManager¶
CacheManager
¶
Singleton-aware cache manager with async/sync APIs.
Responsibilities: - Initialize and manage multiple cache backends (memory, file, redis, etc.) - Provide a consistent sync and async API delegating to selected backend - Enforce configuration via Pydantic models - Follow SOLID principles (separation of concerns, single responsibility) - 12-Factor: environment-driven config handled by the Pydantic config layer
Usage
manager = CacheManager() # uses defaults (in-memory) manager.set("key", {"a": 1}, ttl=60) value = manager.get("key")
Async¶
await manager.aset("k", "v", ttl=30) v = await manager.aget("k")
Source code in src/jinpy_utils/cache/core.py
18 19 20 21 22 23 24 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 | |
Functions¶
__init__
¶
__init__(config: CacheManagerConfig | None = None) -> None
Source code in src/jinpy_utils/cache/core.py
get_backend
¶
get_backend(name: str | None = None) -> BaseBackend
Retrieve a backend by name, or the default if name is None.
Source code in src/jinpy_utils/cache/core.py
get
¶
set
¶
delete
¶
exists
¶
clear
¶
get_many
¶
set_many
¶
delete_many
¶
incr
¶
decr
¶
ttl
¶
touch
¶
is_healthy
¶
close
¶
Close either a specific backend or all backends.
Source code in src/jinpy_utils/cache/core.py
aget
async
¶
aset
async
¶
adelete
async
¶
aexists
async
¶
aclear
async
¶
aget_many
async
¶
aset_many
async
¶
adelete_many
async
¶
aincr
async
¶
adecr
async
¶
attl
async
¶
atouch
async
¶
ais_healthy
async
¶
aclose
async
¶
Close either a specific backend or all backends asynchronously.
Note: Individual backends may only support sync close; this method will best-effort call async close if present, or fallback to sync close.
Source code in src/jinpy_utils/cache/core.py
using
¶
using(backend: str | None = None) -> Generator[CacheClient, None, None]
Context manager yielding a CacheClient bound to a selected backend.
Example
with manager.using("memory") as cache: cache.set("k", "v") v = cache.get("k")
Source code in src/jinpy_utils/cache/core.py
ausing
async
¶
ausing(backend: str | None = None) -> AsyncGenerator[AsyncCacheClient, None]
Async context manager yielding an AsyncCacheClient bound to a selected backend.
Example
async with manager.ausing("redis") as cache: await cache.aset("k", "v") v = await cache.aget("k")
Source code in src/jinpy_utils/cache/core.py
Cache¶
Cache
¶
High-level cache facade API.
This class delegates to a process-wide CacheManager instance under the hood and provides both sync and async methods for common operations. It is a thin wrapper that preserves the backend selection and promotes simple usage.
Usage (sync): cache = Cache() # uses default manager (singleton) cache.set("k", {"v": 1}, ttl=60) value = cache.get("k")
Usage (async): cache = Cache() await cache.aset("k", "v", ttl=30) v = await cache.aget("k")
Source code in src/jinpy_utils/cache/core.py
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 | |
Functions¶
__init__
¶
__init__(
backend: str | None = None, manager: CacheManager | None = None
) -> None
get
¶
set
¶
delete
¶
exists
¶
clear
¶
get_many
¶
set_many
¶
delete_many
¶
incr
¶
decr
¶
ttl
¶
touch
¶
is_healthy
¶
close
¶
aget
async
¶
aset
async
¶
adelete
async
¶
aexists
async
¶
aclear
async
¶
aget_many
async
¶
aset_many
async
¶
adelete_many
async
¶
aincr
async
¶
adecr
async
¶
attl
async
¶
atouch
async
¶
ais_healthy
async
¶
CacheClient¶
CacheClient
¶
Thin synchronous facade over CacheManager bound to a specific backend.
Source code in src/jinpy_utils/cache/core.py
AsyncCacheClient¶
AsyncCacheClient
¶
Thin asynchronous facade over CacheManager bound to a specific backend.
Source code in src/jinpy_utils/cache/core.py
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 | |
Interfaces¶
CacheInterface¶
CacheInterface
¶
Bases: ABC
Abstract cache interface supporting sync operations.
Source code in src/jinpy_utils/cache/interfaces.py
AsyncCacheInterface¶
AsyncCacheInterface
¶
Bases: ABC
Abstract cache interface supporting async operations.
Source code in src/jinpy_utils/cache/interfaces.py
Utilities¶
normalize_key¶
normalize_key
¶
Normalize cache key to a safe form.
Source code in src/jinpy_utils/cache/utils.py
compute_expiry¶
compute_expiry
¶
Compute absolute expiry timestamp in seconds.
remaining_ttl¶
remaining_ttl
¶
Compute remaining ttl from absolute expiry.
default_serializer¶
default_serializer
¶
default_serializer(
kind: SerializerType,
) -> tuple[Callable[[Any], bytes], Callable[[bytes], Any]]
Return serializer, deserializer functions based on kind.
Source code in src/jinpy_utils/cache/utils.py
Examples¶
Basic Usage (in-memory)¶
from jinpy_utils.cache import get_cache
cache = get_cache() # default in-memory backend
cache.set("user:123", {"name": "Ada"}, ttl=60)
print(cache.get("user:123")) # {"name": "Ada"}
Async Usage¶
import asyncio
from jinpy_utils.cache import get_cache
async def main():
cache = get_cache()
await cache.aset("token", "abc", ttl=10)
value = await cache.aget("token")
print(value)
asyncio.run(main())
Using a context client bound to a backend¶
from jinpy_utils.cache import CacheManager, MemoryCacheConfig
manager = CacheManager()
# Ensure a named memory backend exists (defaults will be auto-provisioned)
with manager.using("default_memory") as c:
c.set("k", 1)
assert c.incr("k") == 2
Reconfiguring the manager with multiple backends¶
from pathlib import Path
from jinpy_utils.cache import (
CacheManager, CacheManagerConfig,
MemoryCacheConfig, FileCacheConfig
)
config = CacheManagerConfig(
default_backend="mem",
backends=[
MemoryCacheConfig(name="mem", default_ttl=120),
FileCacheConfig(name="disk", directory=Path(".cache"))
]
)
manager = CacheManager(config) # initializes both
# Use the file backend via high-level facade
from jinpy_utils.cache import Cache
file_cache = Cache(backend="disk")
file_cache.set("report", {"ok": True})