# crewai-cache-hook
[](https://badge.fury.io/py/crewai-cache-hook)
[](https://pypi.org/project/crewai-cache-hook/)
[](https://opensource.org/licenses/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](https://redis.io/download)
#### Starting Redis Server
- **MacOS**: `brew services start redis`
- **Ubuntu/Debian**: `sudo systemctl start redis`
- **Manual**: `redis-server`
## Installation
```bash
# Install via pip
pip install crewai-cache-hook
# Ensure Redis is installed and running
```
## Configuration
### Basic Usage
```python
from cache_hook import cache_hook
@task
@cache_hook(expire=600) # Cache for 10 minutes
def my_task(self):
...
```
### Advanced Configuration
```python
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
```python
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
```python
@task
@cache_hook(expire=600, force_refresh=True)
def always_fresh_task(self):
# This task will always execute and update cache
...
```
## Serialization Options
```python
# 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
Raw data
{
"_id": null,
"home_page": "https://github.com/chopperlee2011/crewai-cache-hook",
"name": "crewai-cache-hook",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.6",
"maintainer_email": null,
"keywords": "crewai redis cache decorator",
"author": "Chopper Lee",
"author_email": "lihengpro@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/c4/be/4eff05e7beeef6de56bb9fa062af33bf7011ff7316bdb169e847a9ee088b/crewai_cache_hook-0.1.1.tar.gz",
"platform": null,
"description": "# crewai-cache-hook\n\n[](https://badge.fury.io/py/crewai-cache-hook)\n[](https://pypi.org/project/crewai-cache-hook/)\n[](https://opensource.org/licenses/MIT)\n\nA Redis-based cache decorator for crewai tasks.\n\n## Project Status\n\n- **Version**: 0.1.1\n- **Status**: Beta\n- **Maintained**: Yes\n\n## Features\n- Add cache capability to tasks via a decorator\n- Checks Redis cache before task execution, returns cached result if hit\n- Automatically writes task result to Redis cache after execution\n- Supports custom cache key, expiration time, and Redis connection parameters\n- Robust error handling and fallback mechanisms\n- Multiple serialization options (pickle, JSON)\n\n## Prerequisites\n\n### Redis Dependency\nThis library requires Redis as a backend for caching. Make sure you have Redis installed and running.\n\n#### Installing Redis\n- **MacOS**: `brew install redis`\n- **Ubuntu/Debian**: `sudo apt-get install redis-server`\n- **Windows**: Download from [Redis Official Website](https://redis.io/download)\n\n#### Starting Redis Server\n- **MacOS**: `brew services start redis`\n- **Ubuntu/Debian**: `sudo systemctl start redis`\n- **Manual**: `redis-server`\n\n## Installation\n\n```bash\n# Install via pip\npip install crewai-cache-hook\n\n# Ensure Redis is installed and running\n```\n\n## Configuration\n\n### Basic Usage\n\n```python\nfrom cache_hook import cache_hook\n\n@task\n@cache_hook(expire=600) # Cache for 10 minutes\ndef my_task(self):\n ...\n```\n\n### Advanced Configuration\n\n```python\nfrom cache_hook import CacheHook\nimport logging\n\n# Configure logging\nlogging.basicConfig(level=logging.INFO)\n\n# Custom Redis configuration\ncustom_cache = CacheHook(\n host='redis.example.com', # Redis host\n port=6379, # Redis port\n db=1, # Redis database\n password='secret', # Optional password\n connect_timeout=5, # Connection timeout\n max_retries=2, # Connection retry attempts\n serializer='json' # Serialization method\n)\ncustom_cache_hook = custom_cache.cache_hook\n\n@task\n@custom_cache_hook(expire=600)\ndef my_task(self):\n ...\n```\n\n## Advanced Features\n\n### Custom Cache Key Generator\n\n```python\nfrom cache_hook import cache_hook\n\ndef custom_key_generator(func, args, kwargs):\n \"\"\"\n Create a custom cache key based on specific logic\n \n Args:\n func: The function being cached\n args: Positional arguments\n kwargs: Keyword arguments\n \n Returns:\n str: Custom cache key\n \"\"\"\n # Example: Include specific arguments in cache key\n key_parts = [\n func.__name__,\n str(kwargs.get('destination', '')),\n str(kwargs.get('start_date', ''))\n ]\n return ':'.join(key_parts)\n\n@task\n@cache_hook(expire=600, cache_key_func=custom_key_generator)\ndef travel_analysis_task(self, destination, start_date):\n ...\n```\n\n### Force Refresh\n\n```python\n@task\n@cache_hook(expire=600, force_refresh=True)\ndef always_fresh_task(self):\n # This task will always execute and update cache\n ...\n```\n\n## Serialization Options\n\n```python\n# Pickle serialization (default)\ncache_hook(serializer='pickle')\n\n# JSON serialization (for more compatibility)\ncache_hook(serializer='json')\n```\n\n## Parameters\n- `expire`: Cache expiration time (seconds)\n- `cache_key_func`: Custom function to generate cache key (optional)\n- `force_refresh`: Force task execution and cache update\n- `host`/`port`/`db`/`password`: Redis connection parameters\n- `serializer`: Serialization method ('pickle' or 'json')\n- `connect_timeout`: Connection timeout in seconds\n- `max_retries`: Number of connection retry attempts\n\n## Notes\n- Uses configurable serialization (pickle or JSON)\n- Ensure task results are serializable\n- Suitable for crewAI tasks\n- Caches the entire Task result\n- Requires an active Redis server\n\n## Troubleshooting\n- **Connection Errors**: Check Redis server status, network, and configuration\n- **Serialization Errors**: Ensure task results are serializable\n- **Performance Issues**: Monitor cache size and hit/miss rates\n",
"bugtrack_url": null,
"license": null,
"summary": "A Redis cache decorator for crewAI tasks",
"version": "0.1.1",
"project_urls": {
"Homepage": "https://github.com/chopperlee2011/crewai-cache-hook"
},
"split_keywords": [
"crewai",
"redis",
"cache",
"decorator"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "ee1f407e7b3a9bee5b984dd3f208ecb23ae4cd3f324145d37587a78225c0fbb3",
"md5": "c7e30dd50a3bbf43ee3ebd0df4079685",
"sha256": "3665789d4aa4010b0e8b857337043c876099bb6eab7d2e14ac8f6901286fd423"
},
"downloads": -1,
"filename": "crewai_cache_hook-0.1.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "c7e30dd50a3bbf43ee3ebd0df4079685",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.6",
"size": 6319,
"upload_time": "2025-08-07T03:28:05",
"upload_time_iso_8601": "2025-08-07T03:28:05.016897Z",
"url": "https://files.pythonhosted.org/packages/ee/1f/407e7b3a9bee5b984dd3f208ecb23ae4cd3f324145d37587a78225c0fbb3/crewai_cache_hook-0.1.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "c4be4eff05e7beeef6de56bb9fa062af33bf7011ff7316bdb169e847a9ee088b",
"md5": "6fd6582fcf0a58854644f096f883d003",
"sha256": "d740eb08e50a8427c0da98bceab4fa71a57640d76178805ed87402d27491b750"
},
"downloads": -1,
"filename": "crewai_cache_hook-0.1.1.tar.gz",
"has_sig": false,
"md5_digest": "6fd6582fcf0a58854644f096f883d003",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6",
"size": 7360,
"upload_time": "2025-08-07T03:28:05",
"upload_time_iso_8601": "2025-08-07T03:28:05.962987Z",
"url": "https://files.pythonhosted.org/packages/c4/be/4eff05e7beeef6de56bb9fa062af33bf7011ff7316bdb169e847a9ee088b/crewai_cache_hook-0.1.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-08-07 03:28:05",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "chopperlee2011",
"github_project": "crewai-cache-hook",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "crewai-cache-hook"
}