Skip to main content

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 table
  • cache_result(table_name, key_mapping, expire_after=None, ignore_errors=False): Cache decorator
  • delete(table_name, key_values): Delete specific cache entry
  • clear(table_name): Clear all cache entries in a table
  • clear_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: Integer
  • float: Float
  • str: String
  • bool: Boolean
  • datetime: Date and time
  • date: Date
  • blob: 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

  1. Use meaningful cache keys: Only include relevant parameters
  2. Set appropriate expiration times: Balance between freshness and performance
  3. Use SQLite for large datasets: Better performance for many cache entries
  4. Use Pickle for simplicity: Easy to inspect cache files
  5. Handle errors gracefully: Use ignore_errors=True in production
  6. Use BLOB type for complex data: Automatically serializes dictionaries, lists, and other objects
  7. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cachedb_lite-0.1.0.tar.gz (14.2 kB view details)

Uploaded Source

Built Distribution

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

cachedb_lite-0.1.0-py3-none-any.whl (15.3 kB view details)

Uploaded Python 3

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

Hashes for cachedb_lite-0.1.0.tar.gz
Algorithm Hash digest
SHA256 731575b851c194d8f34f6c1952be4853b7a2472122d4d601a5ec64c3654547ff
MD5 81bff4f9c20c39b403eb3bd24b2e91fb
BLAKE2b-256 4586c167d4c07fcf9963bd2fc0292e34e43557a429ab9eee8049566b6d85a02e

See more details on using hashes here.

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

Hashes for cachedb_lite-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 19c7b44a33abb9046fdfc0567afee171a811882f12298d2f0f8b54bce9e5f9aa
MD5 8708e170c609abea46a5b0683d74184e
BLAKE2b-256 f20dac1b1fd6a8e3ffc5ed314e9d00ffc1d2291169abc7cd0a6e48898d3a4cd4

See more details on using hashes here.

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