A lightweight persistent function result caching library with SQLite and Pickle support
Project description
CacheDB Lite
A lightweight persistent function result caching library for Python with SQLite and Pickle support.
Features
- 🚀 Easy to use: Simple decorator-based API
- 💾 Dual storage: SQLite database or Pickle files
- 🎯 Flexible key configuration: Specify which parameters to use as cache keys
- 📦 BLOB support: Automatic serialization for complex objects
- 🔑 Auto-increment ID: Internal primary key for better database design
- ⏰ Cache expiration: Set custom expiration time for cache entries
- 🔒 Secure: Parameterized queries for SQLite storage
- 🔄 Backward compatible: Existing code continues to work without changes
- 📦 Lightweight: No external dependencies
Installation
Using pip
pip install cachedb-lite
From source
git clone https://github.com/ciaranchen/cachelite.git
cd cachelite
pip install -e .
Quick Start
Basic Usage
from cachedb_lite import cache_result
@cache_result()
def expensive_function(a, b):
# Simulate a time-consuming operation
import time
time.sleep(2)
return a + b
# First call: executes the function and caches the result
result1 = expensive_function(1, 2) # Takes ~2 seconds
# Second call: returns cached result immediately
result2 = expensive_function(1, 2) # Takes ~0 seconds
Using SQLite Storage
@cache_result(
storage_type='sqlite',
cache_dir='./cache.db'
)
def expensive_function(a, b):
return a + b
Using Pickle Storage
@cache_result(
storage_type='pickle',
cache_dir='./cache'
)
def expensive_function(a, b):
return a + b
Specifying Cache Keys
# Use only specific parameters as cache keys
@cache_result(key_params=['a'])
def expensive_function(a, b):
return a + b
# With parameter types
@cache_result(key_params={'a': 'int', 'b': 'str'})
def expensive_function(a, b):
return f"{a}_{b}"
# With BLOB type (automatically serializes complex objects)
@cache_result(key_params={'data': 'blob'})
def process_data(data):
# data can be bytes or complex objects (dict, list, etc.)
return f"Processed {len(data)} bytes"
Cache Expiration
# Cache expires after 1 hour (3600 seconds)
@cache_result(expire_after=3600)
def expensive_function(a, b):
return a + b
Advanced Usage with CacheManager
For more advanced control, use the CacheManager class:
from cachedb_lite import CacheManager
# Initialize cache manager
cache_manager = CacheManager(db_path='./cache.db')
# Create a custom cache table
cache_manager.create_table(
table_name='user_data',
columns={'user_id': 'INTEGER', 'date': 'TEXT', 'data': 'BLOB'}
)
# Use the cache manager decorator
@cache_manager.cache_result(
table_name='user_data',
key_mapping={'user_id': 'user_id', 'date': 'date', 'data': 'data'},
expire_after=3600
)
def get_user_data(user_id, date, data):
# data will be automatically serialized if it's not bytes
return f"User {user_id} data for {date}"
Note: Tables created with CacheManager automatically include an internal _cachelite_id field as the primary key, and use special field names (_cachelite_value, _cachelite_created_at, _cachelite_expire_at) to avoid conflicts with your column names.
CacheManager API
Initialization
cache_manager = CacheManager(db_path='./cache.db')
Methods
create_table(table_name, columns): Create a new cache tablecache_result(table_name, key_mapping, expire_after=None, ignore_errors=False): Cache decoratordelete(table_name, key_values): Delete specific cache entryclear(table_name): Clear all cache entries in a tableclear_all(): Clear all cache entries in all tables
Configuration
Storage Types
sqlite: SQLite database storage (default)pickle: File-based storage using Pickle
Cache Key Types
int: Integerfloat: Floatstr: Stringbool: Booleandatetime: Date and timedate: Dateblob: Binary data (automatically serializes complex objects using Pickle)
BLOB Type Note: When using blob type, the parameter can be either:
bytes: Used directly without modification- Complex objects (dict, list, etc.): Automatically serialized using Pickle
Best Practices
- Use meaningful cache keys: Only include relevant parameters
- Set appropriate expiration times: Balance between freshness and performance
- Use SQLite for large datasets: Better performance for many cache entries
- Use Pickle for simplicity: Easy to inspect cache files
- Handle errors gracefully: Use
ignore_errors=Truein production - Use BLOB type for complex data: Automatically serializes dictionaries, lists, and other objects
- Avoid naming conflicts: Don't use column names starting with
_cachelite_(reserved for internal use)
Testing
Run tests using pytest:
pytest
License
MIT License
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Issues
If you encounter any issues, please report them on GitHub.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cachedb_lite-0.1.0.tar.gz.
File metadata
- Download URL: cachedb_lite-0.1.0.tar.gz
- Upload date:
- Size: 14.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
731575b851c194d8f34f6c1952be4853b7a2472122d4d601a5ec64c3654547ff
|
|
| MD5 |
81bff4f9c20c39b403eb3bd24b2e91fb
|
|
| BLAKE2b-256 |
4586c167d4c07fcf9963bd2fc0292e34e43557a429ab9eee8049566b6d85a02e
|
File details
Details for the file cachedb_lite-0.1.0-py3-none-any.whl.
File metadata
- Download URL: cachedb_lite-0.1.0-py3-none-any.whl
- Upload date:
- Size: 15.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
19c7b44a33abb9046fdfc0567afee171a811882f12298d2f0f8b54bce9e5f9aa
|
|
| MD5 |
8708e170c609abea46a5b0683d74184e
|
|
| BLAKE2b-256 |
f20dac1b1fd6a8e3ffc5ed314e9d00ffc1d2291169abc7cd0a6e48898d3a4cd4
|