Skip to content

feat: clean functional API, pluggable serializers#8

Merged
agkloop merged 2 commits intomainfrom
upgrade1
Mar 10, 2026
Merged

feat: clean functional API, pluggable serializers#8
agkloop merged 2 commits intomainfrom
upgrade1

Conversation

@agkloop
Copy link
Owner

@agkloop agkloop commented Mar 10, 2026

API redesign

  • Replaced TTLCache/SWRCache/BGCache class-based API with two symbols: cache() and bg
  • @cache(ttl, key=, stale=, store=, metrics=) — TTL and SWR in one decorator
  • @bg(interval, key=, ...) — background refresh decoupled from request path
  • bg.write / bg.read — single-writer / multi-reader cross-process pattern
  • Smart key templates: static 'flags', positional 'user:{}', named 'user:{user_id}'
  • .invalidate(*args) and .clear() on every decorated function

Serializers

  • New serializers module: Serializer protocol, pack_entry/unpack_entry
  • Built-in: json (orjson), pickle, msgpack, protobuf(MyClass)
  • Pluggable to all byte-serializing backends (Redis, LocalFile, S3, GCS)
  • Custom serializer: implement dumps(v) -> bytes + loads(b) -> object

Storage backends updated

  • InMemCache: slots, lock-free hot path (GIL atomicity), simplified API
  • RedisCache, LocalFileCache, S3Cache, GCSCache: all use pack_entry/unpack_entry
  • ChainCache.build(*stores, ttls=[...]) — N-level read-through
  • InstrumentedStorage.get_entry forwards optional now= param

Performance

  • 23% throughput improvement (91ms -> 69ms per 100k calls)
  • TTL-only and SWR code paths split at decoration time (no runtime branch)
  • Single time.time() call per cache hit
  • Lock-free InMemCache reads; lock only on stale eviction
  • bg.read() local hit: ~9M ops/s

bg writer/reader bug fixes (5 bugs)

  • Reader job ID collision: unique IDs per bg.read() via _reader_counter
  • bg.write and bg.read both missing metrics= parameter — added
  • bg.read(store=None) now auto-discovers writer's store in same process
  • asyncio.Lock was lazily initialized inside coroutine — fixed to init at registration
  • bg.write refresh loop never called metrics.record_background_refresh() — fixed

…perf + docs

API redesign
- Replaced TTLCache/SWRCache/BGCache class-based API with two symbols: cache() and bg
- @cache(ttl, key=, stale=, store=, metrics=) — TTL and SWR in one decorator
- @bg(interval, key=, ...) — background refresh decoupled from request path
- bg.write / bg.read — single-writer / multi-reader cross-process pattern
- Smart key templates: static 'flags', positional 'user:{}', named 'user:{user_id}'
- .invalidate(*args) and .clear() on every decorated function

Serializers
- New serializers module: Serializer protocol, pack_entry/unpack_entry
- Built-in: json (orjson), pickle, msgpack, protobuf(MyClass)
- Pluggable to all byte-serializing backends (Redis, LocalFile, S3, GCS)
- Custom serializer: implement dumps(v) -> bytes + loads(b) -> object

Storage backends updated
- InMemCache: __slots__, lock-free hot path (GIL atomicity), simplified API
- RedisCache, LocalFileCache, S3Cache, GCSCache: all use pack_entry/unpack_entry
- ChainCache.build(*stores, ttls=[...]) — N-level read-through
- InstrumentedStorage.get_entry forwards optional now= param

Performance
- 23% throughput improvement (91ms -> 69ms per 100k calls)
- TTL-only and SWR code paths split at decoration time (no runtime branch)
- Single time.time() call per cache hit
- Lock-free InMemCache reads; lock only on stale eviction
- bg.read() local hit: ~9M ops/s

bg writer/reader bug fixes (5 bugs)
- Reader job ID collision: unique IDs per bg.read() via _reader_counter
- bg.write and bg.read both missing metrics= parameter — added
- bg.read(store=None) now auto-discovers writer's store in same process
- asyncio.Lock was lazily initialized inside coroutine — fixed to init at registration
- bg.write refresh loop never called metrics.record_background_refresh() — fixed

Docs and examples
- README.md: strong intro, full API reference with examples, single Mermaid diagram
- docs/guide.md: 1022-line unified guide with Mermaid diagrams for every concept
- Deleted 5 stale doc files using old API
- 4 new runnable examples: quickstart, writer_reader, serializers, metrics
- tests/benchmark.py and tests/profile_decorators.py rewritten for new API
- 101 tests passing
@agkloop agkloop changed the title feat: clean functional API, pluggable serializers, bg writer/reader, … feat: clean functional API, pluggable serializers Mar 10, 2026
@agkloop agkloop merged commit a479b83 into main Mar 10, 2026
9 checks passed
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