Skip to main content

Corto Cache

pip install corto-cache

Source: https://github.com/Bobowski/corto-cache

from corto import memoize

The index name is corto-cache (corto is taken). The import is corto.

Window-TinyLFU memoize for CPython 3.14.

Decorate a function. A hit returns the stored value. A miss runs the function and stores the result if it returns. New keys enter a small LRU window. They enter the main cache only when a frequency sketch says they are hotter than what would be evicted. A scan of unique keys dies in the window. The hot set stays.

from corto import memoize

@memoize(maxsize=1024)
def parse_accept(header: str) -> tuple[str, ...]:
    return tuple(part.strip() for part in header.split(","))

parse_accept.cache_info()    # hits, misses, maxsize, currsize
parse_accept.cache_clear()

cache_info and cache_parameters match functools.lru_cache. After a load run, hits / (hits + misses) tells you if the same keys came back. Near-zero hits: drop the decorator.

When to use this

Use Corto when the key universe is larger than maxsize and traffic also sends one-shot keys. lru_cache evicts whatever was least recent, so a scan can flush a popular entry. Corto keeps the frequent ones.

Use functools.lru_cache when the working set fits and stays. It is the stdlib, a bit cheaper on miss, and has maxsize=None.

Use neither when the function is cheaper than a lookup, or when every key is new. A miss still runs the function; Corto then pays extra to decide admission.

Cache mapping

When the value is not “return of this function” — insert and read in different places — use the same store as a bounded map:

from corto import Cache

cache = Cache(1024)
cache[header] = parsed
parsed = cache.get(header)

pop, setdefault, clear, in, len, hits, and misses are there. There is no public frequency peek.

Contract

  • Keys must be hashable. Equality follows ==.
  • maxsize is a positive int. No unbounded mode, no TTL.
  • Threads may share one wrapper. The lock is always on.
  • memoize drops the lock while the function runs. Two threads may compute the same key; the first store wins.
  • A key or value __del__ must leave that cache unchanged. The mutex is not recursive.
  • After os.fork, make a new cache in the child.
  • Fixed 1% admission window. No hill-climb. No doorkeeper.

Install

CPython 3.14. CI builds wheels for Linux x86_64, macOS (arm64, x86_64), and Windows AMD64. A source build needs a C compiler and Cython.

uv sync
uv run pytest

Numbers

On this machine (CPython 3.14, Apple Silicon), a one-arg hit is next to lru_cache (~70 ns). A Zipf decorator read is faster than Theine and cachebox, and a bit faster than lru_cache on that mix. Hit rate under a scan is the reason to pick Corto, not hit time.

Full tables and how to re-run: benchmarks/README.md.

TinyLFU: https://arxiv.org/pdf/1512.00727
Caffeine W-TinyLFU: https://github.com/ben-manes/caffeine/wiki/Efficiency

Metadata

Release files for corto-cache 0.1.0

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

Source distribution (sdist)

Source distribution for corto-cache 0.1.0
File Size Uploaded
corto_cache-0.1.0.tar.gz 167.1 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for corto-cache 0.1.0
File
corto_cache-0.1.0-cp314-cp314-win_amd64.whl CPython 3.14 CPython 3.14 Windows x86-64 Details
corto_cache-0.1.0-cp314-cp314-musllinux_1_2_x86_64.whl CPython 3.14 CPython 3.14 Linux musl 1.2+ x86-64 Details
corto_cache-0.1.0-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl CPython 3.14 CPython 3.14 Linux glibc 2.17+ x86-64, Linux glibc 2.28+ x86-64 Details
corto_cache-0.1.0-cp314-cp314-macosx_11_0_arm64.whl CPython 3.14 CPython 3.14 macOS 11.0+ ARM64 Details
corto_cache-0.1.0-cp314-cp314-macosx_10_15_x86_64.whl CPython 3.14 CPython 3.14 macOS 10.15+ x86-64 Details

Total release size: 1.4 MB

Release files / corto_cache-0.1.0.tar.gz

Download URL corto_cache-0.1.0.tar.gz
Size 167.1 kB
Tags Source
SHA-256 checksum
How to use checksums
ba7f4299b9f79c7d25e7daefddcf87c0a6822a329cf19a6fb59601f60ad3d41c
BLAKE2b-256 checksum
How to use checksums
2acd201f1d3d33385defc34d58a9d35fb71ffed769861061b8bcac647da57fcc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 21, 2026.

Transparency log

Release files / corto_cache-0.1.0-cp314-cp314-win_amd64.whl

Download URL corto_cache-0.1.0-cp314-cp314-win_amd64.whl
Size 65.1 kB
Tags CPython 3.14 Windows x86-64
SHA-256 checksum
How to use checksums
92055c7cfc739f77ee3bb12bafc0015b8501116ce1e39a792a60068619b79793
BLAKE2b-256 checksum
How to use checksums
414283d4736f1e0b797b0a4fef4273135f06f0de51bd3cf45a16dc32579c62aa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 21, 2026.

Transparency log

Release files / corto_cache-0.1.0-cp314-cp314-musllinux_1_2_x86_64.whl

Download URL corto_cache-0.1.0-cp314-cp314-musllinux_1_2_x86_64.whl
Size 491.1 kB
Tags CPython 3.14 Linux musl 1.2+ x86-64
SHA-256 checksum
How to use checksums
fc81d44fed577a94f4557747c2051e9682cacbf5318acf8ae11a9988f59ccf3b
BLAKE2b-256 checksum
How to use checksums
b2f8c2c9b58b8c08aca1423a9ed5ad372555cfcdeb3e89d90873bf247f505a28
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 21, 2026.

Transparency log

Release files / corto_cache-0.1.0-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl

Download URL corto_cache-0.1.0-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Size 507.7 kB
Tags CPython 3.14 Linux glibc 2.17+ x86-64 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
56143b0a001ba5f97b7321f36b6f959806ae80943b8ed72ca093417b430015f1
BLAKE2b-256 checksum
How to use checksums
568782b7fcb94fe1fde266848042452fd5981ac6a57c5305e74a9bd19a2d8c85
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 21, 2026.

Transparency log

Release files / corto_cache-0.1.0-cp314-cp314-macosx_11_0_arm64.whl

Download URL corto_cache-0.1.0-cp314-cp314-macosx_11_0_arm64.whl
Size 78.4 kB
Tags CPython 3.14 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
54bf987751d551370bb3543cfec093f50034e88118182b6eefa0a1c1d6ddeb90
BLAKE2b-256 checksum
How to use checksums
2a99d33c0e9d8189800097102bd0b3ce4749e731ff77d2f835cbddaa0fc750b5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 21, 2026.

Transparency log

Release files / corto_cache-0.1.0-cp314-cp314-macosx_10_15_x86_64.whl

Download URL corto_cache-0.1.0-cp314-cp314-macosx_10_15_x86_64.whl
Size 76.7 kB
Tags CPython 3.14 macOS 10.15+ x86-64
SHA-256 checksum
How to use checksums
1930b37e2ae6ecab78d671892025729bd544ec3e4fa491329f887214d9e3645e
BLAKE2b-256 checksum
How to use checksums
f80f170422c8c8ad25847bf9a78ca088ea6dddeea0088d080ae83cf53bd50aa4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 21, 2026.

Transparency log

Release history Release notifications | RSS feed

0.1.1

8 release files

This release

0.1.0 This release

6 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