## 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
```sh
pip install shared_cache
```
### Usage
```python
# 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"}
```
```sh
# 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**:
```bash
pytest tests/test_between_workers.py
```
### Contributing
Contributions are welcome! Please open an issue or submit a pull request on the [GitHub repository](https://github.com/wassef911/shared_cache).
### License
This project is licensed under the MIT License.
### Contact
For any questions or inquiries, please contact [wassef911@gmail.com](mailto:wassef911@gmail.com).
---
Raw data
{
"_id": null,
"home_page": "https://github.com/wassef911/shared_cache",
"name": "shared-cache",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.8.0",
"maintainer_email": null,
"keywords": "python3, pypi, package",
"author": "Wassef911",
"author_email": "wassef911@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/3d/d0/d449ad0eaa9f729c31bc0a1d6acee2e05f2d05dba0b9e042692b211b8e16/shared_cache-1.0.1.tar.gz",
"platform": null,
"description": "## Cache Package\n\n### Overview\n\nThe `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.\n\nInternally using the built-in `multiprocessing.shared_memory` and `msgpack`.\n\n### Installation\n\n```sh\npip install shared_cache\n```\n\n### Usage\n\n```python\n# main.py\nfrom fastapi import FastAPI, HTTPException\nfrom pydantic import BaseModel\nfrom typing import Any, Hashable\nimport asyncio\nfrom .models import Item\n\nfrom shared_cache import Cache\n\napp = FastAPI()\ncache = Cache(maxsize=1_280_000) # in mb\n\n@app.on_event(\"startup\")\nasync def startup_event():\n await cache.clear() # Ensure cache is clear at startup\n\n@app.post(\"/set\")\nasync def set_item(item: Item):\n await cache.set(item.key, item.value)\n return {\"message\": \"Item set successfully\"}\n\n@app.get(\"/get/{key}\")\nasync def get_item(key: string):\n value = await cache.get(key)\n if value is None:\n raise HTTPException(status_code=404, detail=\"Item not found\")\n return {\"key\": key, \"value\": value}\n\n@app.delete(\"/delete/{key}\")\nasync def delete_item(key: Hashable):\n await cache.delete(key)\n return {\"message\": \"Item deleted successfully\"}\n\n```\n\n```sh\n# In both cases below, all 4 workers will have access to the same cache\nuvicorn main:app --workers 4\n# or\ngunicorn --workers 4 -k uvicorn.workers.UvicornWorker main:app\n```\n\n\n### Testing the Package\n\nTo 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.\n\n1. **Run the Tests**:\n ```bash\n pytest tests/test_between_workers.py\n ```\n\n### Contributing\n\nContributions are welcome! Please open an issue or submit a pull request on the [GitHub repository](https://github.com/wassef911/shared_cache).\n\n### License\n\nThis project is licensed under the MIT License.\n\n### Contact\n\nFor any questions or inquiries, please contact [wassef911@gmail.com](mailto:wassef911@gmail.com).\n\n---\n",
"bugtrack_url": null,
"license": null,
"summary": "Across workers shared cache for python3",
"version": "1.0.1",
"project_urls": {
"Homepage": "https://github.com/wassef911/shared_cache"
},
"split_keywords": [
"python3",
" pypi",
" package"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "5752c07b46b7a3978d1709c3d128afe5733b18094d1a50c0793009e98ecabbaa",
"md5": "f59d19e5e152f4b164c005e6d6fc9c0f",
"sha256": "72e304669ea14fb13b7731887159b7f4f1cd4cac7acd58e60f1d00a90c95fad2"
},
"downloads": -1,
"filename": "shared_cache-1.0.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "f59d19e5e152f4b164c005e6d6fc9c0f",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8.0",
"size": 5344,
"upload_time": "2024-05-18T21:20:31",
"upload_time_iso_8601": "2024-05-18T21:20:31.287528Z",
"url": "https://files.pythonhosted.org/packages/57/52/c07b46b7a3978d1709c3d128afe5733b18094d1a50c0793009e98ecabbaa/shared_cache-1.0.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "3dd0d449ad0eaa9f729c31bc0a1d6acee2e05f2d05dba0b9e042692b211b8e16",
"md5": "603a489af475215474953940520fb5ad",
"sha256": "ffa561535a12168a5a8db2d4cee68eba1988d933469cf295a56f77957737aa52"
},
"downloads": -1,
"filename": "shared_cache-1.0.1.tar.gz",
"has_sig": false,
"md5_digest": "603a489af475215474953940520fb5ad",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8.0",
"size": 6799,
"upload_time": "2024-05-18T21:20:33",
"upload_time_iso_8601": "2024-05-18T21:20:33.454017Z",
"url": "https://files.pythonhosted.org/packages/3d/d0/d449ad0eaa9f729c31bc0a1d6acee2e05f2d05dba0b9e042692b211b8e16/shared_cache-1.0.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-05-18 21:20:33",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "wassef911",
"github_project": "shared_cache",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [
{
"name": "msgpack",
"specs": [
[
"==",
"1.0.8"
]
]
}
],
"tox": true,
"lcname": "shared-cache"
}