【问题标题】:django tastypie caching is not working for list datadjango sweetpie 缓存不适用于列表数据
【发布时间】:2015-08-07 15:11:56
【问题描述】:

我正在使用 python 2.7,django=1.5 和 sweetpie v0.12.1 当我被击中 1 资源数据然后缓存正在发生

class BranchResource(ModelResource):
    class Meta:
        collection_name="data"
        queryset = Branch.objects.all()
        resource_name = 'branch'
        authorization = Authorization()
        limit = 0 #(unlimted)
        filtering = {
            "branch_flag": ALL,
            "branch_name":('exact', 'startswith','istartswith',),
            "cid":ALL,
        }
        cache = SimpleCache(timeout=1000)

当我点击时缓存正在工作

http://127.0.0.1:8000/v0/branch/1

但是当我获取所有数据时,缓存不起作用

http://127.0.0.1:8000/v0/branch/

那个时间缓存不起作用是直接命中数据库并从数据库中获取数据

【问题讨论】:

    标签: django python-2.7 caching tastypie


    【解决方案1】:

    您可以覆盖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)
    

    然后http://127.0.0.1:8000/v0/branch/ 将被缓存,而不仅仅是http://127.0.0.1:8000/v0/branch/1

    【讨论】:

      【解决方案2】:

      我猜你的服务器配置有问题。请编辑您的服务器配置文件,如下所示 (如果是 nginx) vi /etc/nginx/sites-enabled/your-site-name 并在文件中添加以下代码行

      location / {
      
         proxy_pass http://127.0.0.1:8000;
         proxy_set_header Host $host;
         proxy_set_header X-Real-IP $remote_addr;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_cache     cache;
      
         }
      }
      

      您要做的第二件事是在您的项目中创建一个名为缓存的目录/文件夹

      Finally go to nginx.conf and edit the file like this
      vi /etc/nginx/nginx.conf
      go to line no 80
      proxy_cache_path yourworkspace/projectname/cache keys_zone=cache:1m max_size=2m;
      

      完成!!!现在缓存可以正常工作了

      【讨论】:

        【解决方案3】:

        来自Tastypie documentation

        但是,值得注意的是,这些不会缓存序列化的表示。对于大流量,我们鼓励使用缓存代理,尤其是 Varnish,因为它在这种用法下大放异彩。它比 Django 视图快得多,并且已经巧妙地处理了大多数情况。

        这让你有两个选择:

        • 实现您自己的缓存类,派生自tastypie.cache.SimpleCachetastypie.cache.NoCache,并支持可迭代的数据类型。

        • 听从 sweetpie 的建议,使用专用的缓存服务器。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-05-29
          • 1970-01-01
          • 1970-01-01
          • 2010-11-12
          • 1970-01-01
          • 2017-09-20
          • 1970-01-01
          • 2013-06-23
          相关资源
          最近更新 更多