为什么我的tastypie缓存没有被调用?

Jes*_*dge 3 python django tastypie

我正在查看tastypie缓存文档并尝试设置我自己的简单缓存,但缓存似乎没有被调用.当我访问的http://本地主机:8000/API /调查/格式= JSON,我让我的tastypie产生JSON,但我没有得到从缓存类的输出.

from tastypie.resources import ModelResource
from tastypie.cache import NoCache
from .models import Poll


class JSONCache(NoCache):
    def _load(self):
        print 'loading cache'
        data_file = open(settings.TASTYPIE_JSON_CACHE, 'r')
        return json.load(data_file)

    def _save(self, data):
        print 'saving to cache'
        data_file = open(settings.TASTYPIE_JSON_CACHE, 'w')
        return json.dump(data, data_file)

    def get(self, key):
        print 'jsoncache.get'
        data = self._load()
        return data.get(key, None)

    def set(self, key, value, timeout=60):
        print 'jsoncache.set'
        data = self._load()
        data[key] = value
        self._save(data)


class PollResource(ModelResource):
    class Meta:
        queryset = Poll.objects.all()
        resource_name = 'poll'
        cache = JSONCache()
Run Code Online (Sandbox Code Playgroud)

kgr*_*kgr 7

似乎Tastypie不会自动缓存列表,tastypie.resources在线附近1027:

def get_list(self, request, **kwargs):

    # ...

    # TODO: Uncached for now. Invalidation that works for everyone may be
    #       impossible.
    objects = self.obj_get_list(
        request=request, **self.remove_api_resource_names(kwargs))

    # ...
Run Code Online (Sandbox Code Playgroud)

,而有细节(围绕线1050):

def get_detail(self, request, **kwargs):

   # ...

   try:
       obj = self.cached_obj_get(
           request=request, **self.remove_api_resource_names(kwargs))

   # ...
Run Code Online (Sandbox Code Playgroud)

...请注意,在前一个片段obj_get_list中调用而不是cached_obj_get_list.也许覆盖get_list和使用cached_obj_get_list也允许你在这里使用缓存?

现在你可能会从你的类获得http://localhost:8000/api/poll/<pk>/?format=json(详细视图)的输出,但http://localhost:8000/api/poll/?format=json默认情况下不会输出(列表视图).