Skip to main content

Cache Package

Overview

The shared_cache package provides a in-memory storage solution that can be used with FastAPI or any other Python application requiring efficient, inter-process caching.

Internally using the built-in multiprocessing.shared_memory and msgpack.

Installation

pip install shared_cache

Usage

# main.py
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import Any, Hashable
import asyncio
from .models import Item

from shared_cache import Cache

app = FastAPI()
cache = Cache(maxsize=1_280_000) # in mb

@app.on_event("startup")
async def startup_event():
    await cache.clear()  # Ensure cache is clear at startup

@app.post("/set")
async def set_item(item: Item):
    await cache.set(item.key, item.value)
    return {"message": "Item set successfully"}

@app.get("/get/{key}")
async def get_item(key: string):
    value = await cache.get(key)
    if value is None:
        raise HTTPException(status_code=404, detail="Item not found")
    return {"key": key, "value": value}

@app.delete("/delete/{key}")
async def delete_item(key: Hashable):
    await cache.delete(key)
    return {"message": "Item deleted successfully"}
# In both cases below, all 4 workers will have access to the same cache
uvicorn main:app --workers 4
# or
gunicorn --workers 4 -k uvicorn.workers.UvicornWorker main:app

Testing the Package

To test the Cache package, you can use the provided test suite. The tests include inter-process communication scenarios to ensure that the cache works correctly across multiple processes.

  1. Run the Tests:
    pytest tests/test_between_workers.py
    

Contributing

Contributions are welcome! Please open an issue or submit a pull request on the GitHub repository.

License

This project is licensed under the MIT License.

Contact

For any questions or inquiries, please contact wassef911@gmail.com.


Metadata

Release files for shared-cache 1.0.1

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

Source distribution (sdist)

Source distribution for shared-cache 1.0.1
File Size Uploaded Provenance Actions
shared_cache-1.0.1.tar.gz 6.8 kB Without provenance Details for shared_cache-1.0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for shared-cache 1.0.1
File Interpreter ABI Platform Actions
shared_cache-1.0.1-py3-none-any.whl Python 3 none any Details for shared_cache-1.0.1-py3-none-any.whl

Total release size: 12.1 kB

Release files / shared_cache-1.0.1.tar.gz

Download URL shared_cache-1.0.1.tar.gz
Size 6.8 kB
Tags Source
SHA-256 checksum
How to use checksums
ffa561535a12168a5a8db2d4cee68eba1988d933469cf295a56f77957737aa52
BLAKE2b-256 checksum
How to use checksums
3dd0d449ad0eaa9f729c31bc0a1d6acee2e05f2d05dba0b9e042692b211b8e16
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.0

Release files / shared_cache-1.0.1-py3-none-any.whl

Download URL shared_cache-1.0.1-py3-none-any.whl
Size 5.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
72e304669ea14fb13b7731887159b7f4f1cd4cac7acd58e60f1d00a90c95fad2
BLAKE2b-256 checksum
How to use checksums
5752c07b46b7a3978d1709c3d128afe5733b18094d1a50c0793009e98ecabbaa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.0

Security for shared-cache 1.0.1

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

1.0.1 This release

2 release files No provenance

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