Skip to main content

Implementation of S3-FIFO cache algorithm. FIFO queues are all you need for cache evictions.

Project description

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

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

s3fifo-0.0.1.tar.gz (42.0 kB view details)

Uploaded Source

Built Distribution

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

s3fifo-0.0.1-py3-none-any.whl (13.2 kB view details)

Uploaded Python 3

File details

Details for the file s3fifo-0.0.1.tar.gz.

File metadata

  • Download URL: s3fifo-0.0.1.tar.gz
  • Upload date:
  • Size: 42.0 kB
  • Tags: Source
  • Uploaded using 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}

File hashes

Hashes for s3fifo-0.0.1.tar.gz
Algorithm Hash digest
SHA256 0616ef77d58e1b751ee8699e7955d2fab423a2088edd9be4af70d994b6ff7252
MD5 62165a3fab4521c4b80cb2bfbafd6dce
BLAKE2b-256 394a652857db8d7bc98b3706a95993b6d0fb707e48e86ba811e2b4097aa2361b

See more details on using hashes here.

File details

Details for the file s3fifo-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: s3fifo-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 13.2 kB
  • Tags: Python 3
  • Uploaded using 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}

File hashes

Hashes for s3fifo-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ab7d3d645c63c6ee72da982c8e84fd32c57b1604b67d44be12e242ba72c72e66
MD5 81dea64a1c9209e8b2ba16aac6c27110
BLAKE2b-256 2660a7743e12463bcab6a602cc5a60645d2c9b73862dc71fe5af1a8afba5fa3a

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