Cachy

0.3.0 · maintenance · verified Tue Apr 14

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

Install

Imports

Quickstart

This quickstart demonstrates how to configure and use Cachy with a file-based store. It shows how to put, retrieve, and check for items in the cache. An example for a Redis store is also included (commented out), highlighting the need for external service configuration.

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}")

view raw JSON →