Skip to main content

A clipboard synchronization tool that work on every platform

Project description

KV_Cache

A fast key-value store using SQLite as a backend, designed specifically for CLI tools needing persistent cache storage.

Features

  • 🚀 Fast SQLite-based storage
  • ⏰ Built-in TTL support
  • 🔒 Thread-safe operations
  • 🧹 Automatic cleanup of expired entries

Installation

pip install kv-cache

Quick Start

from kv_cache import KVStore

# Initialize the store
store = KVStore("cache.db")

# Store a value with 1-hour TTL
store.set("my_key", {"data": "value"}, ttl=3600)

# Retrieve the value
value = store.get("my_key")
print(value)  # {'data': 'value'}

# Delete when done
store.delete("my_key")

Usage with CLI Autocomplete

Perfect for caching slow remote calls in CLI tools:

import time
from kv_cache import KVStore, scache

# directly use the store
def get_autocomplete_suggestions(prefix: str) -> list:
    store = KVStore("~/.mycli/store.db")
    
    # Try cache first
    store_key = f"auto:{prefix}"
    results = store.get(store_key)
    
    if results is None:
        # Cache miss - fetch from remote
        results = fetch_from_remote_server(prefix)  # Slow remote call
        store.set(store_key, results, ttl=3600)  # Cache for 1 hour
    
    return results

# or use the `scache` decorator to easily cache the function result
@scache(ttl=3600, KVStore("~/.mycli/store.db"))
def long_function_call(arg1, arg2, arg3=None):
    time.sleep(1)

long_function_call(1, 2, arg3='test') # will take 1 seconds
long_function_call(1, 2, arg3='test') # instant

API Reference

KVStore

class KVStore:
    def __init__(self, db_path: str, table_name: str = "key_value_store"):
        """Initialize the store with database path and optional table name."""
        
    def set(self, key: str, value: Any, ttl: Optional[int] = None):
        """Set a value with optional TTL in seconds."""
        
    def get(self, key: str, default: Any = None) -> Any:
        """Get a value or return default if not found."""
        
    def delete(self, key: str):
        """Delete a key from the store."""
        
    def clear(self):
        """Clear all entries from the store."""
        
    def close(self):
        """Close the database connection."""

    def __enter__(self):
    def __exit__(self):
        """ Context manager to use with `with` """

Development

Setup

# Clone the repository
git clone https://github.com/lcances/kv_cache.git
cd fast-kv

# Create virtual environment
python -m venv venv
source venv/bin/activate  # or `venv\Scripts\activate` on Windows

# Install development dependencies
pip install -e ".[dev]"

Running Tests

pytest tests/

Code Style

The project uses black for code formatting and isort for import sorting:

# Format code
black src/ tests/

# Sort imports
isort src/ tests/

License

MIT License - see LICENSE file for details.

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

clipthread-0.0.1.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

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

clipthread-0.0.1-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file clipthread-0.0.1.tar.gz.

File metadata

  • Download URL: clipthread-0.0.1.tar.gz
  • Upload date:
  • Size: 8.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for clipthread-0.0.1.tar.gz
Algorithm Hash digest
SHA256 4ac2094787cf8157999eb93bc0c571f6c588865c1759089b1d1364faf3ed34c9
MD5 9009726f58f6728b9cae6b9fba5977a9
BLAKE2b-256 d8909d784f10e0d7dbc1a5b56489e868ce2c87b1293893faec92d6fef3eea26b

See more details on using hashes here.

Provenance

The following attestation bundles were made for clipthread-0.0.1.tar.gz:

Publisher: publish-pypi.yml on lcances/clipthread

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file clipthread-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: clipthread-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 7.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for clipthread-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b2c837dfda0d7bf5206c8bcfeb7005b6336c4bd80b789cf551ad213c5eb2e432
MD5 fa9405d01f02014561583f6a1aa0c951
BLAKE2b-256 6b717a36a9bd80f2c5ef442184f459f93bdbdd80b9e694df1294822c6f2c3ce2

See more details on using hashes here.

Provenance

The following attestation bundles were made for clipthread-0.0.1-py3-none-any.whl:

Publisher: publish-pypi.yml on lcances/clipthread

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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