【问题标题】:How to cache API JSON response in Django Tastypie如何在 Django Tastypie 中缓存 API JSON 响应
【发布时间】:2013-11-27 17:44:35
【问题描述】:

我是 django 的美味派 REST 新手,我在我的 ModelResource 中使用属性缓存但不起作用,当我更改数据库并刷新 API url http://localhost:8000/api/rest/tip/ 这显示了最后的更改,此信息不需要经常刷新

class TipResource(ModelResource):
    class Meta:
        queryset = Tip.objects.all()
        resource_name = 'tip'
        allowed_methods = ['get']
        include_resource_uri = False
        cache = SimpleCache(timeout=1000)

我需要缓存这个响应,以免对数据库进行很多查询

有什么问题?

谢谢

【问题讨论】:

    标签: django caching tastypie


    【解决方案1】:

    Tastypie 的 SimpleCache 将缓存单个资源获取,但不会缓存查询。这是非常有限的。所以 /api/rest/tip// 将被缓存,但任何需要查询的请求都不会。

    缓存结果的最简单方法是启用 Django 的视图缓存。在这种情况下,Django 只会缓存序列化的 JSON 结果,并返回它直到缓存超时。 Tastypie 甚至不会看到请求。

    但是,要小心,因为 Django 的缓存使用 HTTP 缓存,所以浏览器可以缓存数据而根本不会向您的服务器发出请求。如果您的数据发生变化,那么在这种情况下您就完蛋了。

    适当的解决方案有点复杂,并且不包含在 Tastypie 中。不久前我实际上已经在这方面工作过。请在 sweetpie 组中查看我的消息,尽管我还没有看到其他人对解决方案感兴趣:https://groups.google.com/forum/?hl=en#!topic/django-tastypie/87mlo7bvCgo

    【讨论】:

    • 该消息有后续吗?简直不敢相信没有任何兴趣......
    • 从未有过跟进。我相信使任意查询的缓存无效的问题几乎是不可能的,所以没有处理。如果只使用特定查询,则必须自己手动处理缓存失效。
    【解决方案2】:

    您可以在tastypie.resources.ModelResource 中覆盖函数get_list 将代码objects = self.obj_get_list(bundle=base_bundle,**self.remove_api_resource_names(kwargs))改为objects=self.cached_obj_get_list(bundle=base_bundle,**self.remove_api_resource_names(kwargs))

    def get_list(self, request, **kwargs):
        base_bundle = self.build_bundle(request=request)
    
        objects = self.cached_obj_get_list(bundle=base_bundle, **self.remove_api_resource_names(kwargs))
    
        sorted_objects = self.apply_sorting(objects, options=request.GET)
    
        paginator = self._meta.paginator_class(request.GET, sorted_objects, resource_uri=self.get_resource_uri(), limit=self._meta.limit, max_limit=self._meta.max_limit, collection_name=self._meta.collection_name)
        to_be_serialized = paginator.page()
    
        # Dehydrate the bundles in preparation for serialization.
        bundles = []
    
        for obj in to_be_serialized[self._meta.collection_name]:
            bundle = self.build_bundle(obj=obj, request=request)
            bundles.append(self.full_dehydrate(bundle, for_list=True))
    
        to_be_serialized[self._meta.collection_name] = bundles
        to_be_serialized = self.alter_list_data_to_serialize(request, to_be_serialized)
    
        return self.create_response(request, to_be_serialized)
    

    然后 /api/rest/tip/ 将被缓存,不仅 /api/rest/tip//

    【讨论】:

      猜你喜欢
      • 2012-06-07
      • 2013-02-16
      • 2016-02-18
      • 2015-08-21
      • 1970-01-01
      • 2017-09-03
      • 2018-12-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多