A thread-safe, time-versioned key-value store for Python. Every write is kept, not just overwritten, so you can ask "what was this key at time T" as well as "what is it now."
from chronomap import ChronoMap
cm = ChronoMap()
cm["config"] = {"debug": True}
cm["config"] = {"debug": False}
cm.history("config")
# [(1720699200.1, {'debug': True}), (1720699241.7, {'debug': False})]
cm.get("config", timestamp="2024-07-11T10:00:00")
# the value that was visible at that momentMost in-memory stores (dicts, Redis) only remember the current value. Once you overwrite a key, the old value is gone. ChronoMap keeps every version, indexed by timestamp, so debugging "why did this config change" or "what did this look like an hour ago" doesn't require a separate audit log bolted on afterward.
It's not trying to replace a real database. It's an in-memory structure for the case where you want dict-like ergonomics plus history: config stores, session state, feature flags, small time-series, that kind of thing.
pip install chronomap
# with pandas export support
pip install chronomap[pandas]Requires Python 3.8+. No required runtime dependencies.
from chronomap import ChronoMap
cm = ChronoMap()
cm.put("cpu", 0.42, timestamp=100)
cm.put("cpu", 0.91, timestamp=200)
cm.put("cpu", 0.17, ttl=3600)
cm.get("cpu") # current value
cm.get("cpu", timestamp=150) # 0.42, the value as of timestamp 150
cm.history("cpu") # every stored (timestamp, value) pair
cm.get_range("cpu", 100, 200) # versions in an inclusive time rangeTimestamps can be Unix numbers, datetime objects, or ISO-8601 strings.
Expired keys are hidden from reads immediately and are removed lazily on
access (or by the optional cleanup thread).
Batch TTL operations take a single key iterable and one lock acquisition:
cm.put_many({"a": 1, "b": 2})
cm.expire_many(["a", "b"], ttl=60)
cm.ttl_remaining_many(["a", "b"]) # seconds remaining per key
cm.persist_many(["a", "b"]) # remove the deadlinesFor crash recovery, point the map at a write-ahead journal:
cm = ChronoMap(wal_path="chronomap.wal")
with cm:
cm.put("region", "us-east")
cm.put("retries", 3)
recovered = ChronoMap(wal_path="chronomap.wal")
assert recovered.get("retries") == 3Records are flushed and fsynced before the in-memory mutation by default
(wal_sync=True). Journals compact into an atomic checkpoint automatically
at 64 MiB, or whenever you call cm.checkpoint(). Recovery streams records
through a consumer instead of buffering the whole journal. AsyncChronoMap
accepts the same options; use async with or await cm.aclose() to close its
journal.
docs/API.md- method-by-method reference for the synchronous map, async map, exceptions, and CLI.docs/ARCHITECTURE.md- storage layout, locking, caching, TTL cleanup, snapshots, and the async design.docs/COOKBOOK.md- practical recipes for config, sessions, caches, snapshots, merges, and persistence.docs/TESTING.md- local test commands, markers, coverage, and CI expectations.CHANGELOG.md- what changed and why.
- Time travel -
get(key, timestamp=...)returns the value as of any point in history, with binary search over each key's sorted version list. - Snapshots and rollback -
snapshot()/rollback(), orwith cm.snapshot_context(): ...for automatic rollback when a block raises. - TTL -
put(key, value, ttl=3600); inspect withttl_remaining(), refresh withexpire(), remove withpersist(), and sweep withclean_expired_keys()or the background cleanup thread. The_manyvariants apply the same operations to multiple keys. - Batch operations -
put_many(),get_many(),pop_many(),delete_many(),expire_many(),persist_many(), andttl_remaining_many()operate under a single lock acquisition. - Queries -
query(),aggregate(), andcount()take ordinary Python callables instead of a query language. - Change hooks -
on_change()for global notifications andsubscribe(key, callback)for one key. - Auto-pruning - cap history length per key with
max_history, or trim on demand withprune_history()/prune_all_history(). - Diff and merge - compare two maps with
diff()/diff_detailed()and combine histories withmerge(strategy=...). - Durability - opt into an append-only, CRC-checked write-ahead journal
with crash recovery, CRC-checked atomic checkpoints, and configurable fsync.
Both
ChronoMapandAsyncChronoMapsupport WAL mode. - Memory guardrails -
max_memory_mbadds an approximate ceiling and warning to both map implementations;memory_usage()reports the estimate. - Persistence - checkpoint atomically with
save_json(),save_pickle(),to_dict(), and the matching loaders. - Version export - stream every live version with
iter_rows(), or build a pandas DataFrame withto_dataframe(). - Async -
AsyncChronoMapis a separate asyncio-native implementation for use inside event loops, including TTL refresh and batch operations. - CLI inspection -
python -m chronomap.cli show state.jsonprints a saved map's current state; thettl,expire, andpersistcommands inspect or update expiry metadata in place.
ChronoMap is safe to share between threads in one process. It uses a
reader-writer lock by default, with an RLock fallback if you set
use_rwlock=False. It does not share state across processes. A WAL-backed map
takes an OS-backed advisory lock on <wal_path>.lock, so opening the same
journal from a second live process raises JournalLockError; the lock is
released when the owner exits, including after a crash.
AsyncChronoMap uses an asyncio.Lock and should stay on one event loop.
Do not share a synchronous map and an async map as if they were the same
object.
- Not a database. The optional WAL survives process crashes and torn tail
writes for one writer, but the map is still in-memory, has no transactions
or replication, and does not share state across processes. The WAL lock only
prevents a second live writer; it is not distributed coordination. Snapshot
helpers (
save_json(),save_pickle()) are separate from the journal. JSON saves coerce keys to strings; use pickle when key types matter. - Not process-safe. Thread-safe within one process; for multiprocessing,
use separate instances and your own IPC. A WAL path accepts only one live
owner and rejects a second one with
JournalLockError. - Not built for large-scale analytics.
query(),aggregate(), andcount()are O(n) scans. They are intended for hundreds to low thousands of keys, not warehouse-sized datasets.
The project has 800+ tests and 100% line coverage across the package.
CI runs the suite on Python 3.8 through 3.14 on Linux, plus Windows and macOS,
and checks formatting with black. Reproduce the coverage locally with:
pytest tests/ --cov=chronomapIssues and PRs pointing out rough edges are welcome.
See CONTRIBUTING.md and
docs/TESTING.md. Short version: fork, branch, write a
test for the change, run the suite, and open a PR.
MIT - see LICENSE.