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

Uploaded Python 3

File details

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

File metadata

  • Download URL: cachetiny-0.1.0.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.0.tar.gz
Algorithm Hash digest
SHA256 00973625534b1c084fef1a2a674eefd667adb2130fd3f88ff95a3f85e23688fa
MD5 fc58ecba83dc5f2e275ae136ba659f65
BLAKE2b-256 5bdf624f501be61670359ca09bdb3fea91a9d0db7c6b2a5310e18690cee00550

See more details on using hashes here.

File details

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

File metadata

  • Download URL: cachetiny-0.1.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4aa45c6e4a178cf86a551f462e829411ffeef4291412fc2bfbf1e169e28d3d11
MD5 db71d8938530c9870b41eacf53ff1a6d
BLAKE2b-256 f45819038f2dcf8379317a377e34435f6ad0399c7fac79aec979e7e420f5c033

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