Skip to main content

s3fifo

Release Build status codecov License

Python implementation of the S3-FIFO cache algorithm.

It provides:

  • S3FIFOCache: low-level cache container.
  • s3fifo_cache: sync decorator with an API similar to functools.lru_cache.
  • as3fifo_cache: async decorator inspired by async-lru.

Installation

pip install s3fifo

Quick Start

Sync decorator

from s3fifo import s3fifo_cache


@s3fifo_cache(maxsize=128)
def load_user(user_id: int) -> dict:
    return {"id": user_id}


load_user(1)
load_user(1)
print(load_user.cache_info())
# CacheInfo(hits=1, misses=1, maxsize=128, currsize=1)

Async decorator

import asyncio

from s3fifo import as3fifo_cache


@as3fifo_cache(maxsize=128)
async def fetch_user(user_id: int) -> dict:
    await asyncio.sleep(0.01)
    return {"id": user_id}


async def main() -> None:
    await asyncio.gather(fetch_user(1), fetch_user(1), fetch_user(1))
    print(fetch_user.cache_info())
    # CacheInfo(hits=2, misses=1, maxsize=128, currsize=1)

    await fetch_user.cache_close()


asyncio.run(main())

as3fifo_cache deduplicates concurrent calls for the same key and shares one in-flight task.

Configuration

The S3-FIFO policy parameters are configurable across all entry points and default to the paper/reference values:

  • small_size_ratio=0.10
  • ghost_size_ratio=0.90
  • move_to_main_threshold=2

Example:

from s3fifo import s3fifo_cache


@s3fifo_cache(
    maxsize=256,
    small_size_ratio=0.20,
    ghost_size_ratio=0.50,
    move_to_main_threshold=3,
)
def f(x: int) -> int:
    return x

You can inspect active settings via cache_parameters().

API Summary

S3FIFOCache

S3FIFOCache(
    maxsize: int,
    *,
    small_size_ratio: float = 0.10,
    ghost_size_ratio: float = 0.90,
    move_to_main_threshold: int = 2,
)

Methods:

  • get(key, default=None)
  • put(key, value)
  • remove(key, include_ghost=True)
  • clear()
  • len(cache)
  • key in cache

s3fifo_cache

Usage:

  • @s3fifo_cache
  • @s3fifo_cache(maxsize=128, typed=False, ...)

Methods on decorated function:

  • cache_info()
  • cache_clear()
  • cache_parameters()

Notes:

  • maxsize=0 disables storage and still counts misses.
  • typed=True includes argument types in cache keys.

as3fifo_cache

Usage:

  • @as3fifo_cache
  • @as3fifo_cache(maxsize=128, typed=False, ...)

Methods on decorated function:

  • cache_info()
  • cache_clear()
  • cache_invalidate(*args, **kwargs)
  • cache_parameters()
  • await cache_close(wait=False)

Limitations

  • as3fifo_cache has event-loop affinity: a single cache instance must be used on one event loop.
  • TTL and jitter policies are not implemented.

Differences from Origin

  • Capacity is measured differently. The original implementation is byte-size based, while this package is entry-count based. In practice, maxsize means “maximum number of cached keys”, not memory bytes.

  • The programming model is Python-first. The original design is request/object-size oriented; here you work with a plain Python cache object (S3FIFOCache) and decorators (s3fifo_cache, as3fifo_cache).

  • Overwriting an existing key is treated as an update. Calling put with an existing key updates the value in place and does not trigger an extra eviction cycle.

Development

make install
make check
make test

License

Apache-2.0

Metadata

Release files for s3fifo 0.0.1

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

Source distribution (sdist)

Source distribution for s3fifo 0.0.1
File Size Uploaded Provenance
s3fifo-0.0.1.tar.gz 42.0 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for s3fifo 0.0.1
File Interpreter ABI Platform
s3fifo-0.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 55.1 kB

Release files / s3fifo-0.0.1.tar.gz

Download URL s3fifo-0.0.1.tar.gz
Size 42.0 kB
Tags Source
SHA-256 checksum
How to use checksums
0616ef77d58e1b751ee8699e7955d2fab423a2088edd9be4af70d994b6ff7252
BLAKE2b-256 checksum
How to use checksums
394a652857db8d7bc98b3706a95993b6d0fb707e48e86ba811e2b4097aa2361b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.28 {"installer":{"name":"uv","version":"0.9.28","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 / s3fifo-0.0.1-py3-none-any.whl

Download URL s3fifo-0.0.1-py3-none-any.whl
Size 13.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ab7d3d645c63c6ee72da982c8e84fd32c57b1604b67d44be12e242ba72c72e66
BLAKE2b-256 checksum
How to use checksums
2660a7743e12463bcab6a602cc5a60645d2c9b73862dc71fe5af1a8afba5fa3a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.28 {"installer":{"name":"uv","version":"0.9.28","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}

Security for s3fifo 0.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 release files No provenance

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