from time import sleep
from moka_py import Moka
# Create a cache with a capacity of 100 entries, with a TTL of 10.0 seconds
# and a TTI of 0.1 seconds. Entries are always removed after 10 seconds
# and are removed after 0.1 seconds if there are no `get`s happened for this time.
#
# Both TTL and TTI settings are optional. In the absence of an entry,
# the corresponding policy will not expire it.
# The default eviction policy is "tiny_lfu" which is optimal for most workloads,
# but you can choose "lru" as well.
cache: Moka[str, list[int]] = Moka(capacity=100, ttl=10.0, tti=0.1, policy="lru")
# Insert a value.
cache.set("key", [3, 2, 1])
# Retrieve the value.
assert cache.get("key") == [3, 2, 1]
# Wait for 0.1+ seconds, and the entry will be automatically evicted.
sleep(0.12)
assert cache.get("key") is None
Per-entry TTL / TTI
By default, TTL and TTI are set once for the entire cache. You can also set them
per entry by passing ttl and/or tti to set() or get_with():
from time import sleep
from moka_py import Moka
cache = Moka(100)
cache.set("short-lived", "value", ttl=0.5)
cache.set("session", {"user": "alice"}, ttl=3600.0)
cache.set("idle-sensitive", "value", tti=1.0)
cache.set("both", "value", ttl=60.0, tti=5.0)
# Entries without per-entry ttl/tti never expire (unless the cache has global settings).
cache.set("permanent", "value")
sleep(0.6)
assert cache.get("short-lived") is None # expired after 0.5s
assert cache.get("session") is not None # still alive
assert cache.get("permanent") is not None
get_with() accepts the same parameters:
from moka_py import Moka
cache = Moka(100)
value = cache.get_with("key", lambda: "computed", ttl=30.0)
Concurrent get_with with different TTL / TTI
get_with() guarantees that only one thread executes the initializer for a given key (stampede protection).
When multiple threads call get_with() for the same key concurrently with differentttl/tti values,
the thread that wins the race runs its initializer — and its ttl/tti values are stored with the entry.
All other threads receive the same cached value and their ttl/tti parameters are silently ignored.
import threading
from moka_py import Moka
cache = Moka(100)
# Thread A: get_with("k", compute, ttl=1.0)
# Thread B: get_with("k", compute, ttl=60.0)
#
# If thread A wins, the entry expires in 1 second.
# If thread B wins, the entry expires in 60 seconds.
# The loser's ttl is discarded — it is NOT merged or compared.
Interaction with cache-wide TTL / TTI
When the cache is constructed with global ttl or ttiand an entry specifies its own, the entry
expires at whichever deadline comes first.
WARNING
Per-entry TTL / TTI can only make an entry expire sooner than the cache-wide
policy, not later. This is a technical limitation of the underlying
Moka library: global and per-entry expiration
are evaluated independently, and the earliest deadline wins.
If you need entries with different lifetimes that can exceed a common default,
do not set global ttl/tti on the cache. Use per-entry values exclusively instead.
from moka_py import Moka
# Do this:
cache = Moka(1000)
cache.set("short", "v", ttl=60.0)
cache.set("long", "v", ttl=300.0) # works as expected
# NOT this — "long" will still expire in 60 s:
cache = Moka(1000, ttl=60.0)
cache.set("long", "v", ttl=300.0) # capped at 60 s by the global policy
from time import sleep
from moka_py import Moka
# Global TTL of 10 seconds.
cache = Moka(100, ttl=10.0)
# This entry will expire in 0.5 s (per-entry TTL wins, it is shorter).
cache.set("fast", "value", ttl=0.5)
# This entry keeps the global 10 s TTL (per-entry TTL=20 s is longer, so global wins).
cache.set("slow", "value", ttl=20.0)
sleep(0.6)
assert cache.get("fast") is None
assert cache.get("slow") is not None
As a decorator
moka-py can be used as a drop-in replacement for @lru_cache() with TTL + TTI support:
from time import sleep
from moka_py import cached
calls = []
@cached(maxsize=1024, ttl=5.0, tti=0.05)
def f(x, y):
calls.append((x, y))
return x + y
assert f(1, 2) == 3 # calls computations
assert f(1, 2) == 3 # gets from the cache
assert len(calls) == 1
sleep(0.06)
assert f(1, 2) == 3 # calls computations again (since TTI has passed)
assert len(calls) == 2
import asyncio
from time import perf_counter
from moka_py import cached
calls = []
@cached(maxsize=1024, ttl=5.0, tti=0.1)
async def f(x, y):
calls.append((x, y))
await asyncio.sleep(0.05)
return x + y
start = perf_counter()
assert asyncio.run(f(5, 6)) == 11
assert asyncio.run(f(5, 6)) == 11 # from cache
elapsed = perf_counter() - start
assert elapsed < 0.2
assert len(calls) == 1
Coalesce concurrent calls (wait_concurrent)
moka-py can synchronize threads on keys
import moka_py
from typing import Any
from time import sleep
import threading
from decimal import Decimal
calls = []
@moka_py.cached(ttl=5, wait_concurrent=True)
def get_user(id_: int) -> dict[str, Any]:
calls.append(id_)
sleep(0.02) # simulate an HTTP request (short for tests)
return {
"id": id_,
"first_name": "Jack",
"last_name": "Pot",
}
def process_request(path: str, user_id: int) -> None:
user = get_user(user_id)
...
def charge_money(from_user_id: int, amount: Decimal) -> None:
user = get_user(from_user_id)
...
if __name__ == "__main__":
request_processing = threading.Thread(target=process_request, args=("/user/info/123", 123))
money_charging = threading.Thread(target=charge_money, args=(123, Decimal("3.14")))
request_processing.start()
money_charging.start()
request_processing.join()
money_charging.join()
# Only one call occurred. Without `wait_concurrent`, each thread would issue its own HTTP request
# before the cache entry is set.
assert len(calls) == 1
Async wait_concurrent
When using wait_concurrent=True with async functions, moka-py creates a shared asyncio.Task per cache key. All
concurrent callers await the same task and receive the same result or exception. This eliminates duplicate in-flight
work for identical arguments.
Eviction listener
moka-py supports an eviction listener, called whenever a key is removed.
The listener must be a three-argument function (key, value, cause) and uses positional arguments only.
Possible reasons:
"expired": The entry's expiration timestamp has passed.
"explicit": The entry was manually removed by the user (.remove() is called).
"replaced": The entry itself was not actually removed, but its value was replaced by the user (.set() is
called for an existing entry).
"size": The entry was evicted due to size constraints.
from typing import Literal
from moka_py import Moka
from time import sleep
def key_evicted(k: str, v: list[int], cause: Literal["explicit", "size", "expired", "replaced"]):
events.append((k, v, cause))
events: list[tuple[str, list[int], str]] = []
moka: Moka[str, list[int]] = Moka(2, eviction_listener=key_evicted, ttl=0.5)
moka.set("hello", [1, 2, 3])
moka.set("hello", [3, 2, 1]) # replaced
moka.set("foo", [4])
moka.remove("foo") # explicit
for i in range(10):
moka.set(f"overflow-{i}", [i]) # over capacity, so most of these go away with "size"
# Maintenance is lazy, so ask for it explicitly instead of guessing when it happens
moka.run_pending_tasks()
sleep(1.0)
moka.run_pending_tasks() # whatever survived is past its TTL by now: "expired"
causes = {c for _, _, c in events}
assert causes == {"size", "expired", "replaced", "explicit"}, events
IMPORTANT NOTES
The listener is not called just-in-time. moka has no background threads or tasks; it runs only during cache operations.
The listener must not raise exceptions. If it does, the exception may surface from any moka-py method on any thread.
Keep the listener fast. Heavy work (especially I/O) will slow .get(), .set(), etc. Offload via ThreadPoolExecutor.submit() or asyncio.create_task()
Per-entry TTL / TTI and the eviction listener. Per-entry expiry fires the
listener with "expired" just like global TTL/TTI does. The notification is
delivered lazily during subsequent cache operations (e.g. get, set) after
the per-entry deadline passes — it is not instant.
Removing entries
Remove an entry with Moka.remove(key). It returns the previous value if present; otherwise None.
from moka_py import Moka
c = Moka(128)
c.set("hello", "world")
assert c.remove("hello") == "world"
assert c.get("hello") is None
If None is a valid cached value, distinguish it from absence using Moka.remove(key, default=...):
from moka_py import Moka
c = Moka(128)
c.set("hello", None)
assert c.remove("hello", default="WAS_NOT_SET") is None # None was set explicitly
# Now the entry "hello" does not exist, so `default` is returned
assert c.remove("hello", default="WAS_NOT_SET") == "WAS_NOT_SET"
Size-aware cache (weigher)
capacity bounds the total weight of all entries. Every entry weighs 1 by default —
that's why capacity normally reads as "maximum number of entries". Pass a weigher —
a callable (key, value) -> int — to weigh entries yourself, in any unit you like;
bytes are typical:
from moka_py import Moka
# A 100-character budget for string values
c = Moka(100, weigher=lambda k, v: len(v))
c.set("a", "x" * 40)
c.set("b", "y" * 40)
# Maintenance (evictions, expirations) runs lazily. Force it when you need
# accurate numbers — e.g. in tests or metrics:
c.run_pending_tasks()
assert c.weighted_size() == 80
assert c.count() == 2
The weigher is called once per insert, on the calling thread, and its result is fixed
for that entry (replacing a value re-weighs it). The contract:
The result must be a non-negative int; a negative result raises ValueError,
a non-int raises TypeError.
0 is allowed and means the entry spends no capacity budget.
Values above 2**32 - 1 are clamped to that maximum.
If the weigher raises, the insert fails with that exception and the cache is left
unchanged (for get_with, nothing is cached and all concurrent waiters see the error).
@cached accepts a weigher as well — maxsize bounds the total weight the same way.
The key passed to the weigher is an opaque hashable object built from the call arguments,
so weigh the value:
With wait_concurrent=True on an async function, a weigher changes what the cache
stores: finished results instead of in-flight asyncio.Tasks (a Task has no result to
weigh yet). Concurrent calls still share one computation; a failed computation is not
cached. One asymmetry to be aware of: if the weigher raises, the error goes to the call
that started the computation, while concurrent waiters still receive the computed value.
weighted_size() and run_pending_tasks() work without a weigher too: since every entry
weighs 1, weighted_size() equals count().
How it works
Moka stores Python object references
(by Py_INCREF) and does not serialize or deserialize values.
You can use any Python object as a value and any hashable object as a key (__hash__ is used).
Mutable objects remain mutable:
cachettl is an elegant LRU TTL cache decorator that also works with asyncio. It has the cache_info(), cache_clear() methods and access to the remainingttl property.