Skip to main content

Atomic LRU

What is this?

This is a thread-safe and dependency-free in-memory LRU storage Python 3.10+ library with optional Time To Live (TTL).

You can define:

  • limits (max_items or size_limit_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.

Features

  • Thread-Safe
  • (optional) TTL expiration (globally or per item)
  • (optional) Total size limit (in bytes) 1
  • (optional) Max items limit
  • Automatic LRU eviction (when the limits are reached)
  • Full-typing support
  • High level Cache API with automatic serialization/deserialization 2
  • Low level Storage API without serialization/deserialization (store only references to given objects)

Quickstart

Installation

pip install atomic-lru (or equivalent for your package manager)

High level API (with automatic serialization/deserialization)

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. 2

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 dictionary here) in the cache with a custom TTL
cache.set(key="user:123", value={"name": "Alice", "age": 30}, ttl=60)

# ...

# Let's retrieve it
user = cache.get(key="user:123")

if user is not CACHE_MISS:
    # cache hit
    print(user["name"])

# Always close to stop the background expiration thread
cache.close()

Low level API (without serialization/deserialization)

But you can use it at a lower level to store any kind of data type without serialization. In that case, you will lose the size_limit_in_bytes feature but you still get the max_items feature.

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

# Always close to stop the background expiration thread
storage.close()

Full API reference

Refer to the API reference for the full API.

DEV

This library is managed with uv and a Makefile. Execute:

  • uv sync to create the virtual environment
  • make lint to lint the code (style, checks, types, architecture) and fix obvious things
  • make test to execute unit tests
  • make doc to generate the documentation

See https://docs.astral.sh/uv/getting-started/installation/ to install uv if you need to.

  1. When using the low level Storage API, values must be of type bytes for size tracking to work. The high level Cache API handles this automatically. ↩

  2. 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

Metadata

Release files for atomic-lru 0.0.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for atomic-lru 0.0.7
File Size Uploaded
atomic_lru-0.0.7.tar.gz 20.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for atomic-lru 0.0.7
File Interpreter ABI Platform
atomic_lru-0.0.7-py3-none-any.whl Python 3 none any Details

Total release size: 37.8 kB

Release files / atomic_lru-0.0.7.tar.gz

Download URL atomic_lru-0.0.7.tar.gz
Size 20.6 kB
Tags Source
SHA-256 checksum
How to use checksums
824e0edf1bfbbd20e5b9a7da738d166d0731b9e9c87f44b17d417a46280447f5
BLAKE2b-256 checksum
How to use checksums
ec79b766787531e02a4a8fcc39b8abdecfa80532d00271c5bde1678fc4e496d3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.10.8 {"installer":{"name":"uv","version":"0.10.8","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}

Release files / atomic_lru-0.0.7-py3-none-any.whl

Download URL atomic_lru-0.0.7-py3-none-any.whl
Size 17.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2f6f862700225b1fdec2bf7e11a0408dfba3f257f3f46f67eff7fb84efdda2ad
BLAKE2b-256 checksum
How to use checksums
be077434d02c2067f6a59760dcc6e8549e6966c395a8a52d54ffa9ee1f1980ed
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.10.8 {"installer":{"name":"uv","version":"0.10.8","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}

Release history Release notifications | RSS feed

This release

0.0.7 This release

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page