美文网首页
Django-DRF redis缓存机制

Django-DRF redis缓存机制

作者: Ming_a221 | 来源:发表于2018-06-12 14:19 被阅读0次

1.安装扩展包

pip install drf-extensions

2.setting配置

CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://x.x.x.x:6379",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
}
}
}

3.Views.py中导入及使用,直接把 CacheResponseMixin 放在继承类的最前面

from myapps.serializers import UserSerializer
from rest_framework_extensions.cache.mixins import CacheResponseMixin
class UserViewSet(CacheResponseMixin, viewsets.ModelViewSet):
serializer_class = UserSerializer

相关文章

网友评论

      本文标题:Django-DRF redis缓存机制

      本文链接:https://www.haomeiwen.com/subject/ypgleftx.html