Quick and small memcached client for Python
86
Design a small service that uses a memcached client to keep integer counters with atomic updates. All mutations must rely on the client's atomic counter operations rather than manual read/modify/write sequences.
get_count returns the current value or a provided default when missing. @test@generates
class CounterService:
def __init__(self, client, default_ttl: int = 0):
"""client is a configured pylibmc client; default_ttl applies when a counter is first created."""
def increment(self, key: str, delta: int = 1) -> int:
"""Atomically increases a counter by delta, initializing missing counters at zero before the operation. Returns the new count."""
def decrement(self, key: str, delta: int = 1) -> int:
"""Atomically decreases a counter by delta without going below zero. Returns the new count."""
def bulk_bump(self, deltas: dict[str, int]) -> dict[str, int]:
"""Atomically adjusts multiple counters by given deltas (positive to increment, negative to decrement). Returns updated counts keyed by counter name."""
def get_count(self, key: str, default: int = 0) -> int:
"""Fetches the current counter value, returning the provided default when the counter does not exist."""Memcached client used to persist counters with atomic increment/decrement support.
Install with Tessl CLI
npx tessl i tessl/pypi-pylibmcevals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10