Skip to content

Commit 2f35aa9

Browse files
Update README and version for perscache
- Enhanced caching functionality with instance-specific caching for both synchronous and asynchronous methods. - Added detailed examples demonstrating the use of the cache decorator with instance methods and async functions. - Updated version number to 0.7.0 in setup.cfg to reflect new features and improvements. This commit improves documentation clarity and ensures users understand the new caching capabilities for class instances.
1 parent 4f09025 commit 2f35aa9

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,12 @@ def get_data():
3333
### Caching
3434
- Easy to swap out the cache configuration when switching environments.
3535
- Async functions supported.
36+
- Instance-specific caching for both sync and async class methods.
3637
- Time-to-live (TTL) support - automatically invalidate cache entries after a certain time.
3738
- Automatic cache invalidation when the decorated function arguments or code have been changed.
3839
- You can ignore changes in certain arguments of the decorated function.
40+
- Each instance of a class maintains its own separate cache.
41+
- Smart detection of instance methods vs standalone functions.
3942

4043
### Serialization and storage
4144
- Various serialization formats: JSON, YAML, pickle, Parquet, CSV etc.
@@ -84,6 +87,49 @@ print(counter) # the function was called only once
8487
# 1
8588
```
8689

90+
### Instance Method Caching
91+
The cache decorator works seamlessly with both regular functions and instance methods:
92+
93+
```python
94+
class Calculator:
95+
def __init__(self):
96+
self.compute_count = 0
97+
98+
@cache
99+
def add(self, a: int, b: int) -> int:
100+
self.compute_count += 1
101+
return a + b
102+
103+
calc1 = Calculator()
104+
calc2 = Calculator()
105+
106+
# First call computes the result
107+
result1 = calc1.add(5, 3) # compute_count = 1
108+
# Second call uses cache
109+
result2 = calc1.add(5, 3) # compute_count still 1
110+
111+
# Different instance gets its own cache
112+
result3 = calc2.add(5, 3) # calc2.compute_count = 1
113+
```
114+
115+
The cache is instance-specific, so different instances of the same class maintain separate caches. This works for both synchronous and asynchronous methods.
116+
117+
### Async Support
118+
The cache works with async functions and methods:
119+
120+
```python
121+
@cache
122+
async def fetch_data(url: str):
123+
print("Fetching data...")
124+
response = await some_async_request(url)
125+
return response
126+
127+
# First call fetches
128+
data1 = await fetch_data("example.com")
129+
# Second call uses cache
130+
data2 = await fetch_data("example.com")
131+
```
132+
87133
### Changing parameters or the code of the function invalidates the cache
88134
```python
89135
@cache

setup.cfg

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = perscache
3-
version = 0.6.2
3+
version = 0.7.0
44
author = leshchenko1979
55
author_email = [email protected]
66
summary = An easy to use decorator for persistent memoization: like `functools.lrucache`, but results can be saved in any format to any storage.
@@ -16,11 +16,12 @@ classifier =
1616
Natural Language :: English
1717
Operating System :: OS Independent
1818
Programming Language :: Python
19-
Programming Language :: Python :: 3.6
20-
Programming Language :: Python :: 3.7
2119
Programming Language :: Python :: 3.8
2220
Programming Language :: Python :: 3.9
2321
Programming Language :: Python :: 3.10
22+
Programming Language :: Python :: 3.11
23+
Programming Language :: Python :: 3.12
24+
Programming Language :: Python :: 3.13
2425
Programming Language :: Python :: Implementation :: CPython
2526
Topic :: Software Development
2627

0 commit comments

Comments
 (0)