Docs / Guides / Cache manager

Platform 06 · Study guide

Cache manager

Store temporary values in memory, on disk, or Redis — and clear them from the CLI.

What you will learn
  • How to construct a Cache with a store
  • put / get / forget / remember patterns
  • When to use memory vs file vs Redis

1. Setup

from tpy.cache import Cache, MemoryStore, FileStore

# Process memory (fast, lost on restart)
cache = Cache(MemoryStore())

# Shared file cache for local multi-process demos
cache = Cache(FileStore("storage/framework/cache"))

cache.put("user:1", {"name": "Asha"}, ttl=60)
user = cache.get("user:1")
cache.forget("user:1")
$ tpy cache clear

2. Redis

$ pip install "tamilPY[redis]"
from tpy.cache import Cache, RedisStore
cache = Cache(RedisStore(url="redis://localhost:6379/0"))

3. How it works

  1. Cache is a small facade over a CacheStore.
  2. put writes a value with optional TTL seconds.
  3. get returns the value or a default if missing/expired.
  4. CLI tpy cache clear flushes the configured/default stores used by the project.
StoreUse when
MemorySingle process, tests, tiny TTL data
FileLocal persistence without Redis
RedisMulti-server production cache