kv-cache


Namekv-cache JSON
Version 0.1.8 PyPI version JSON
download
home_pageNone
SummaryA fast key-value store using SQLite for CLI tools
upload_time2025-03-03 11:43:52
maintainerNone
docs_urlNone
authorNone
requires_python>=3.7
licenseNone
keywords cache cli key-value sqlite
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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

```bash
pip install kv-cache
```

## Quick Start

```python
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:

```python
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

```python
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

```bash
# 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

```bash
pytest tests/
```

### Code Style

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

```bash
# Format code
black src/ tests/

# Sort imports
isort src/ tests/
```

## License

MIT License - see LICENSE file for details.
            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "kv-cache",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": null,
    "keywords": "cache, cli, key-value, sqlite",
    "author": null,
    "author_email": "Your Name <your.email@example.com>",
    "download_url": "https://files.pythonhosted.org/packages/a9/5e/456e2794576afe49af1470ad1bdd03eb83f4ec7e1fc94950e115ae50276f/kv_cache-0.1.8.tar.gz",
    "platform": null,
    "description": "# KV_Cache\n\nA fast key-value store using SQLite as a backend, designed specifically for CLI tools needing persistent cache storage.\n\n## Features\n\n- \ud83d\ude80 Fast SQLite-based storage\n- \u23f0 Built-in TTL support\n- \ud83d\udd12 Thread-safe operations\n- \ud83e\uddf9 Automatic cleanup of expired entries\n\n## Installation\n\n```bash\npip install kv-cache\n```\n\n## Quick Start\n\n```python\nfrom kv_cache import KVStore\n\n# Initialize the store\nstore = KVStore(\"cache.db\")\n\n# Store a value with 1-hour TTL\nstore.set(\"my_key\", {\"data\": \"value\"}, ttl=3600)\n\n# Retrieve the value\nvalue = store.get(\"my_key\")\nprint(value)  # {'data': 'value'}\n\n# Delete when done\nstore.delete(\"my_key\")\n```\n\n## Usage with CLI Autocomplete\n\nPerfect for caching slow remote calls in CLI tools:\n\n```python\nimport time\nfrom kv_cache import KVStore, scache\n\n# directly use the store\ndef get_autocomplete_suggestions(prefix: str) -> list:\n    store = KVStore(\"~/.mycli/store.db\")\n    \n    # Try cache first\n    store_key = f\"auto:{prefix}\"\n    results = store.get(store_key)\n    \n    if results is None:\n        # Cache miss - fetch from remote\n        results = fetch_from_remote_server(prefix)  # Slow remote call\n        store.set(store_key, results, ttl=3600)  # Cache for 1 hour\n    \n    return results\n\n# or use the `scache` decorator to easily cache the function result\n@scache(ttl=3600, KVStore(\"~/.mycli/store.db\"))\ndef long_function_call(arg1, arg2, arg3=None):\n    time.sleep(1)\n\nlong_function_call(1, 2, arg3='test') # will take 1 seconds\nlong_function_call(1, 2, arg3='test') # instant\n\n```\n\n## API Reference\n\n### KVStore\n\n```python\nclass KVStore:\n    def __init__(self, db_path: str, table_name: str = \"key_value_store\"):\n        \"\"\"Initialize the store with database path and optional table name.\"\"\"\n        \n    def set(self, key: str, value: Any, ttl: Optional[int] = None):\n        \"\"\"Set a value with optional TTL in seconds.\"\"\"\n        \n    def get(self, key: str, default: Any = None) -> Any:\n        \"\"\"Get a value or return default if not found.\"\"\"\n        \n    def delete(self, key: str):\n        \"\"\"Delete a key from the store.\"\"\"\n        \n    def clear(self):\n        \"\"\"Clear all entries from the store.\"\"\"\n        \n    def close(self):\n        \"\"\"Close the database connection.\"\"\"\n\n    def __enter__(self):\n    def __exit__(self):\n        \"\"\" Context manager to use with `with` \"\"\"\n```\n\n## Development\n\n### Setup\n\n```bash\n# Clone the repository\ngit clone https://github.com/lcances/kv_cache.git\ncd fast-kv\n\n# Create virtual environment\npython -m venv venv\nsource venv/bin/activate  # or `venv\\Scripts\\activate` on Windows\n\n# Install development dependencies\npip install -e \".[dev]\"\n```\n\n### Running Tests\n\n```bash\npytest tests/\n```\n\n### Code Style\n\nThe project uses `black` for code formatting and `isort` for import sorting:\n\n```bash\n# Format code\nblack src/ tests/\n\n# Sort imports\nisort src/ tests/\n```\n\n## License\n\nMIT License - see LICENSE file for details.",
    "bugtrack_url": null,
    "license": null,
    "summary": "A fast key-value store using SQLite for CLI tools",
    "version": "0.1.8",
    "project_urls": {
        "Bug Tracker": "https://github.com/yourusername/fast-kv/issues",
        "Documentation": "https://github.com/yourusername/fast-kv#readme",
        "Homepage": "https://github.com/yourusername/fast-kv",
        "Repository": "https://github.com/yourusername/fast-kv.git"
    },
    "split_keywords": [
        "cache",
        " cli",
        " key-value",
        " sqlite"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "8b8c6e76cca79faf2a2b0213124917b209cf30e5a02613c63d84eb97bd0d8961",
                "md5": "34727480213b4ec3aa780a2edfdd965d",
                "sha256": "b57e5e1127a7599d82cf2294870802c415ea513ff75b2823c22e7ec1a054e5ee"
            },
            "downloads": -1,
            "filename": "kv_cache-0.1.8-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "34727480213b4ec3aa780a2edfdd965d",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 9212,
            "upload_time": "2025-03-03T11:43:51",
            "upload_time_iso_8601": "2025-03-03T11:43:51.265618Z",
            "url": "https://files.pythonhosted.org/packages/8b/8c/6e76cca79faf2a2b0213124917b209cf30e5a02613c63d84eb97bd0d8961/kv_cache-0.1.8-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "a95e456e2794576afe49af1470ad1bdd03eb83f4ec7e1fc94950e115ae50276f",
                "md5": "0b0bed9791b67e440246388e9e98c762",
                "sha256": "41eedad538e94d6ece61446b32f4ae784b9fe4eeb10b19c46fe8e0cffd14e7aa"
            },
            "downloads": -1,
            "filename": "kv_cache-0.1.8.tar.gz",
            "has_sig": false,
            "md5_digest": "0b0bed9791b67e440246388e9e98c762",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 14213,
            "upload_time": "2025-03-03T11:43:52",
            "upload_time_iso_8601": "2025-03-03T11:43:52.435033Z",
            "url": "https://files.pythonhosted.org/packages/a9/5e/456e2794576afe49af1470ad1bdd03eb83f4ec7e1fc94950e115ae50276f/kv_cache-0.1.8.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-03-03 11:43:52",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "yourusername",
    "github_project": "fast-kv",
    "github_not_found": true,
    "lcname": "kv-cache"
}
        
Elapsed time: 1.19520s