You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Wanna caching this function? That's what you need:
12
+
13
+
```python
14
+
from memoization import cached
15
+
16
+
@cached()
17
+
deffun(arg):
18
+
return do_something_slow(arg)
19
+
```
20
+
21
+
Repetitive calls to ```fun()``` with the same arguments then run ```fun()``` only once.
22
+
23
+
24
+
## Advanced topics
25
+
26
+
### TTL (Time-To-Live)
27
+
28
+
```python
29
+
@cached(ttl=5) # cache expires after 5 seconds
30
+
defread_user_info(user):
31
+
return expensive_db_query(user)
32
+
```
33
+
34
+
For impure functions, TTL will be a solution.
35
+
36
+
### Limited cache capacity
37
+
38
+
```python
39
+
@cached(max_items=1000) # cache holds no more than 1000 items
40
+
defget_compiled_binary(filename):
41
+
return a_very_large_object(filename)
42
+
```
43
+
44
+
When the cache is fully occupied, the data first came will be overwritten.
45
+
46
+
47
+
## Contributing
48
+
49
+
Any improvement or bug-fixing is welcome. Create a [pull request](https://github.com/lonelyenvoy/python-memoization/pulls) when you are done.
50
+
51
+
## License
52
+
53
+
[The MIT License](https://github.com/lonelyenvoy/python-memoization/blob/master/LICENSE)
0 commit comments