Skip to main content

Redis-based caching library with intelligent dependency tracking for Python applications

Project description

simple-dep-cache

A Redis-based caching library with dependency tracking for Python applications.

Overview

Cache function results and automatically invalidate related caches when dependencies change. Uses Redis for distributed caching and supports both sync/async functions.

Installation

pip install simple-dep-cache

Quick Start

Basic Usage

from simple_dep_cache import cache_with_deps, add_dependency, CacheManager

# Initialize cache manager (optional - will be created automatically if not provided)
cache = CacheManager()

@cache_with_deps(cache_manager=cache, ttl=300)
def get_user_profile(user_id):
    # This function's result depends on user data
    add_dependency(f"user:{user_id}")

    # Expensive operation (e.g., database query, API call)
    return fetch_user_from_database(user_id)

@cache_with_deps(ttl=600)  # No cache_manager - will create one automatically
def get_user_posts(user_id):
    # This depends on both user and posts data
    add_dependency(f"user:{user_id}")
    add_dependency(f"posts:user:{user_id}")

    return fetch_user_posts_from_database(user_id)

# Use the cached functions
profile = get_user_profile("123")  # Cache miss - fetches from DB
profile = get_user_profile("123")  # Cache hit - returns cached result

posts = get_user_posts("123")     # Cache miss - fetches from DB
posts = get_user_posts("123")     # Cache hit - returns cached result

# When user data changes, invalidate the dependency
cache.invalidate_dependency("user:123")
# Now both get_user_profile("123") and get_user_posts("123") are invalidated!

profile = get_user_profile("123")  # Cache miss - will fetch fresh data

Async Support

from simple_dep_cache import async_cache_with_deps, add_dependency, AsyncCacheManager

cache = AsyncCacheManager()

@async_cache_with_deps(cache_manager=cache, ttl=300)
async def get_user_profile_async(user_id):
    add_dependency(f"user:{user_id}")
    return await fetch_user_from_database_async(user_id)

# Usage
profile = await get_user_profile_async("123")  # Cache miss
profile = await get_user_profile_async("123")  # Cache hit

# Invalidate dependency
await cache.invalidate_dependency("user:123")

Monitoring

from simple_dep_cache import StatsCollector, create_logger_callback

cache = CacheManager()
stats = StatsCollector()
cache.events.on_all(stats)
cache.events.on_all(create_logger_callback("my_cache"))

# Check statistics
print(stats.get_stats())  # hit_ratio, ops_per_second, etc.

Configuration

REDIS_URL=redis://localhost:6379/0    # Full Redis URL (preferred)
REDIS_HOST=localhost                  # Or individual settings
REDIS_PORT=6379
REDIS_PASSWORD=secret
DEP_CACHE_ENABLED=true                # Disable caching entirely

Manual Cache Operations

cache = CacheManager()

# Direct operations
cache.set("key", value, ttl=300, dependencies={"dep1"})
value = cache.get("key")
cache.delete("key")
cache.invalidate_dependency("dep1")  # Invalidates all dependent caches

API Reference

Decorators:

  • @cache_with_deps(cache_manager, ttl, key_prefix, dependencies)
  • @async_cache_with_deps(cache_manager, ttl, key_prefix, dependencies)

If cache_manager is not provided, one will be created automatically using configured environment variables or defaults.

Context:

  • add_dependency(dependency) - Track dependency in current function
  • current_cache_key() - Get current cache key

Managers:

  • CacheManager(redis_client, prefix) - Sync Redis cache manager
  • AsyncCacheManager(redis_client, prefix) - Async Redis cache manager

Monitoring:

  • StatsCollector() - Cache statistics

Requirements

  • Python 3.10+
  • Redis server
  • redis package

License

MIT

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

simple_dep_cache-0.1.1.tar.gz (40.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

simple_dep_cache-0.1.1-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: simple_dep_cache-0.1.1.tar.gz
  • Upload date:
  • Size: 40.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.8.17

File hashes

Hashes for simple_dep_cache-0.1.1.tar.gz
Algorithm Hash digest
SHA256 25c23caa9a8a39c68b1b5634d6114a52747786d5fc3a60397677554a7de74839
MD5 665b885a667b0fce20089c92e63a3f07
BLAKE2b-256 d9192df84390002c0fd9218d0be8de64b22ed240a0e9b653930787d69de23795

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for simple_dep_cache-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3ecac2a837c7ecfc1b39ec67726e8c94e2d63c4b22295b5eca93e753b53f6744
MD5 9628f974cbc3ffbf56f62af49179c06f
BLAKE2b-256 0d652bcc75bcf730140347f5a36bf9252773c3e4db4873816df5f6eab063973b

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