Thread-safe, dependency-free in-memory LRU storage library for Python 3.12+ (with optional TTL)
Project description
Atomic LRU
What is this?
This is a thread-safe and dependency-free in-memory LRU storage Python 3.12+ library with optional Time To Live (TTL).
You can define:
- limits (
max-itemsormax-size-in-bytes) - TTL expiration (globally or per item)
to prevent the storage from growing too big.
You will get an automatic LRU eviction of the least recently used items when the limits are reached.
High level API
The main use-case is to use it as a cache for your data. You store any kind of data type which will be automatically serialized to bytes. [^1]
from atomic_lru import CACHE_MISS, Cache
# Create a Cache object instance (with a size limit of 1MB)
# (this object is thread-safe, so you can use it from multiple threads)
cache = Cache(size_limit_in_bytes=1_000_000, default_ttl=3600)
# Let's store something (a dictionnary here) in the cache with a custom TTL
cache.set("user:123", {"name": "Alice", "age": 30}, ttl=60)
# ...
# Let's retrieve it
user = cache.get("user:123")
if user is not CACHE_MISS:
# cache hit
print(user["name"])
Low level API
But you can use it at a lower level to store any kind of data type without serialization. In that case, you will loose the max-size-in-bytes feature. See below for an example.
Features
- Thread-Safe
- (optional) TTL expiration (globally or per item)
- (optional) Total size limit (in bytes) [^2]
- (optional) Max items limit
- Automatic LRU eviction (when the limits are reached)
- Full-typing support
- High level
CacheAPI with automatic serialization/deserialization [^1] - Low level
StorageAPI without serialization/deserialization (store only references to given objects)
Low level API example
from atomic_lru import CACHE_MISS, Storage
class ExpensiveObject:
"""An expensive object that is not serializable."""
pass
# Create a Storage object instance to store ExpensiveObject instances
# (this object is thread-safe, so you can use it from multiple threads)
storage = Storage[ExpensiveObject](max_items=100, default_ttl=3600)
# Create and store an ExpensiveObject instance
value = ExpensiveObject()
storage.set("key1", value, ttl=60)
# ...
# Let's retrieve it
obj = storage.get("key1")
if obj is not CACHE_MISS:
# cache hit
assert isinstance(obj, ExpensiveObject)
assert id(obj) == id(value) # this is the same object instance
[^1]: By default, pickle is used for serialization/deserialization but you can provide your own serializer/deserializer if you want to use a different format.
[^2]: This feature is only available when using the high level Cache API.
DEV
This library is managed with uv and a Makefile. Execute:
uv syncto create the virtual environmentmake lintto lint the code (style, checks, types, architecture) and fix obvious thingsmake testto execute unit testsmake docto generate the documentation
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file atomic_lru-0.0.3.tar.gz.
File metadata
- Download URL: atomic_lru-0.0.3.tar.gz
- Upload date:
- Size: 17.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fbcf46c28c382e4bd5991d3148bc0d0931e417bcef7f14c2aa36cfbd6ed715e0
|
|
| MD5 |
455be2fdd8f4df2578a111ba4a0839c2
|
|
| BLAKE2b-256 |
87d2e2a33ccd31a96a8f091d33430afbab7b8718004365e87b50d85823875df5
|
File details
Details for the file atomic_lru-0.0.3-py3-none-any.whl.
File metadata
- Download URL: atomic_lru-0.0.3-py3-none-any.whl
- Upload date:
- Size: 16.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
75cc1dcc54254a9d13e5ea1e8b05c0a67bafaab06c6a334559f0152d7b60cfab
|
|
| MD5 |
e2ed494aabbd2aed51a8e88f18391697
|
|
| BLAKE2b-256 |
2243e075cbd5de32c91968c4236b52bfd554f636d15897b93f33e0657005c18a
|