Skip to main content

A tiny decorator-based TTL cache for Python

Project description

cachetiny

A tiny, dependency‑free, in‑memory caching decorator for Python with TTL support and optional logging.

cachetiny is designed to be extremely small, easy to understand, and useful for scripts, CLIs, APIs, and lightweight applications that need fast caching without external services or disk storage.


Features

• Decorator-based API
• In-memory cache (no files written to disk)
• Configurable TTL (time-to-live)
• Optional logging for cache hits vs computations
• Thread-safe implementation
• Zero dependencies
• Extremely small and fast


Quick Example

import time
from cachetiny import cachetiny


@cachetiny(ttl=5, log=True)
def slow_add(a, b):
    print("Doing expensive work...")
    time.sleep(2)
    return a + b


print(slow_add(1, 2))
print(slow_add(1, 2))
print(slow_add(1, 2))

time.sleep(6)

print(slow_add(1, 2))

Example output:

[cachetiny] COMPUTING -> slow_add(1, 2){}
Doing expensive work...
3

[cachetiny] USING CACHE -> slow_add(1, 2){}
3

[cachetiny] USING CACHE -> slow_add(1, 2){}
3

[cachetiny] COMPUTING -> slow_add(1, 2){}
Doing expensive work...
3

API

Decorator

@cachetiny(ttl=60, log=False)

Parameters

Parameter Type Description


ttl int Time in seconds before cache expires log bool Print when retrieving cache or computing value


Logging

When logging is enabled:

@cachetiny(ttl=30, log=True)

You will see messages indicating whether the value was retrieved from cache or computed again.

Example:

[cachetiny] USING CACHE -> fetch_data(123){}
[cachetiny] COMPUTING -> fetch_data(456){}

Important Behavior

Cache is stored in memory

cachetiny does NOT write anything to disk.

This means:

✔ Extremely fast lookups
✔ No file management
✔ No external dependencies

But also:

⚠ Cache is cleared when the Python process exits.


Thread Safety

cachetiny uses a thread lock internally to protect the cache dictionary.

This ensures safe behavior when multiple threads access cached functions.


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

cachetiny-0.1.2.tar.gz (3.1 kB view details)

Uploaded Source

Built Distribution

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

cachetiny-0.1.2-py3-none-any.whl (3.0 kB view details)

Uploaded Python 3

File details

Details for the file cachetiny-0.1.2.tar.gz.

File metadata

  • Download URL: cachetiny-0.1.2.tar.gz
  • Upload date:
  • Size: 3.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.0

File hashes

Hashes for cachetiny-0.1.2.tar.gz
Algorithm Hash digest
SHA256 40b62691e507bfa7ebec20ef2afa8b155c90c17639d95cef96da9f931c954bef
MD5 64590694ae616215f67c35caed94a01e
BLAKE2b-256 217ea6836fdc9bccb0ba538159f64b458d3e97cdf0c13f97634c60e6f2253da0

See more details on using hashes here.

File details

Details for the file cachetiny-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: cachetiny-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 3.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.0

File hashes

Hashes for cachetiny-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 60ae91d142579af71a54ab3694f83b8fc8dbac36c49c60ec7b3f81ebe1c3cc3b
MD5 6f4525968711d7124310949f5243322e
BLAKE2b-256 1ac707192a18a7c77535591fcebeb0f33066978f0f54e36cfa12b9186befae2f

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