|
| 1 | +## Cache Package |
| 2 | + |
| 3 | +### Overview |
| 4 | + |
| 5 | +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. |
| 6 | + |
| 7 | +Internally using the built-in `multiprocessing.shared_memory` and `msgpack`. |
| 8 | + |
| 9 | +### Installation |
| 10 | + |
| 11 | +```sh |
| 12 | +pip install shared_cache # https://pypi.org/project/shared-cache/ |
| 13 | +``` |
| 14 | + |
| 15 | +### Usage |
| 16 | + |
| 17 | +```python |
| 18 | +from fastapi import FastAPI, HTTPException |
| 19 | +from pydantic import BaseModel |
| 20 | +from typing import Any, Hashable |
| 21 | +import asyncio |
| 22 | +from .models import Item |
| 23 | + |
| 24 | +from shared_cache import Cache |
| 25 | + |
| 26 | +app = FastAPI() |
| 27 | +cache = Cache(maxsize=1_280_000) # in mb |
| 28 | + |
| 29 | +@app.on_event("startup") |
| 30 | +async def startup_event(): |
| 31 | + await cache.clear() |
| 32 | + |
| 33 | +@app.post("/set") |
| 34 | +async def set_item(item: Item): |
| 35 | + await cache.set(item.key, item.value) |
| 36 | + return {"message": "Item set successfully"} |
| 37 | + |
| 38 | +@app.get("/get/{key}") |
| 39 | +async def get_item(key: string): |
| 40 | + value = await cache.get(key) |
| 41 | + if value is None: |
| 42 | + raise HTTPException(status_code=404, detail="Item not found") |
| 43 | + return {"key": key, "value": value} |
| 44 | + |
| 45 | +@app.delete("/delete/{key}") |
| 46 | +async def delete_item(key: Hashable): |
| 47 | + await cache.delete(key) |
| 48 | + return {"message": "Item deleted successfully"} |
| 49 | + |
| 50 | +``` |
| 51 | + |
| 52 | +```sh |
| 53 | +# In both cases below, all 4 workers will have access to the same cache |
| 54 | +uvicorn main:app --workers 4 |
| 55 | +# or |
| 56 | +gunicorn --workers 4 -k uvicorn.workers.UvicornWorker main:app |
| 57 | +``` |
| 58 | + |
| 59 | + |
| 60 | +### Testing the Package |
| 61 | + |
| 62 | +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. |
| 63 | + |
| 64 | +1. **Run the Tests**: |
| 65 | + ```bash |
| 66 | + pytest tests/test_between_workers.py |
| 67 | + ``` |
| 68 | + |
| 69 | +### Contributing |
| 70 | + |
| 71 | +Contributions are welcome! Please open an issue or submit a pull request on the [GitHub repository](https://github.com/wassef911/shared_cache). |
| 72 | + |
| 73 | +### License |
| 74 | + |
| 75 | +This project is licensed under the MIT License. |
| 76 | + |
| 77 | +### Contact |
| 78 | + |
| 79 | +For any questions or inquiries, please contact [wassef911@gmail.com](mailto:wassef911@gmail.com). |
| 80 | + |
| 81 | +--- |
0 commit comments