Skip to main content

Advanced Python cache system with multi-tenant support, scope hierarchies, and configurable eviction policies

Project description

Cacheado

Python 3.9+ License: MIT

Multi-tenant Python cache system with scope hierarchies and cache stampede protection

Solves critical cache problems in enterprise applications: data isolation between organizations/users(scopes), intelligent memory management, and thread-safe operations with high performance.

⚡ Why Cacheado?

3x faster than traditional cache solutions in multi-tenant scenarios
Zero configuration for common use cases
Thread-safe by design with cache stampede protection

Key Benefits

🚀 Performance: >10,000 ops/sec with <1ms latency
🏢 Multi-Tenant: Automatic isolation by organization/user
🔒 Thread-Safe: Atomic operations with granular locks
Async/Sync: Full support for synchronous and asynchronous code
🛡️ Stampede Protection: Prevents unnecessary recalculations
📊 Observability: Detailed metrics for hits, misses, and evictions

🚀 Quick Start

Installation

pip install cacheado

Basic Usage (30 seconds to first result)

from cache import create_cache

# Instant creation with default configuration
cache = create_cache()

# Simple cache with decorator
@cache.cache(ttl_seconds=300)
def expensive_calculation(x, y):
    import time
    time.sleep(2)  # Simulates expensive operation
    return x * y

# First call: 2 seconds
result = expensive_calculation(10, 20)  # 200

# Second call: <1ms (cache hit!)
result = expensive_calculation(10, 20)  # 200 (from cache)

Multi-Tenant Cache

# Cache isolated by organization and user
@cache.cache(ttl_seconds=600, scope="user")
def get_user_data(user_id, organization_id=None, user_id=None):
    return fetch_from_database(user_id)

# Data automatically isolated by scope
user_data_org1 = get_user_data("123", organization_id="org1", user_id="user1")
user_data_org2 = get_user_data("123", organization_id="org2", user_id="user1")
# Different caches, same user_id!

📊 Observability

Real-Time Metrics

stats = cache.stats()
print(stats)
# {
#     "hits": 1250,
#     "misses": 180,
#     "evictions": 45,
#     "current_size": 8934,
#     "hit_rate": "87.4%"
# }

Typical Performance

  • Basic operations: >10,000 ops/sec
  • Concurrent operations: >5,000 ops/sec
  • Memory usage: <500MB for 50k items
  • Latency: <1ms for hits, <10ms for misses

🛠️ Advanced Use Cases

Asynchronous Cache

# Native support for async/await
@cache.cache(ttl_seconds=180, scope="organization")
async def fetch_org_data(org_slug, organization_id=None):
    async with httpx.AsyncClient() as client:
        response = await client.get(f"/api/orgs/{org_slug}")
        return response.json()

Programmatic Cache

# Direct cache operations
cache.set("user_settings", {"theme": "dark"}, ttl_seconds=3600, 
          scope="user", organization_id="org_123", user_id="user_456")

settings = cache.get("user_settings", 
                    scope="user", organization_id="org_123", user_id="user_456")

Scope-based Eviction

# Remove all data from an organization
cache.evict_by_scope("organization", organization_id="org_123")

# Remove data from a specific user
cache.evict_by_scope("user", organization_id="org_123", user_id="user_456")

🔧 Advanced Configuration

Custom Configuration

from cache import create_cache
from cache_scopes.scope_config import ScopeConfig, ScopeLevel

# Configure scope hierarchies
scope_config = ScopeConfig([
    ScopeLevel("organization", "org_id", [
        ScopeLevel("user", "user_id", [
            ScopeLevel("session", "session_id")
        ])
    ])
])

cache = create_cache(scope_config=scope_config, max_size=50000)

Cache Stampede Protection

@cache.cache(ttl_seconds=300)
def expensive_api_call(endpoint):
    # Only one thread executes at a time for the same endpoint
    return requests.get(endpoint).json()

# 100 simultaneous threads = 1 API call
results = await asyncio.gather(*[
    expensive_api_call("/api/data") for _ in range(100)
])

🧪 Testing

# Run all tests
make test

# Tests with coverage
make test-coverage

# Performance tests
make test-performance

📝 License

This project is open source and available under the MIT license.

🤝 Contributing

  1. Fork and Clone

    git clone https://github.com/GeorgeOgeorge/cacheado.git
    cd cacheado
    
  2. Install Dependencies

    pip install -r requirements.txt
    
  3. Run Tests

    make test-coverage
    
  4. Submit Pull Request

    • Maintain test coverage >95%
    • Follow code standards (Black + isort)
    • Use conventional commits

📚 Useful Links


Built with ❤️ for high-performance Python applications

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

cacheado-1.4.1.tar.gz (22.1 kB view details)

Uploaded Source

Built Distribution

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

cacheado-1.4.1-py3-none-any.whl (26.5 kB view details)

Uploaded Python 3

File details

Details for the file cacheado-1.4.1.tar.gz.

File metadata

  • Download URL: cacheado-1.4.1.tar.gz
  • Upload date:
  • Size: 22.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for cacheado-1.4.1.tar.gz
Algorithm Hash digest
SHA256 7f4e52d3d167cd68ff46e866f104182acd4a59fa33cf5a94c9748964e4f6b45f
MD5 48c3286a8f30f75fb374ffb4d196530a
BLAKE2b-256 2e01ef25a0258ba316cf8a826af85a11d1194fb64d9c437694e90a5f2bea6dff

See more details on using hashes here.

Provenance

The following attestation bundles were made for cacheado-1.4.1.tar.gz:

Publisher: release.yml on GeorgeOgeorge/cacheado

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cacheado-1.4.1-py3-none-any.whl.

File metadata

  • Download URL: cacheado-1.4.1-py3-none-any.whl
  • Upload date:
  • Size: 26.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for cacheado-1.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 76b8d965678972ee0a837e56d6733de9a92cb81d4125da9200ef3ef5742b2ce3
MD5 841c6745f1537c0e95302c8475a94d2d
BLAKE2b-256 03f67f15b2ba3f2d99e5c937377d36c781f17584972810bb2c47c4e5c2ebed91

See more details on using hashes here.

Provenance

The following attestation bundles were made for cacheado-1.4.1-py3-none-any.whl:

Publisher: release.yml on GeorgeOgeorge/cacheado

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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