Cachy
Cachy provides a simple yet effective caching library for Python. It offers a powerful and thread-safe API with decorator syntax and supports various backend stores including memcached, Redis, database, file system, and in-memory dictionaries. The current stable version is 0.3.0, released in 2019.
Warnings
- breaking Cachy explicitly excludes Python versions 3.0 through 3.3. Attempting to use the library with these interpreter versions will result in an error due to the `requires_python` metadata.
- gotcha The library's last release was in August 2019 (v0.3.0). This indicates it is not under active development and may not receive updates for newer Python versions, security patches, or compatibility with future library ecosystems.
- gotcha When using external cache stores (e.g., Redis, Memcached), Cachy relies on separate client libraries (e.g., `redis`, `python-memcached`). These must be installed manually and configured correctly in Cachy's global configuration. Misconfiguration or missing dependencies will lead to runtime errors or silently failed caching operations.
- gotcha Cachy uses a global configuration (`Cache.configure`). If your application needs different cache configurations for various parts or threads, managing this global state can become complex and lead to unexpected interactions. Ensure careful handling of configuration updates in multi-threaded or multi-component applications.
Install
-
pip install cachy
Imports
- Cache
from cachy import Cache
Quickstart
from cachy import Cache
import os
# Configure a file-based cache store
Cache.configure({
'stores': {
'file': {
'driver': 'file',
'path': os.path.join(os.getcwd(), 'cache_data'),
},
'redis': {
'driver': 'redis',
'host': os.environ.get('REDIS_HOST', 'localhost'),
'password': os.environ.get('REDIS_PASSWORD'),
'port': int(os.environ.get('REDIS_PORT', 6379)),
'database': int(os.environ.get('REDIS_DB', 0)),
}
},
'default': 'file',
})
# Get the default cache store (file)
cache = Cache.store()
# Put an item in the cache for 10 minutes (600 seconds)
cache.put('my_key', 'my_value', 600)
# Retrieve an item from the cache
value = cache.get('my_key')
print(f"Retrieved value: {value}")
# Check if an item exists
exists = cache.has('my_key')
print(f"Key exists: {exists}")
# Get a specific store (e.g., Redis)
# Ensure Redis is running and configured correctly via env vars or direct config.
# try:
# redis_cache = Cache.store('redis')
# redis_cache.put('redis_key', 'redis_value', 60)
# print(f"Retrieved from Redis: {redis_cache.get('redis_key')}")
# except Exception as e:
# print(f"Could not connect to Redis: {e}")