Why is my tastypie cache not called?

I am looking at tastypie caching docs and trying to set up my own simple caching thing, but the cache doesn't seem to get a call. When I am at http: // localhost: 8000 / api / poll /? Format = json , I get my tastypie generated json, but I do not get the output from the cache class.

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()
+3
source share
1 answer

It seems that Tastypie does not automatically write off lists tastypie.resourcesaround the line 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))

    # ...

whereas with the details (around the line 1050):

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

   # ...

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

   # ...

... , 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 ( ) .

+7

All Articles