Skip to main content
Archived

This project has been archived by its maintainers, and is no longer receiving any updates.

crewai-cache-hook

PyPI version Python Versions License: MIT

A Redis-based cache decorator for crewai tasks.

Project Status

  • Version: 0.1.1
  • Status: Beta
  • Maintained: Yes

Features

  • Add cache capability to tasks via a decorator
  • Checks Redis cache before task execution, returns cached result if hit
  • Automatically writes task result to Redis cache after execution
  • Supports custom cache key, expiration time, and Redis connection parameters
  • Robust error handling and fallback mechanisms
  • Multiple serialization options (pickle, JSON)

Prerequisites

Redis Dependency

This library requires Redis as a backend for caching. Make sure you have Redis installed and running.

Installing Redis

  • MacOS: brew install redis
  • Ubuntu/Debian: sudo apt-get install redis-server
  • Windows: Download from Redis Official Website

Starting Redis Server

  • MacOS: brew services start redis
  • Ubuntu/Debian: sudo systemctl start redis
  • Manual: redis-server

Installation

# Install via pip
pip install crewai-cache-hook

# Ensure Redis is installed and running

Configuration

Basic Usage

from cache_hook import cache_hook

@task
@cache_hook(expire=600)  # Cache for 10 minutes
def my_task(self):
    ...

Advanced Configuration

from cache_hook import CacheHook
import logging

# Configure logging
logging.basicConfig(level=logging.INFO)

# Custom Redis configuration
custom_cache = CacheHook(
    host='redis.example.com',     # Redis host
    port=6379,                    # Redis port
    db=1,                         # Redis database
    password='secret',            # Optional password
    connect_timeout=5,            # Connection timeout
    max_retries=2,                # Connection retry attempts
    serializer='json'             # Serialization method
)
custom_cache_hook = custom_cache.cache_hook

@task
@custom_cache_hook(expire=600)
def my_task(self):
    ...

Advanced Features

Custom Cache Key Generator

from cache_hook import cache_hook

def custom_key_generator(func, args, kwargs):
    """
    Create a custom cache key based on specific logic
    
    Args:
        func: The function being cached
        args: Positional arguments
        kwargs: Keyword arguments
    
    Returns:
        str: Custom cache key
    """
    # Example: Include specific arguments in cache key
    key_parts = [
        func.__name__,
        str(kwargs.get('destination', '')),
        str(kwargs.get('start_date', ''))
    ]
    return ':'.join(key_parts)

@task
@cache_hook(expire=600, cache_key_func=custom_key_generator)
def travel_analysis_task(self, destination, start_date):
    ...

Force Refresh

@task
@cache_hook(expire=600, force_refresh=True)
def always_fresh_task(self):
    # This task will always execute and update cache
    ...

Serialization Options

# Pickle serialization (default)
cache_hook(serializer='pickle')

# JSON serialization (for more compatibility)
cache_hook(serializer='json')

Parameters

  • expire: Cache expiration time (seconds)
  • cache_key_func: Custom function to generate cache key (optional)
  • force_refresh: Force task execution and cache update
  • host/port/db/password: Redis connection parameters
  • serializer: Serialization method ('pickle' or 'json')
  • connect_timeout: Connection timeout in seconds
  • max_retries: Number of connection retry attempts

Notes

  • Uses configurable serialization (pickle or JSON)
  • Ensure task results are serializable
  • Suitable for crewAI tasks
  • Caches the entire Task result
  • Requires an active Redis server

Troubleshooting

  • Connection Errors: Check Redis server status, network, and configuration
  • Serialization Errors: Ensure task results are serializable
  • Performance Issues: Monitor cache size and hit/miss rates

Metadata

Release files for crewai-cache-hook 0.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 crewai-cache-hook 0.1.1
File Size Uploaded
crewai_cache_hook-0.1.1.tar.gz 7.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for crewai-cache-hook 0.1.1
File Interpreter ABI Platform
crewai_cache_hook-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 13.7 kB

Release files / crewai_cache_hook-0.1.1.tar.gz

Download URL crewai_cache_hook-0.1.1.tar.gz
Size 7.4 kB
Tags Source
SHA-256 checksum
How to use checksums
d740eb08e50a8427c0da98bceab4fa71a57640d76178805ed87402d27491b750
BLAKE2b-256 checksum
How to use checksums
c4be4eff05e7beeef6de56bb9fa062af33bf7011ff7316bdb169e847a9ee088b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.5

Release files / crewai_cache_hook-0.1.1-py3-none-any.whl

Download URL crewai_cache_hook-0.1.1-py3-none-any.whl
Size 6.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3665789d4aa4010b0e8b857337043c876099bb6eab7d2e14ac8f6901286fd423
BLAKE2b-256 checksum
How to use checksums
ee1f407e7b3a9bee5b984dd3f208ecb23ae4cd3f324145d37587a78225c0fbb3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.5

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

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