Skip to main content

async-cache-ext

A high-performance async caching solution for Python with extended features

A lightweight, efficient caching solution designed specifically for asyncio applications. Supports both LRU (Least Recently Used) and TTL (Time To Live) caching strategies with a clean, decorator-based API.

Features

  • 🚀 Async-First: Built specifically for asyncio applications
  • 🔄 Multiple Cache Types:
    • LRU (Least Recently Used) cache
    • TTL (Time To Live) cache
  • 🎯 Flexible Key Generation: Works with primitive types, custom objects, and ORM models
  • 🛠 Configurable: Adjustable cache size and TTL duration
  • 🧹 Cache Management: Clear cache on demand
  • 💡 Smart Argument Handling: Skip specific arguments in cache key generation
  • 🔍 Cache Bypass: Ability to bypass cache for specific calls

Installation

pip install async-cache-ext

Basic Usage

LRU Cache

The LRU cache maintains a fixed number of items, removing the least recently used item when the cache is full.

from cache import AsyncLRU

@AsyncLRU(maxsize=128)
async def get_user_data(user_id: int) -> dict:
    # Expensive database operation
    data = await db.fetch_user(user_id)
    return data

TTL Cache

The TTL cache automatically expires entries after a specified time period.

from cache import AsyncTTL

@AsyncTTL(time_to_live=60, maxsize=1024)
async def get_weather(city: str) -> dict:
    # External API call
    weather = await weather_api.get_data(city)
    return weather

Advanced Usage

Working with Custom Objects

The cache works seamlessly with custom objects and ORM models:

from dataclasses import dataclass
from cache import AsyncLRU

@dataclass
class UserFilter:
    age: int
    country: str
    status: str

@AsyncLRU(maxsize=128)
async def filter_users(filter_params: UserFilter) -> list:
    # Complex filtering operation
    users = await db.filter_users(
        age=filter_params.age,
        country=filter_params.country,
        status=filter_params.status
    )
    return users

Skipping Arguments

Useful for methods where certain arguments shouldn't affect the cache key:

from cache import AsyncTTL

class UserService:
    @AsyncTTL(time_to_live=300, maxsize=1000, skip_args=1)
    async def get_user_preferences(self, user_id: int) -> dict:
        # 'self' is skipped in cache key generation
        return await self.db.get_preferences(user_id)

Cache Management

Bypassing Cache

# Normal cached call
result = await get_user_data(123)

# Force fresh data
fresh_result = await get_user_data(123, use_cache=False)

Clearing Cache

# Clear the entire cache
get_user_data.cache_clear()

Performance Considerations

  • The LRU cache is ideal for frequently accessed data with no expiration requirements
  • The TTL cache is perfect for data that becomes stale after a certain period
  • Choose maxsize based on your memory constraints and data access patterns
  • Consider using skip_args when caching class methods to avoid instance-specific caching

Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Metadata

Release files for async-cache-ext 1.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for async-cache-ext 1.1.1
File Size Uploaded Provenance
async_cache_ext-1.1.1.tar.gz 8.9 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for async-cache-ext 1.1.1
File Interpreter ABI Platform
async_cache_ext-1.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 19.6 kB

Release files / async_cache_ext-1.1.1.tar.gz

Download URL async_cache_ext-1.1.1.tar.gz
Size 8.9 kB
Tags Source
SHA-256 checksum
How to use checksums
8d1038ba375c9383842d569aa4d66af5aa3dc6042c03659497b94b60712bc52c
BLAKE2b-256 checksum
How to use checksums
fd3c470350183b91d9fdaa518a63f3cd7d241c1bc9c8f7db0d6fd27faef7c69e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

Release files / async_cache_ext-1.1.1-py3-none-any.whl

Download URL async_cache_ext-1.1.1-py3-none-any.whl
Size 10.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
87ecd85684ec2d1a8cc56a9e2a0681c20c18680f418f9e45a1240617be88d49f
BLAKE2b-256 checksum
How to use checksums
d46b701fbb8fe5223a75a912a16b974ac42f0f9a2fd3faffd34ec9b1386fa0f9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

Security for async-cache-ext 1.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.1.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page