cachify 0.2.0__py3-none-any.whl → 0.2.1__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.
- cachify/__init__.py +0 -1
- {cachify-0.2.0.dist-info → cachify-0.2.1.dist-info}/METADATA +69 -11
- {cachify-0.2.0.dist-info → cachify-0.2.1.dist-info}/RECORD +6 -6
- {cachify-0.2.0.dist-info → cachify-0.2.1.dist-info}/licenses/LICENSE +1 -1
- {cachify-0.2.0.dist-info → cachify-0.2.1.dist-info}/WHEEL +0 -0
- {cachify-0.2.0.dist-info → cachify-0.2.1.dist-info}/entry_points.txt +0 -0
cachify/__init__.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: cachify
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.1
|
|
4
4
|
Summary: A simple cache library with sync/async support, Memory and Redis backend
|
|
5
5
|
License: MIT
|
|
6
6
|
License-File: LICENSE
|
|
@@ -28,6 +28,19 @@ Description-Content-Type: text/markdown
|
|
|
28
28
|
|
|
29
29
|
A simple and robust caching library for Python functions, supporting both synchronous and asynchronous code.
|
|
30
30
|
|
|
31
|
+
## Table of Contents
|
|
32
|
+
|
|
33
|
+
- [Features](#features)
|
|
34
|
+
- [Installation](#installation)
|
|
35
|
+
- [Usage](#usage)
|
|
36
|
+
- [Basic Usage](#basic-usage)
|
|
37
|
+
- [Redis Cache](#redis-cache)
|
|
38
|
+
- [Never Die Cache](#never-die-cache)
|
|
39
|
+
- [Skip Cache](#skip-cache)
|
|
40
|
+
- [Testing](#testing)
|
|
41
|
+
- [Contributing](#contributing)
|
|
42
|
+
- [License](#license)
|
|
43
|
+
|
|
31
44
|
## Features
|
|
32
45
|
|
|
33
46
|
- Cache function results based on function ID and arguments
|
|
@@ -41,12 +54,14 @@ A simple and robust caching library for Python functions, supporting both synchr
|
|
|
41
54
|
## Installation
|
|
42
55
|
|
|
43
56
|
```bash
|
|
44
|
-
#
|
|
45
|
-
|
|
46
|
-
|
|
57
|
+
# Using pip
|
|
58
|
+
pip install cachify
|
|
59
|
+
|
|
60
|
+
# Using poetry
|
|
61
|
+
poetry add cachify
|
|
47
62
|
|
|
48
|
-
#
|
|
49
|
-
|
|
63
|
+
# Using uv
|
|
64
|
+
uv add cachify
|
|
50
65
|
```
|
|
51
66
|
|
|
52
67
|
## Usage
|
|
@@ -57,18 +72,57 @@ poetry install
|
|
|
57
72
|
from cachify import cache
|
|
58
73
|
|
|
59
74
|
# Cache function in sync functions
|
|
60
|
-
@cache(ttl=60)
|
|
75
|
+
@cache(ttl=60) # ttl in seconds
|
|
61
76
|
def expensive_calculation(a, b):
|
|
62
77
|
# Some expensive operation
|
|
63
78
|
return a + b
|
|
64
79
|
|
|
65
80
|
# And async functions
|
|
66
|
-
@cache(ttl=3600)
|
|
81
|
+
@cache(ttl=3600) # ttl in seconds
|
|
67
82
|
async def another_calculation(url):
|
|
68
83
|
# Some expensive IO call
|
|
69
84
|
return await httpx.get(url).json()
|
|
70
85
|
```
|
|
71
86
|
|
|
87
|
+
### Decorator Parameters
|
|
88
|
+
|
|
89
|
+
| Parameter | Type | Default | Description |
|
|
90
|
+
| ---------------- | ----------------- | ------- | ---------------------------------------------------- |
|
|
91
|
+
| `ttl` | `int \| float` | `300` | Time to live for cached items in seconds |
|
|
92
|
+
| `never_die` | `bool` | `False` | If True, cache refreshes automatically in background |
|
|
93
|
+
| `cache_key_func` | `Callable` | `None` | Custom function to generate cache keys |
|
|
94
|
+
| `ignore_fields` | `tuple[str, ...]` | `()` | Function parameters to exclude from cache key |
|
|
95
|
+
|
|
96
|
+
### Custom Cache Key Function
|
|
97
|
+
|
|
98
|
+
Use `cache_key_func` when you need custom control over how cache keys are generated:
|
|
99
|
+
|
|
100
|
+
```python
|
|
101
|
+
from cachify import cache
|
|
102
|
+
|
|
103
|
+
def custom_key(args: tuple, kwargs: dict) -> str:
|
|
104
|
+
user_id = kwargs.get("user_id") or args[0]
|
|
105
|
+
return f"user:{user_id}"
|
|
106
|
+
|
|
107
|
+
@cache(ttl=60, cache_key_func=custom_key)
|
|
108
|
+
def get_user_profile(user_id: int):
|
|
109
|
+
return fetch_from_database(user_id)
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### Ignore Fields
|
|
113
|
+
|
|
114
|
+
Use `ignore_fields` to exclude specific parameters from the cache key. Useful when some arguments don't affect the result:
|
|
115
|
+
|
|
116
|
+
```python
|
|
117
|
+
from cachify import cache
|
|
118
|
+
|
|
119
|
+
@cache(ttl=300, ignore_fields=("logger", "request_id"))
|
|
120
|
+
def fetch_data(query: str, logger: Logger, request_id: str):
|
|
121
|
+
# Cache key only uses 'query', ignoring logger and request_id
|
|
122
|
+
logger.info(f"Fetching data for request {request_id}")
|
|
123
|
+
return database.execute(query)
|
|
124
|
+
```
|
|
125
|
+
|
|
72
126
|
### Redis Cache
|
|
73
127
|
|
|
74
128
|
For distributed caching across multiple processes or machines, use `rcache`:
|
|
@@ -80,7 +134,7 @@ from cachify import setup_redis_config, rcache
|
|
|
80
134
|
# Configure Redis (call once at startup)
|
|
81
135
|
setup_redis_config(
|
|
82
136
|
sync_client=redis.from_url("redis://localhost:6379/0"),
|
|
83
|
-
key_prefix="myapp", # default: "
|
|
137
|
+
key_prefix="myapp", # default: "cachify", prefix searchable on redis "PREFIX:*"
|
|
84
138
|
lock_timeout=10, # default: 10, maximum lock lifetime in seconds
|
|
85
139
|
on_error="silent", # "silent" (default) or "raise" in case of redis errors
|
|
86
140
|
)
|
|
@@ -95,7 +149,7 @@ import redis.asyncio as aredis
|
|
|
95
149
|
setup_redis_config(async_client=aredis.from_url("redis://localhost:6379/0"))
|
|
96
150
|
|
|
97
151
|
@rcache(ttl=300)
|
|
98
|
-
def get_user_async(user_id: int) -> dict:
|
|
152
|
+
async def get_user_async(user_id: int) -> dict:
|
|
99
153
|
return await fetch_from_database(user_id)
|
|
100
154
|
```
|
|
101
155
|
|
|
@@ -166,7 +220,11 @@ Run the test scripts
|
|
|
166
220
|
poetry run python -m pytest
|
|
167
221
|
```
|
|
168
222
|
|
|
223
|
+
## Contributing
|
|
224
|
+
|
|
225
|
+
Contributions are welcome! Feel free to open an issue or submit a pull request.
|
|
226
|
+
|
|
169
227
|
## License
|
|
170
228
|
|
|
171
|
-
MIT
|
|
229
|
+
This project is licensed under the MIT License - see the [LICENSE](https://github.com/PulsarDataSolutions/cachify/blob/master/LICENSE) file for details.
|
|
172
230
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
cachify/__init__.py,sha256=
|
|
1
|
+
cachify/__init__.py,sha256=OvqM85Qq3rO6-KilFjq4MO0rm_NBdTfzAo2uhBVksdU,550
|
|
2
2
|
cachify/cache.py,sha256=zJM9J4DdBXyGquQUTDJJ_voC51p-RM_CQcC-4DM92Gg,4021
|
|
3
3
|
cachify/config/__init__.py,sha256=ZvLLEsprTbKai3-uUTbZ6Axbgf-xw-7sxmz8Y62YdT4,98
|
|
4
4
|
cachify/features/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
@@ -17,8 +17,8 @@ cachify/utils/arguments.py,sha256=7NcuntqxK7G1UV4wrIaLNfxE0E7Gr-MmPWi9U-JyQ9o,20
|
|
|
17
17
|
cachify/utils/decorator_factory.py,sha256=brraPKR7ZJqdTbp8s5tEk6D517CKOlo9z1ZLoqhoz_s,1325
|
|
18
18
|
cachify/utils/functions.py,sha256=AA1GXJEEBsIr2NX9h6AslAdmqKaWUcjnf1q5kA216uY,312
|
|
19
19
|
cachify/utils/locks.py,sha256=7x-4XK27NiM_Eu04pftUgEgu91w8B3rE15jjAl0g1AE,216
|
|
20
|
-
cachify-0.2.
|
|
21
|
-
cachify-0.2.
|
|
22
|
-
cachify-0.2.
|
|
23
|
-
cachify-0.2.
|
|
24
|
-
cachify-0.2.
|
|
20
|
+
cachify-0.2.1.dist-info/METADATA,sha256=pl450wfErW4M_deswdy9thDYahuDHno6p1LfJ3kdmHU,7432
|
|
21
|
+
cachify-0.2.1.dist-info/WHEEL,sha256=zp0Cn7JsFoX2ATtOhtaFYIiE2rmFAD4OcMhtUki8W3U,88
|
|
22
|
+
cachify-0.2.1.dist-info/entry_points.txt,sha256=a8B7GSYgDPfUClqct0o7yyBbJ61AWgSSy1idL6YZLUM,45
|
|
23
|
+
cachify-0.2.1.dist-info/licenses/LICENSE,sha256=zUzOFiuoPIQzGktjk4kL78hpi57iWD6-Kug_96OgUO0,1071
|
|
24
|
+
cachify-0.2.1.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|