Django上下文缓存库。
django-context-cache的Python项目详细描述
序言
Django-context-cache
使用上下文缓存扩展Django框架,该缓存为每个请求或每个修饰块缓存数据。在
安装
- 使用
pip
命令安装django-context-cache
:
pip install django-context-cache
- {cd5>
使用
假设有一个函数或方法,您希望每个请求缓存一个结果:
import random
import string
from context_cache.decorators import cache_for_context
@cache_for_context
def get_random_string():
letters = string.ascii_lowercase
return ''.join(random.choice(letters) for i in range(10))
如果多次调用函数get_random_string,则值将从缓存获取:
def view(request):
get_random_string() == get_random_string() # should return True
如果您想缓存请求之外的函数,例如在django命令中,可以使用decorator init_context_cache
。decorator也可以用作上下文处理器:
from django.core.management.base import BaseCommand, CommandError
from context_cache.decorators import init_context_cache
class Command(BaseCommand):
@init_context_cache
def handle(self, *args, **options):
get_random_string() == get_random_string() # should return True
- 项目
标签: