Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将redis缓存提取到django rest框架序列化程序中_Django_Caching_Serialization_Redis_Celery Task - Fatal编程技术网

将redis缓存提取到django rest框架序列化程序中

将redis缓存提取到django rest框架序列化程序中,django,caching,serialization,redis,celery-task,Django,Caching,Serialization,Redis,Celery Task,我正在使用redis作为缓存后端。我正在尝试从序列化程序获取redis缓存数据 我的代码是: class CocView(APIView): """ Celery and Redis Usage """ def get(self,request): data = cache.get('alldata') print "In the Cache",data if not data: print "in the database" data

我正在使用redis作为缓存后端。我正在尝试从序列化程序获取redis缓存数据

我的代码是:

class CocView(APIView):
"""
Celery and Redis Usage
"""
def get(self,request):

    data = cache.get('alldata')
    print "In the Cache",data
    if not data:
        print "in the database"
        data = Coc.objects.values('cache_id', 'username', 'email')
        test_result=mytask.delay(data) 
        test_result=mytask.delay() 

    serializer = CocSerializer(data, many=True)
    return Response(serializer.data)
我正在使用PUT更新数据库中已有的数据

我将redis缓存数据传递到序列化程序中,序列化程序并没有更新,但它仍然显示来自数据库的更新数据

这意味着我的序列化程序将自动从数据库中获取数据。 我不知道情况如何

在my settings.py中,我为redis提供了以下中间件

'django.middleware.cache.UpdateCacheMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.cache.FetchFromCacheMiddleware',
在my task.py中,我有以下任务

 from __future__ import absolute_import
 from app.models import Coc
 from app.celery import app
 from django.core.cache import cache
 from app.serializers import *
 from app.views import *

 @app.task
 def mytask(data):
    try:
        dataa = data
        cache.set('alldata', dataa,60*45)
        return 

    except ValueError:
        return "error"
如果有人对它的实现有任何建议,请告诉我们。

在您的shell中尝试此功能

from django.core.cache import cache
然后输入

cache.get('alldata')
如果返回none,则表示缓存未使用corresponding键进行设置。在这种情况下,请使用以下语句设置缓存:

cache.set('alldata') = {<your data>}
cache.set('alldata')={
这将为您设置缓存,然后它将处理来自缓存的数据


您正在接收的数据显示为
None
empty
,因此每当您尝试发送序列化程序数据时,它都会查询数据库。

实际上,我们正在芹菜任务文件中设置缓存。首先,我需要了解
dataa=data
的原因:,不管怎么说,我能想到的没有设置缓存的原因是芹菜任务没有运行,因此无法为您设置缓存,或者我能想到的另一个原因是芹菜任务可能已成功运行,但您在任务实际运行之前访问了缓存,因此您获得了无数据。为此,请尝试不将缓存设置为芹菜任务,而是在某些脚本中设置它,然后尝试访问。