Skip to main content

Thread-safe, dependency-free in-memory LRU storage library for Python 3.10+ (with optional TTL)

Project description

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

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 dictionnary 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"])

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 loose the max-size-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

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]: This feature is only available when using the high level Cache API. [^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.

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

atomic_lru-0.0.6.tar.gz (18.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

atomic_lru-0.0.6-py3-none-any.whl (17.0 kB view details)

Uploaded Python 3

File details

Details for the file atomic_lru-0.0.6.tar.gz.

File metadata

  • Download URL: atomic_lru-0.0.6.tar.gz
  • Upload date:
  • Size: 18.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.27 {"installer":{"name":"uv","version":"0.9.27","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

Hashes for atomic_lru-0.0.6.tar.gz
Algorithm Hash digest
SHA256 c6623a1e0e49576af9dface7700f90020131bb90701a795004ee21486f160f75
MD5 b35be64b9c2068c7d6d907c7b2945e22
BLAKE2b-256 a847a94b1e16dc0aae7b7cc9cdf35664b9e7906fc404d9c26febedfec4d9ca04

See more details on using hashes here.

File details

Details for the file atomic_lru-0.0.6-py3-none-any.whl.

File metadata

  • Download URL: atomic_lru-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 17.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.27 {"installer":{"name":"uv","version":"0.9.27","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

Hashes for atomic_lru-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 c635394c0c98503effcf05568163dc05f629e1ff05937e3a47f9244043af748f
MD5 47f1378cc2f421fb559f3d1753f2fdf1
BLAKE2b-256 d1d4d02311c8cc0c9f735dc70f352a9b47251d1a06d40277363078d4a21be12e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page