Add extension methods to cache
-
Install
cache extension
by pip::pip install django-cache-extension
-
For redis backend use cache like this::
config your cache file backend to cache_extension:
CACHES={ "default": { 'BACKEND': 'cache_extension.backends.redis.ExtensionRedisBackend', 'LOCATION': 'redis://redis:6379/0', 'TIMEOUT': '172800', "KEY_PREFIX": "cache_extension", 'OPTIONS': { "DB": 0, "CLIENT_CLASS": "django_redis.client.DefaultClient", 'PARSER_CLASS': 'redis.connection.HiredisParser', 'PICKLE_VERSION': 2, } } },
-
For custom cache backend::
from cache_extension.cache import ExtensionCache from some_module import CustomCache class ExtensionCustomCache(ExtensionCache, CustomCache): pass
-
Use extension cache methods::
cache.get_model(Article, pk=1) cache.get_models(Article, [1,2,3]) cache.get_model(UserArticle, user_id=1, article_id=1) cache.get_model_list(UserArticle, user_id=1)
-
Built in cached fields: ForeignKeyField, OneToOneField.
Say you have two models defined as below:
from django.db import models from cache_extension import cached_fields class Foo(models.Model): pass class Bar(models.Model): foo = cached_fields.ForeignKeyField(Foo)
Then instances of Bar will have a
cached_foo
property which will try to fetch foo from cache first, if cache misses, will fallback to database.bar = Bar.objects.get(pk=1) bar.cached_foo # cached foreign field, really handy