|
1 | 1 | django-cache-helper
|
2 | 2 | ===================
|
3 | 3 |
|
4 |
| -## Support |
5 |
| -**Python:** 3.4, 3.5, 3.6, 3.7 |
6 |
| - |
7 |
| -**Django:** 1.7, 1.8, 1.9, 1.10, 1.11, 2.0, 2.1, 2.2, 3.0 |
| 4 | + |
| 5 | + |
| 6 | +[](https://github.com/ycharts/django_cache_helper/actions/workflows/main.yml) |
| 7 | +[](https://coveralls.io/github/ycharts/django_cache_helper?branch=master) |
8 | 8 |
|
9 | 9 | ## Overview
|
10 | 10 | django-cache-helper is a simple tool for making caching functions, methods, and class methods a little bit easier.
|
11 | 11 | It is largely based off of django-cache-utils, however, since cache-utils did not support caching model methods by instance and carried other features I didn't need, django-cache-helper was created.
|
12 | 12 |
|
13 |
| -In order to cache a function/method/class_method: |
| 13 | +In order to cache and invalidate a function/method/class_method/static_method: |
| 14 | + |
| 15 | +## Support |
| 16 | + |
| 17 | +| Python | Django | |
| 18 | +|--------|--------| |
| 19 | +| 3.7, 3.8, 3.9, 3.10 | 3.2 | |
| 20 | + |
| 21 | + |
| 22 | +#### How to Cache |
14 | 23 |
|
15 | 24 | ```python
|
16 |
| -@cached(60*60) |
| 25 | +# Caching a function |
| 26 | +@cached(60*60) # 60 Minutes |
17 | 27 | def foo(bar):
|
18 | 28 | return bar
|
19 | 29 |
|
20 |
| -@property |
21 |
| -@cached(60*60) |
22 |
| -def foo(self): |
23 |
| - return self.id + 2 |
| 30 | +class Incrementer: |
| 31 | + |
| 32 | + @cached_instance_method(60 * 60) |
| 33 | + def instance_increment_by(self, num): |
| 34 | + return num |
| 35 | + |
| 36 | + @classmethod |
| 37 | + @cached_class_method(60 * 60) |
| 38 | + def class_increment_by(cls, num): |
| 39 | + return num |
| 40 | + |
| 41 | + @staticmethod |
| 42 | + @cached(60 * 60) |
| 43 | + def get_datetime(): |
| 44 | + return datetime.utcnow() |
24 | 45 | ```
|
25 | 46 |
|
| 47 | +#### How to invalidate a cache |
| 48 | + |
| 49 | +```python |
| 50 | + |
| 51 | +foo(1) |
| 52 | +foo.invalidate(1) |
| 53 | + |
| 54 | +Incrementer.instance_increment_by(1) |
| 55 | +Incrementer.instance_increment_by.invalidate(1) |
| 56 | + |
| 57 | +Incrementer.class_increment_by(1) |
| 58 | +Incrementer.class_increment_by.invalidate(1) |
| 59 | + |
| 60 | +Incrementer.get_datetime() |
| 61 | +Incrementer.get_datetime.invalidate() |
| 62 | +``` |
| 63 | + |
| 64 | + |
| 65 | +## Contributors ✨ |
| 66 | + |
| 67 | +Thanks goes to these wonderful people. |
| 68 | + |
| 69 | +<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section --> |
| 70 | +<!-- prettier-ignore-start --> |
| 71 | +<!-- markdownlint-disable --> |
| 72 | +<table> |
| 73 | + <tr> |
| 74 | + <td align="center"><img src="https://avatars.githubusercontent.com/u/2000316?v=4" width="100px;" alt="Kevin Fox"/><br /><sub><b>Kevin Fox</b></sub></td> |
| 75 | + <td align="center"><img src="https://avatars.githubusercontent.com/u/3022071?v=4" width="100px;" alt="Tom Jakeway"/><br /><sub><b>Tom Jakeway</b></sub></td> |
| 76 | + <td align="center"><img src="https://avatars.githubusercontent.com/u/83293?v=4" width="100px;" alt="Ara Anjargolian"/><br /><sub><b>Ara Anjargolian</b></sub></td> |
| 77 | + <td align="center"><img src="https://avatars.githubusercontent.com/u/15602942?v=4" width="100px;" alt="Hyuckin David Lim"/><br /><sub><b>Hyuckin David Lim</b></sub></td> |
| 78 | + <td align="center"><img src="https://avatars.githubusercontent.com/u/1248116?v=4" width="100px;" alt="James"/><br /><sub><b>James</b></sub></td> |
| 79 | + </tr> |
| 80 | +</table> |
26 | 81 |
|
| 82 | +<!-- markdownlint-enable --> |
| 83 | +<!-- prettier-ignore-end --> |
| 84 | +<!-- ALL-CONTRIBUTORS-LIST:END --> |
0 commit comments