django-cache-helper is a simple tool for making caching functions, methods, and class methods a little bit easier. 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.
In order to cache and invalidate a function/method/class_method/static_method:
Python |
---|
3.7, 3.8, 3.9, 3.10 |
# Caching a function
@cached(60*60) # 60 Minutes
def foo(bar):
return bar
class Incrementer:
@cached_instance_method(60 * 60)
def instance_increment_by(self, num):
return num
@classmethod
@cached_class_method(60 * 60)
def class_increment_by(cls, num):
return num
@staticmethod
@cached(60 * 60)
def get_datetime():
return datetime.utcnow()
foo(1)
foo.invalidate(1)
Incrementer.instance_increment_by(1)
Incrementer.instance_increment_by.invalidate(1)
Incrementer.class_increment_by(1)
Incrementer.class_increment_by.invalidate(1)
Incrementer.get_datetime()
Incrementer.get_datetime.invalidate()
Thanks goes to these wonderful people.
Kevin Fox |
Tom Jakeway |
Ara Anjargolian |
Hyuckin David Lim |
James |