Django缓存工具最初是为Ella CMS开发的
dj-cache-tools的Python项目详细描述
django缓存工具最初是为Ella CMS开发的。 它包含一些有用的缓存工具(如缓存的foreignkey、用于缓存对象的函数、用于缓存函数/方法的decorator等)
用于型号
在models.py中,您可以使用cachedforeignkey
from __future__ import unicode_literals from django.db import models from cache_tools.fields import CachedForeignKey @python_2_unicode_compatible class Question(models.Model): question_text = models.CharField(max_length=200) pub_date = models.DateTimeField('date published') def __str__(self): return self.question_text @python_2_unicode_compatible class Choice(models.Model): question = CachedForeignKey(Question, on_delete=models.CASCADE) choice_text = models.CharField(max_length=200) votes = models.IntegerField(default=0) def __str__(self): return self.choice_text
缓存函数的用法
在whatever.py中,您可以使用缓存这个:
from cache_tools.utils import cache_this from .models import Question @cache_this(lambda *args, **kwargs: 'my_app_all_questions_cache_key') def get_all_guestions(): return list(Question.objects.all().order_by('pk'))