我正在使用Django-redis缓存,我将数据保存在redis缓存中但是当我更新数据库时,我无法在redis缓存中找到更新的数据。
我的代码是:
from django.shortcuts import render
from myapp.models import students
from rest_framework.views import APIView
from rest_framework.response import Response
from django.core.cache import cache
class UserListView(APIView):
def get(self, request):
data = cache.get('alldata')
if not data:
data = students.objects.values('cache_id', 'username', 'email')
cache.set('alldata', data)
在settings.py中,我在中间件类中有以下项目:
MIDDLEWARE_CLASSES = (
'django.middleware.cache.UpdateCacheMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
'debug_toolbar.middleware.DebugToolbarMiddleware',
'django.middleware.cache.FetchFromCacheMiddleware',
)
有没有办法自动更新redis缓存?