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.1.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.1-py3-none-any.whl (3.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: cachetiny-0.1.1.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.1.tar.gz
Algorithm Hash digest
SHA256 15528cb7a19d7c579c88d0073b04160de5790960fe0673fcb9ad9d378f0f9402
MD5 930cf057debcfa503a3ec8afeefea250
BLAKE2b-256 6292cd751ea25fb322947a069c08792f3ea17125baa00aa96e8b6ed1ab895cd8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: cachetiny-0.1.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6d43f962270bddf5a8eecd5ae498c54be652d03ceea544a330600132553c9573
MD5 c5b73fca25ec599fd162cef78c655876
BLAKE2b-256 c6c4a02ae84757c71e195b0a22ea150490142147e1a9a4dff57f4bbcd1f64ca2

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