openschichtplaner5-api 1.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- openschichtplaner5_api-1.1.0.dist-info/METADATA +134 -0
- openschichtplaner5_api-1.1.0.dist-info/RECORD +40 -0
- openschichtplaner5_api-1.1.0.dist-info/WHEEL +5 -0
- openschichtplaner5_api-1.1.0.dist-info/licenses/LICENSE +21 -0
- openschichtplaner5_api-1.1.0.dist-info/top_level.txt +1 -0
- sp5api/__init__.py +0 -0
- sp5api/_paths.py +23 -0
- sp5api/cache.py +75 -0
- sp5api/dependencies.py +480 -0
- sp5api/main.py +1704 -0
- sp5api/rate_limit_store.py +116 -0
- sp5api/routers/__init__.py +25 -0
- sp5api/routers/absences.py +861 -0
- sp5api/routers/admin.py +841 -0
- sp5api/routers/auth.py +729 -0
- sp5api/routers/availability.py +229 -0
- sp5api/routers/companies.py +270 -0
- sp5api/routers/conflict_report.py +456 -0
- sp5api/routers/email.py +75 -0
- sp5api/routers/employees.py +975 -0
- sp5api/routers/events.py +103 -0
- sp5api/routers/export_scheduler.py +528 -0
- sp5api/routers/ical.py +623 -0
- sp5api/routers/master_data.py +978 -0
- sp5api/routers/misc.py +1455 -0
- sp5api/routers/notification_settings.py +98 -0
- sp5api/routers/notifications.py +311 -0
- sp5api/routers/orm_mirror.py +479 -0
- sp5api/routers/overtime.py +189 -0
- sp5api/routers/qualification_matrix.py +139 -0
- sp5api/routers/recurring_shifts.py +322 -0
- sp5api/routers/reports.py +4306 -0
- sp5api/routers/schedule.py +1657 -0
- sp5api/routers/schedule_comments.py +99 -0
- sp5api/routers/schedule_pdf.py +408 -0
- sp5api/routers/scheduled_reports.py +840 -0
- sp5api/routers/webhooks.py +358 -0
- sp5api/routers/work_time_rules.py +402 -0
- sp5api/schemas.py +129 -0
- sp5api/types.py +20 -0
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: openschichtplaner5-api
|
|
3
|
+
Version: 1.1.0
|
|
4
|
+
Summary: REST API of OpenSchichtplaner5 — the FastAPI service layer over libopenschichtplaner5 (sp5lib): auth/2FA, employees, schedule, absences, reports, notifications and more.
|
|
5
|
+
Author-email: Matthias Schabhüttl <matthias@matthiasschabhuettl.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/mschabhuettl/openschichtplaner5-api
|
|
8
|
+
Project-URL: Source, https://github.com/mschabhuettl/openschichtplaner5-api
|
|
9
|
+
Project-URL: Changelog, https://github.com/mschabhuettl/openschichtplaner5-api/blob/main/CHANGELOG.md
|
|
10
|
+
Project-URL: Issues, https://github.com/mschabhuettl/openschichtplaner5-api/issues
|
|
11
|
+
Project-URL: Main application, https://github.com/mschabhuettl/openschichtplaner5
|
|
12
|
+
Project-URL: Core library, https://github.com/mschabhuettl/libopenschichtplaner5
|
|
13
|
+
Keywords: schichtplaner5,shift-planning,fastapi,rest-api,scheduling
|
|
14
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
15
|
+
Classifier: Intended Audience :: Developers
|
|
16
|
+
Classifier: Operating System :: OS Independent
|
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
21
|
+
Classifier: Framework :: FastAPI
|
|
22
|
+
Classifier: Topic :: Office/Business :: Scheduling
|
|
23
|
+
Requires-Python: >=3.11
|
|
24
|
+
Description-Content-Type: text/markdown
|
|
25
|
+
License-File: LICENSE
|
|
26
|
+
Requires-Dist: fastapi!=0.136.3,>=0.104.0
|
|
27
|
+
Requires-Dist: uvicorn[standard]>=0.24.0
|
|
28
|
+
Requires-Dist: pydantic>=2.0.0
|
|
29
|
+
Requires-Dist: starlette>=0.27.0
|
|
30
|
+
Requires-Dist: anyio>=3.7.0
|
|
31
|
+
Requires-Dist: httpx>=0.24.0
|
|
32
|
+
Requires-Dist: python-multipart>=0.0.7
|
|
33
|
+
Requires-Dist: python-dotenv>=1.0.0
|
|
34
|
+
Requires-Dist: slowapi>=0.1.9
|
|
35
|
+
Requires-Dist: fpdf2>=2.7.0
|
|
36
|
+
Requires-Dist: bcrypt>=4.0.0
|
|
37
|
+
Requires-Dist: PyJWT>=2.8.0
|
|
38
|
+
Requires-Dist: pyotp>=2.9.0
|
|
39
|
+
Requires-Dist: qrcode[pil]>=7.4
|
|
40
|
+
Requires-Dist: openpyxl>=3.1.0
|
|
41
|
+
Requires-Dist: psutil>=5.9.0
|
|
42
|
+
Requires-Dist: Pillow>=10.0.0
|
|
43
|
+
Requires-Dist: SQLAlchemy>=2.0.0
|
|
44
|
+
Requires-Dist: libopenschichtplaner5[postgres]>=1.6.0
|
|
45
|
+
Provides-Extra: dev
|
|
46
|
+
Requires-Dist: pytest>=7.4.0; extra == "dev"
|
|
47
|
+
Requires-Dist: pytest-cov>=4.1.0; extra == "dev"
|
|
48
|
+
Requires-Dist: pytest-asyncio>=0.23.0; extra == "dev"
|
|
49
|
+
Requires-Dist: ruff>=0.1.0; extra == "dev"
|
|
50
|
+
Dynamic: license-file
|
|
51
|
+
|
|
52
|
+
# openschichtplaner5-api
|
|
53
|
+
|
|
54
|
+
[](https://github.com/mschabhuettl/openschichtplaner5-api/actions/workflows/ci.yml)
|
|
55
|
+
[](LICENSE)
|
|
56
|
+
|
|
57
|
+
The REST API behind [**OpenSchichtplaner5**](https://github.com/mschabhuettl/openschichtplaner5) —
|
|
58
|
+
a pip-installable FastAPI service over
|
|
59
|
+
[**libopenschichtplaner5**](https://github.com/mschabhuettl/libopenschichtplaner5) (`sp5lib`),
|
|
60
|
+
serving shift-planning data from the original *Schichtplaner5* FoxPro `.DBF` files or the
|
|
61
|
+
SQLite/PostgreSQL mirror: auth/2FA with JWT sessions, employees, schedule, absences,
|
|
62
|
+
reports/exports, notifications (SSE), webhooks, iCal feeds and more.
|
|
63
|
+
|
|
64
|
+
> **Import name:** the distribution is `openschichtplaner5-api`, but the importable
|
|
65
|
+
> package is **`sp5api`** (mirroring `libopenschichtplaner5` → `sp5lib`).
|
|
66
|
+
> It was extracted from the main app's `backend/api/` with full git history.
|
|
67
|
+
|
|
68
|
+
## What's inside
|
|
69
|
+
|
|
70
|
+
| Module | Purpose |
|
|
71
|
+
|---|---|
|
|
72
|
+
| `sp5api.main` | The FastAPI application (`sp5api.main:app`) — middlewares, health/metrics, SPA serving |
|
|
73
|
+
| `sp5api.routers.*` | One router per domain: `auth`, `employees`, `schedule`, `absences`, `reports`, `notifications`, `availability`, `overtime`, `qualification_matrix`, `recurring_shifts`, `ical`, `webhooks`, `admin`, … |
|
|
74
|
+
| `sp5api.dependencies` | Session store, JWT, rate limiting, logging, DB wiring |
|
|
75
|
+
| `sp5api.schemas` / `sp5api.types` | Pydantic models / type aliases |
|
|
76
|
+
| `sp5api.cache` / `sp5api.rate_limit_store` | Response caching, rate-limit event log |
|
|
77
|
+
|
|
78
|
+
## Installation
|
|
79
|
+
|
|
80
|
+
```bash
|
|
81
|
+
pip install "openschichtplaner5-api @ git+https://github.com/mschabhuettl/openschichtplaner5-api.git@main"
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
(PyPI release pending — once published: `pip install openschichtplaner5-api`.)
|
|
85
|
+
|
|
86
|
+
## Running
|
|
87
|
+
|
|
88
|
+
```bash
|
|
89
|
+
SP5_DB_PATH=/path/to/SP5/Daten python -m uvicorn sp5api.main:app --host 0.0.0.0 --port 8000
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### Key environment variables
|
|
93
|
+
|
|
94
|
+
| Variable | Default | Purpose |
|
|
95
|
+
|---|---|---|
|
|
96
|
+
| `SP5_DB_PATH` | *(set it!)* | Directory with the Schichtplaner5 `.DBF` files |
|
|
97
|
+
| `SP5_BACKEND_DIR` | package parent dir | Host-app resource root: `<dir>/data`, `<dir>/api/data`, `<dir>/api/uploads`, alembic config. Shared contract with `sp5lib` — set it in installed deployments |
|
|
98
|
+
| `SP5_FRONTEND_DIST` | `<SP5_BACKEND_DIR>/../frontend/dist` | Built SPA to serve at `/` (skipped if absent → API-only mode) |
|
|
99
|
+
| `SP5_JWT_SECRET` / `SECRET_KEY` | random per process | JWT signing secret |
|
|
100
|
+
| `SP5_DEV_MODE` | off | Dev bypass token — never in production |
|
|
101
|
+
| `ALLOWED_ORIGINS` | localhost:5173/8000 | CORS origins (comma-separated) |
|
|
102
|
+
| `DB_BACKEND` / `DATABASE_URL` | `dbf` | Switch to the PostgreSQL mirror (via `sp5lib`) |
|
|
103
|
+
|
|
104
|
+
The full list (rate limits, brute-force lockout, SMTP, logging, password policy …) is
|
|
105
|
+
documented in the main app's [`.env.example`](https://github.com/mschabhuettl/openschichtplaner5/blob/main/.env.example).
|
|
106
|
+
|
|
107
|
+
## Development
|
|
108
|
+
|
|
109
|
+
```bash
|
|
110
|
+
python3 -m venv .venv && . .venv/bin/activate
|
|
111
|
+
pip install -e ".[dev]"
|
|
112
|
+
ruff check .
|
|
113
|
+
pytest
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
To develop against a local clone of the library instead of the PyPI release:
|
|
117
|
+
|
|
118
|
+
```bash
|
|
119
|
+
pip install -e "../libopenschichtplaner5[postgres]"
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
`data/` and `api/data/` at the repo root are runtime-state seeds (skills, wishes,
|
|
123
|
+
notification settings) used by the test suite — the same layout the main app keeps
|
|
124
|
+
under `backend/`, resolved via `SP5_BACKEND_DIR`. `tests/fixtures/` holds the DBF
|
|
125
|
+
fixture database.
|
|
126
|
+
|
|
127
|
+
## Releasing
|
|
128
|
+
|
|
129
|
+
Tag `vX.Y.Z` and push — the [release workflow](.github/workflows/release.yml) builds
|
|
130
|
+
sdist+wheel and publishes to PyPI via Trusted Publishing (OIDC).
|
|
131
|
+
|
|
132
|
+
## License
|
|
133
|
+
|
|
134
|
+
MIT — see [LICENSE](LICENSE).
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
openschichtplaner5_api-1.1.0.dist-info/licenses/LICENSE,sha256=AR48Bxp-u4fRBkNgKZLE9NxZ30ENMrDgUVRTd4tPjUU,1077
|
|
2
|
+
sp5api/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
3
|
+
sp5api/_paths.py,sha256=VcAtGM0U_gKb_mBAcOvynYlYGsktppMkXmJQYCj6XxA,929
|
|
4
|
+
sp5api/cache.py,sha256=QPeO_irExgIzvyN0jv-cI4yxh1C8-xWtVaorQJM-gcY,2053
|
|
5
|
+
sp5api/dependencies.py,sha256=E8u28rU21zVOorLVzgk4RWU5t2s98pRKDuhlXI-fSqE,17831
|
|
6
|
+
sp5api/main.py,sha256=ko2Q4kOibzDDO2fDaeF5JrdA7O-_lFq6bS0uz_7HDSo,63125
|
|
7
|
+
sp5api/rate_limit_store.py,sha256=vQzqsfISHUSbHn8t1Hoa7fSaJOPVoaXh1yqfCeu0mYM,3263
|
|
8
|
+
sp5api/schemas.py,sha256=H5noGIbO1N8HHZQ-OEzOXvUHTGDoDoBtc_D7IYYJtb0,4150
|
|
9
|
+
sp5api/types.py,sha256=jvG4QEW1G1I4az0g2lVlJp9eUQ6bZyP25aQg09QO1ww,505
|
|
10
|
+
sp5api/routers/__init__.py,sha256=6Sb6gkmuQt6osPKmb5w5wq_jaaklrHeRmCkPSMh5LrQ,333
|
|
11
|
+
sp5api/routers/absences.py,sha256=eREHFYA0NAYQRJp2sQbFUz3FC6qnJvDas--gRmFbfP4,32695
|
|
12
|
+
sp5api/routers/admin.py,sha256=PprWukSBhsC8whPhHNxVu0mY1URgX8Ope0uYfiTbWCI,29507
|
|
13
|
+
sp5api/routers/auth.py,sha256=Tj9cQwJvenNVVF734dIyeGuwqe2xH_AOX7Itv4T5WZg,25091
|
|
14
|
+
sp5api/routers/availability.py,sha256=ivj1y6HuPYFvWLcCG01uD-p_TLfWcYsmXRImhsv6I8w,7687
|
|
15
|
+
sp5api/routers/companies.py,sha256=0Jh5JqyEaoXj1eclBq2o6BIAtbLj_Xn2Hii0ZuPxeEU,8938
|
|
16
|
+
sp5api/routers/conflict_report.py,sha256=lq473-1qMYb43ASC30b7AAizja564n30vbOA9R1qR-8,17653
|
|
17
|
+
sp5api/routers/email.py,sha256=QRWnAfY-4vTVejRMfj7MdiuOBjjNtWZhYjLIIKoktvw,2284
|
|
18
|
+
sp5api/routers/employees.py,sha256=7pjZ4TAiWd6Aop45MFKPurJXr4smX_BcNwcTV6zMxo8,35370
|
|
19
|
+
sp5api/routers/events.py,sha256=kRGymZYO2IFrLciP47Uq41oWNNBbvCjKJAQSVto-qxY,3662
|
|
20
|
+
sp5api/routers/export_scheduler.py,sha256=Mj5PVCBbKonjkOWsDeDosn5oE7agb6b73a4SBZqHtlo,19461
|
|
21
|
+
sp5api/routers/ical.py,sha256=6p69sKJXL4eQ2Ed5eJq-Oz0dCO0ZAkmVkZkqR4VHKI8,22074
|
|
22
|
+
sp5api/routers/master_data.py,sha256=llbC1TX5-ojsiWG68Ce4Bpbu_65CncvKruhkCMHAsN0,35816
|
|
23
|
+
sp5api/routers/misc.py,sha256=aMveHT4Um7Xfw77w4d8wQQAUQ_dt7sO1rD5urPX4CsI,55376
|
|
24
|
+
sp5api/routers/notification_settings.py,sha256=jWGQvRiniWzspkkAfKuYr1xgNh9Bd_9-_ABgdNdnjHc,2949
|
|
25
|
+
sp5api/routers/notifications.py,sha256=pD7r4XVCWJmcUYMVvg-i9l-SsB7tMTC5PHUpUi71gQ0,10926
|
|
26
|
+
sp5api/routers/orm_mirror.py,sha256=arBV7mBAegB7pTv_RZz6444lyhSIBaUFB0i-qQ6rOYQ,17213
|
|
27
|
+
sp5api/routers/overtime.py,sha256=qXfsT_kJR6ljd6Sg11YwTco5KoGgTKbIEV4Zt3IoWW0,6428
|
|
28
|
+
sp5api/routers/qualification_matrix.py,sha256=LPsyMdBPNMm7EQhtB6rDyrLpTHzgexbZq7f9ffTHFKk,4531
|
|
29
|
+
sp5api/routers/recurring_shifts.py,sha256=U8ANaGVRv5cvwvwSDvR0oEMHbhPGrgkJz0GZLMIveI8,11390
|
|
30
|
+
sp5api/routers/reports.py,sha256=2hRE3OKns3okQVJAy6JJVfMQ2HLaXejfJiZM8iHdDH4,153870
|
|
31
|
+
sp5api/routers/schedule.py,sha256=gLsPRcXeiBscM03xwtXMsZxB1lXa9GiAsK_05EoOIyU,62615
|
|
32
|
+
sp5api/routers/schedule_comments.py,sha256=10IhQDhES0nO9sKk1g2Fc-ADNqvylIhPHzW2uS3pRug,3171
|
|
33
|
+
sp5api/routers/schedule_pdf.py,sha256=XMsWtWUAQXjynC7JNOezCuG4RFuIEMMMKvvhTPMTfPU,11102
|
|
34
|
+
sp5api/routers/scheduled_reports.py,sha256=uAHd9SfoF6W_UbC7JyaYntNvmwv6XbVkoBI0BKbvvnk,32534
|
|
35
|
+
sp5api/routers/webhooks.py,sha256=m40QdXq8xnMmTT6umtLk18jtYj3X-4tJEhsyneMJQEk,11274
|
|
36
|
+
sp5api/routers/work_time_rules.py,sha256=vnCOEGQsp6d4JDvSWRRY5ddr6Z4NRKX5zn2vKWm-QF8,14606
|
|
37
|
+
openschichtplaner5_api-1.1.0.dist-info/METADATA,sha256=7s7puacQp1FW-Foa-BHncDAPmk5Ge0782ro8vRFlQKc,6020
|
|
38
|
+
openschichtplaner5_api-1.1.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
|
|
39
|
+
openschichtplaner5_api-1.1.0.dist-info/top_level.txt,sha256=CjMBVQStK8ckPWwfLuGmdUb73ezbcLbrkpkJrhR4Z2w,7
|
|
40
|
+
openschichtplaner5_api-1.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Matthias Schabhüttl
|
|
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.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
sp5api
|
sp5api/__init__.py
ADDED
|
File without changes
|
sp5api/_paths.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"""Resolve the host application's backend resource root.
|
|
2
|
+
|
|
3
|
+
The API keeps mutable runtime state (JSON document stores, uploads) and finds
|
|
4
|
+
host resources (frontend dist, CHANGELOG.md) under one root directory — the
|
|
5
|
+
same root sp5lib (libopenschichtplaner5) resolves via the SP5_BACKEND_DIR
|
|
6
|
+
environment variable: ``backend/`` in the main application, the repo root in
|
|
7
|
+
this repository's dev/test checkout.
|
|
8
|
+
|
|
9
|
+
The fallback mirrors ``sp5lib._resource_paths``: the directory containing the
|
|
10
|
+
sp5api package. That is only correct for an in-tree/source checkout — installed
|
|
11
|
+
deployments must set SP5_BACKEND_DIR explicitly (the main app's start.sh,
|
|
12
|
+
Dockerfile and CI do).
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
import os
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def backend_dir() -> str:
|
|
19
|
+
"""Return the host backend root (see module docstring)."""
|
|
20
|
+
env = os.environ.get("SP5_BACKEND_DIR")
|
|
21
|
+
if env:
|
|
22
|
+
return os.path.abspath(env)
|
|
23
|
+
return os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
sp5api/cache.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"""Simple TTL-based in-memory cache for frequently queried DB data.
|
|
2
|
+
|
|
3
|
+
No external dependencies (no Redis). Thread-safe via threading.Lock.
|
|
4
|
+
Cache entries expire after TTL seconds and are invalidated on writes.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import threading
|
|
8
|
+
import time
|
|
9
|
+
from typing import Any
|
|
10
|
+
|
|
11
|
+
_lock = threading.Lock()
|
|
12
|
+
_store: dict[str, tuple[float, Any]] = {} # key -> (expires_at, value)
|
|
13
|
+
|
|
14
|
+
# Default TTL in seconds
|
|
15
|
+
DEFAULT_TTL = 60
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def get(key: str) -> Any | None:
|
|
19
|
+
"""Return cached value if present and not expired, else None."""
|
|
20
|
+
with _lock:
|
|
21
|
+
entry = _store.get(key)
|
|
22
|
+
if entry is None:
|
|
23
|
+
return None
|
|
24
|
+
expires_at, value = entry
|
|
25
|
+
if time.monotonic() > expires_at:
|
|
26
|
+
del _store[key]
|
|
27
|
+
return None
|
|
28
|
+
return value
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def put(key: str, value: Any, ttl: float = DEFAULT_TTL) -> None:
|
|
32
|
+
"""Store a value with the given TTL (seconds)."""
|
|
33
|
+
with _lock:
|
|
34
|
+
_store[key] = (time.monotonic() + ttl, value)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def invalidate(*prefixes: str) -> int:
|
|
38
|
+
"""Remove all cache entries whose keys start with any of the given prefixes.
|
|
39
|
+
|
|
40
|
+
Returns the number of entries removed.
|
|
41
|
+
"""
|
|
42
|
+
with _lock:
|
|
43
|
+
to_delete = [
|
|
44
|
+
k for k in _store if any(k.startswith(p) for p in prefixes)
|
|
45
|
+
]
|
|
46
|
+
for k in to_delete:
|
|
47
|
+
del _store[k]
|
|
48
|
+
return len(to_delete)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def clear() -> int:
|
|
52
|
+
"""Remove all entries. Returns count removed."""
|
|
53
|
+
with _lock:
|
|
54
|
+
n = len(_store)
|
|
55
|
+
_store.clear()
|
|
56
|
+
return n
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def stats() -> dict:
|
|
60
|
+
"""Return cache statistics."""
|
|
61
|
+
with _lock:
|
|
62
|
+
now = time.monotonic()
|
|
63
|
+
total = len(_store)
|
|
64
|
+
expired = sum(1 for _, (exp, _v) in _store.items() if now > exp)
|
|
65
|
+
return {"total": total, "active": total - expired, "expired": expired}
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def get_or_set(key: str, factory, ttl: float = DEFAULT_TTL) -> Any:
|
|
69
|
+
"""Return cached value or call factory() to compute, cache, and return it."""
|
|
70
|
+
value = get(key)
|
|
71
|
+
if value is not None:
|
|
72
|
+
return value
|
|
73
|
+
result = factory()
|
|
74
|
+
put(key, result, ttl)
|
|
75
|
+
return result
|