Skip to main content

collections-cache 🚀

collections-cache is a fast and scalable key–value caching solution built on top of SQLite. It allows you to store, update, and retrieve data using unique keys, and it supports complex Python data types (thanks to pickle). Designed to harness the power of multiple CPU cores, the library shards data across multiple SQLite databases, enabling impressive performance scaling.


Features ✨

  • Multiple SQLite Databases: Distributes your data across several databases to optimize I/O and take advantage of multi-core systems.
  • Key–Value Store: Simple and intuitive interface for storing and retrieving data.
  • Supports Complex Data Types: Serialize and store lists, dictionaries, objects, and more using pickle.
  • Parallel Processing: Uses Python’s multiprocessing and concurrent.futures modules to perform operations in parallel.
  • Efficient Data Retrieval: Caches all keys in memory for super-fast lookups.
  • Cross-Platform: Runs on Linux, macOS, and Windows.
  • Performance Scaling: Benchmarks show near-linear scaling with the number of real CPU cores.

Installation 📦

Use Poetry to install and manage dependencies:

  1. Clone the repository:

    git clone https://github.com/Luiz-Trindade/collections_cache.git
    cd collections-cache
    
  2. Install the package with Poetry:

    poetry install
    

Usage ⚙️

Simply import and start using the main class, Collection_Cache, to interact with your collection:

Basic Example

from collections_cache import Collection_Cache

# Create a new collection named "STORE"
cache = Collection_Cache("STORE")

# Set a key-value pair
cache.set_key("products", ["apple", "orange", "onion"])

# Retrieve the value by key
products = cache.get_key("products")
print(products)  # Output: ['apple', 'orange', 'onion']

Bulk Insertion Example

For faster insertions, accumulate your data and use set_multi_keys:

from collections_cache import Collection_Cache
from random import uniform, randint
from time import time

cache = Collection_Cache("web_cache")
insertions = 100_000
data = {}

# Generate data
for i in range(insertions):
    key = str(uniform(0.0, 100.0))
    value = "some text :)" * randint(1, 100)
    data[key] = value

# Bulk insert keys using multi-threaded execution
cache.set_multi_keys(data)

print(f"Inserted {len(cache.keys())} keys successfully!")

Performance Benchmark 📊

After optimizing SQLite settings (including setting synchronous = OFF), the library has shown a significant performance improvement. The insertion performance has been accelerated dramatically, allowing for much faster data insertions and better scalability.

Benchmark Results

For 100,000 insertions:

  • Previous performance: ~797 insertions per second.
  • Optimized performance: ~6,657 insertions per second after disabling SQLite's synchronization (synchronous = OFF), reducing the total insertion time from 125 seconds to 15.02 seconds.

Performance Scaling

With the optimized configuration, the library scales nearly linearly with the number of CPU cores. For example:

  • 4 cores: ~6,657 insertions per second.
  • 8 cores: ~13,300 insertions per second.
  • 16 cores: ~26,600 insertions per second.
  • 32 cores: ~53,200 insertions per second.
  • 128 cores: ~212,000 insertions per second (theoretically).

Note: Actual performance may vary depending on system architecture, disk I/O, and specific workload, but benchmarks indicate a substantial increase in insertion rate as the number of CPU cores increases.


API Overview 📚

  • set_key(key, value): Stores a key–value pair. Updates the value if the key already exists.
  • set_multi_keys(key_and_value): (Experimental) Inserts multiple key–value pairs in parallel.
  • get_key(key): Retrieves the value associated with a given key.
  • delete_key(key): Removes a key and its corresponding value.
  • keys(): Returns a list of all stored keys.
  • export_to_json(): (Future feature) Exports your collection to a JSON file.

Development & Contributing 👩‍💻👨‍💻

To contribute or run tests:

  1. Install development dependencies:

    poetry install --dev
    
  2. Run tests using:

    poetry run pytest
    

Feel free to submit issues, pull requests, or feature suggestions. Your contributions help make collections-cache even better!


License 📄

This project is licensed under the MIT License. See the LICENSE file for details.


Acknowledgements 🙌

  • Inspired by the need for efficient, multi-core caching with SQLite.
  • Created by Luiz Trindade.
  • Thanks to all contributors and users who provide feedback to keep improving the library!

Give collections-cache a try and let it power your high-performance caching needs! 🚀

Metadata

Release files for collections-cache 0.3.9.20250420

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for collections-cache 0.3.9.20250420
File Size Uploaded Provenance Actions
collections_cache-0.3.9.20250420.tar.gz 5.4 kB Without provenance Details for collections_cache-0.3.9.20250420.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for collections-cache 0.3.9.20250420
File Interpreter ABI Platform Actions
collections_cache-0.3.9.20250420-py3-none-any.whl Python 3 none any Details for collections_cache-0.3.9.20250420-py3-none-any.whl

Total release size: 12.0 kB

Release files / collections_cache-0.3.9.20250420.tar.gz

Download URL collections_cache-0.3.9.20250420.tar.gz
Size 5.4 kB
Tags Source
SHA-256 checksum
How to use checksums
3db4f3c617e4c2e00ce077b786371649e3646b7199adebda5a92dad16088623a
BLAKE2b-256 checksum
How to use checksums
d16e5cdb3d9b65ae9af9c62941ab68ef7a40bdd863f2b09669b43c911365d76e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.1 CPython/3.12.3 Linux/6.11.0-24-generic

Release files / collections_cache-0.3.9.20250420-py3-none-any.whl

Download URL collections_cache-0.3.9.20250420-py3-none-any.whl
Size 6.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a282d9d584ff13d59fdd2949f80116cc34f3089104306545ad7a864a2768901d
BLAKE2b-256 checksum
How to use checksums
91c3ed492e4328695841331561eb2fc8015ebd5a3a9142e387bacc611016cae9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.1 CPython/3.12.3 Linux/6.11.0-24-generic

Security for collections-cache 0.3.9.20250420

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page