Skip to content

Commit aa8bbac

Browse files
committed
Init repo
0 parents  commit aa8bbac

File tree

12 files changed

+601
-0
lines changed

12 files changed

+601
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
__pycache__
2+
*.pyc
3+
test*.py

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) 2024 Phat Tran
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.

README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Redis cache for Python
2+
3+
- Simple python redis cache library, mostly used as distributed caching, where application servers is in separated processes such as Gunicorn workers, K8s replicas.
4+
- **Asyncio Support for FastAPI, Starlette**
5+
6+
## Requirements
7+
8+
- Redis 5+
9+
- Python 3.6+
10+
11+
## Installation
12+
13+
```bash
14+
$ pip install py-redis-cache
15+
```
16+
17+
## Simple usage
18+
19+
```python
20+
from py_redis_cache import RedisCache
21+
22+
# init redis_cache instance and connection
23+
redis_cache = RedisCache(
24+
host="127.0.0.1",
25+
port=6379,
26+
verbose=1 # Turn on logging for demonstration, set to 0 for silent caching
27+
)
28+
29+
@redis_cache.cache(ttl=10) # Expire after 10 seconds
30+
def heavy_compute(a: list, b: list):
31+
length = max(len(a), len(b))
32+
c = [[]] * length
33+
for i in range(length):
34+
c[i] = a[i] * b[i]
35+
return c
36+
37+
result = heavy_compute([1, 2, 3], [4, 5, 6])
38+
print(result)
39+
# Cache added, key=redis_cache::11=__main__.heavy_compute(a=[1, 2, 3].b=[4, 5, 6]), size=0.000Kb
40+
# [4, 10, 18]
41+
42+
result = heavy_compute([1, 2, 3], [4, 5, 6])
43+
print(result)
44+
# Cache hit, key=redis_cache::11=__main__.heavy_compute(a=[1, 2, 3].b=[4, 5, 6])
45+
# [4, 10, 18]
46+
```
47+
48+
for further examples and use cases please visit [examples](examples)

examples/basic.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import sys
2+
sys.path.append(".")
3+
4+
from py_redis_cache import RedisCache
5+
from py_redis_cache.functional import cache
6+
7+
# init redis_cache instance and connection
8+
redis_cache = RedisCache(
9+
host="127.0.0.1",
10+
port=6379,
11+
verbose=1 # Turn on logging for demonstration, set to 0 for silent caching
12+
)
13+
14+
@redis_cache.cache(ttl=10) # Expire after 10 seconds
15+
def heavy_compute(a: list, b: list):
16+
length = max(len(a), len(b))
17+
c = [[]] * length
18+
for i in range(length):
19+
c[i] = a[i] * b[i]
20+
return c
21+
22+
result = heavy_compute([1, 2, 3], [4, 5, 6])
23+
print(result)
24+
# Cache added, key=redis_cache::11=__main__.heavy_compute(a=[1, 2, 3].b=[4, 5, 6]), size=0.000Kb
25+
# [4, 10, 18]
26+
27+
result = heavy_compute([1, 2, 3], [4, 5, 6])
28+
print(result)
29+
# Cache hit, key=redis_cache::11=__main__.heavy_compute(a=[1, 2, 3].b=[4, 5, 6])
30+
# [4, 10, 18]

0 commit comments

Comments
 (0)