Django-DRF redis缓存机制

2018-06-12  本文已影响0人  Ming_a221

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

上一篇 下一篇

猜你喜欢

热点阅读