Skip to content

Commit 18cdbec

Browse files
committed
init from old account
0 parents  commit 18cdbec

21 files changed

+845
-0
lines changed

.editorconfig

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
indent_style = space
6+
indent_size = 2
7+
end_of_line = lf
8+
insert_final_newline = true
9+
trim_trailing_whitespace = true
10+
11+
[*.py]
12+
charset = utf-8
13+
indent_style = space
14+
indent_size = 4
15+
end_of_line = lf
16+
insert_final_newline = true
17+
trim_trailing_whitespace = true
18+
19+
[*.{md,mdx}]
20+
trim_trailing_whitespace = false

.github/workflows/release.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: Release
2+
3+
on:
4+
release:
5+
types: [created]
6+
7+
jobs:
8+
deploy:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v2
12+
- name: Set up Python
13+
uses: actions/setup-python@v2
14+
with:
15+
python-version: "3.x"
16+
17+
- name: Install dependencies
18+
run: |
19+
python -m pip install --upgrade pip
20+
pip install setuptools wheel twine
21+
22+
- name: Build and publish
23+
env:
24+
TWINE_USERNAME: __token__
25+
TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }}
26+
run: |
27+
python setup.py sdist bdist_wheel
28+
twine upload --repository pypi dist/*

.github/workflows/test.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Test
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
branches:
9+
- main
10+
11+
jobs:
12+
build:
13+
runs-on: ubuntu-latest
14+
strategy:
15+
matrix:
16+
python: [3.8, 3.9, 3.10, 3.11, 3.12]
17+
18+
steps:
19+
- uses: actions/checkout@v2
20+
- name: Setup Python
21+
uses: actions/setup-python@v2
22+
with:
23+
python-version: ${{ matrix.python }}
24+
- name: Install Tox and any other packages
25+
run: pip install tox
26+
- name: Run Tox
27+
run: tox -e py

.gitignore

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
share/python-wheels/
24+
*.egg-info/
25+
.installed.cfg
26+
*.egg
27+
MANIFEST
28+
29+
# PyInstaller
30+
# Usually these files are written by a python script from a template
31+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
32+
*.manifest
33+
*.spec
34+
35+
# Installer logs
36+
pip-log.txt
37+
pip-delete-this-directory.txt
38+
39+
# Unit test / coverage reports
40+
htmlcov/
41+
.tox/
42+
.nox/
43+
.coverage
44+
.coverage.*
45+
.cache
46+
nosetests.xml
47+
coverage.xml
48+
*.cover
49+
*.py,cover
50+
.hypothesis/
51+
.pytest_cache/
52+
cover/
53+
54+
# Translations
55+
*.mo
56+
*.pot
57+
58+
# Django stuff:
59+
*.log
60+
local_settings.py
61+
db.sqlite3
62+
db.sqlite3-journal
63+
64+
# Flask stuff:
65+
instance/
66+
.webassets-cache
67+
68+
# Scrapy stuff:
69+
.scrapy
70+
71+
# Sphinx documentation
72+
docs/_build/
73+
74+
# PyBuilder
75+
.pybuilder/
76+
target/
77+
78+
# Jupyter Notebook
79+
.ipynb_checkpoints
80+
81+
# IPython
82+
profile_default/
83+
ipython_config.py
84+
85+
# pyenv
86+
# For a library or package, you might want to ignore these files since the code is
87+
# intended to run in multiple environments; otherwise, check them in:
88+
# .python-version
89+
90+
# pipenv
91+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
93+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
94+
# install all needed dependencies.
95+
#Pipfile.lock
96+
97+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
98+
__pypackages__/
99+
100+
# Celery stuff
101+
celerybeat-schedule
102+
celerybeat.pid
103+
104+
# SageMath parsed files
105+
*.sage.py
106+
107+
# Environments
108+
.env
109+
.venv
110+
env/
111+
venv/
112+
ENV/
113+
env.bak/
114+
venv.bak/
115+
116+
# Spyder project settings
117+
.spyderproject
118+
.spyproject
119+
120+
# Rope project settings
121+
.ropeproject
122+
123+
# mkdocs documentation
124+
/site
125+
126+
# mypy
127+
.mypy_cache/
128+
.dmypy.json
129+
dmypy.json
130+
131+
# Pyre type checker
132+
.pyre/
133+
134+
# pytype static type analyzer
135+
.pytype/
136+
137+
# Cython debug symbols
138+
cython_debug/

.pre-commit-config.yaml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
repos:
2+
- repo: https://github.com/pre-commit/pre-commit-hooks
3+
rev: v4.4.0
4+
hooks:
5+
- id: trailing-whitespace
6+
- id: end-of-file-fixer
7+
- id: check-yaml
8+
- id: debug-statements
9+
- id: requirements-txt-fixer
10+
11+
- repo: https://github.com/astral-sh/ruff-pre-commit
12+
rev: v0.3.4
13+
hooks:
14+
- id: ruff
15+
- id: ruff-format
16+
17+
- repo: https://github.com/PyCQA/bandit
18+
rev: "1.7.8"
19+
hooks:
20+
- id: bandit
21+
files: \.py$
22+
args: ["--exclude", "tests/"]
23+
24+
- repo: https://github.com/gitleaks/gitleaks
25+
rev: v8.18.2
26+
hooks:
27+
- id: gitleaks
28+
29+
- repo: https://github.com/asottile/reorder_python_imports
30+
rev: v3.9.0
31+
hooks:
32+
- id: reorder-python-imports
33+
exclude: ^(pre_commit/resources/|testing/resources/python3_hooks_repo/)
34+
args: [--py38-plus, --add-import, "from __future__ import annotations"]

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 Amine Ben Ahmed
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

MANIFEST.in

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
include pyproject.toml
2+
include *.md
3+
include LICENSE
4+
recursive-include src/shared_cache *.py
5+
include *.json
6+
include *.txt
7+
include *.yaml

README.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
---

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[build-system]
2+
requires = ["setuptools>=46.4.0", "wheel"]
3+
build-backend = "setuptools.build_meta"

requirements-dev.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-r requirements.txt
2+
pre-commit==3.0.1
3+
pytest-asyncio==0.23.6
4+
pytest-sugar==1.0.0
5+
pytest-timeout==2.2.0

0 commit comments

Comments
 (0)