标签:指定 cache ack return efault 前言 objects tin 链接地址
pip install django-redis
# 缓存配置
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache", # 默认选择redis作为缓存
"LOCATION": "redis://127.0.0.1:6379/0", # redis链接地址,并指定数据库
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
}
}
}
django.core.cache
中的cache
缓存Api去设置缓存数据from django.core.cache import cache
class GetArea(APIView):
authentication_classes = []
def get(self, request):
# cache.get("键名")去取redis中缓存的数据
foo = cache.get(‘foo‘)
if foo:
print(‘存在缓存‘, foo)
return MyResponse(data=foo)
province = Province.objects.all()
province_serial = ProvinceSerializers(province, many=True)
# cache.set("键名","值")设置缓存到redis
cache.set(‘foo‘, province_serial.data)
return MyResponse(data=province_serial.data)
标签:指定 cache ack return efault 前言 objects tin 链接地址
原文地址:https://www.cnblogs.com/se7enjean/p/13714391.html