django tastypie 缓存不适用于列表数据
django tastypie caching is not working for list data
我正在使用 python 2.7,django=1.5 和 tastypie 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/
当时缓存不起作用是直接命中数据库并从数据库中获取数据
However, it’s worth noting that these do NOT cache serialized representations. For heavy traffic, we’d encourage the use of a caching proxy, especially Varnish, as it shines under this kind of usage. It’s far faster than Django views and already neatly handles most situations.
你有两个选择:
实现您自己的缓存 class,派生自 tastypie.cache.SimpleCache
或 tastypie.cache.NoCache
,并支持可迭代数据类型。
听从 tastypie 的建议并使用专用缓存服务器。
我想问题出在您的服务器配置上。请编辑您的服务器配置文件,如下所示
(如果是 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;
}
}
您必须做的第二件事是在您的项目中创建一个 directory/folder 称为缓存
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;
完成!!!现在缓存可以正常工作了
您可以覆盖 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/ will get cached,not only http://127.0.0.1:8000/v0/branch/1
我正在使用 python 2.7,django=1.5 和 tastypie 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/
当时缓存不起作用是直接命中数据库并从数据库中获取数据
However, it’s worth noting that these do NOT cache serialized representations. For heavy traffic, we’d encourage the use of a caching proxy, especially Varnish, as it shines under this kind of usage. It’s far faster than Django views and already neatly handles most situations.
你有两个选择:
实现您自己的缓存 class,派生自
tastypie.cache.SimpleCache
或tastypie.cache.NoCache
,并支持可迭代数据类型。听从 tastypie 的建议并使用专用缓存服务器。
我想问题出在您的服务器配置上。请编辑您的服务器配置文件,如下所示 (如果是 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;
}
}
您必须做的第二件事是在您的项目中创建一个 directory/folder 称为缓存
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;
完成!!!现在缓存可以正常工作了
您可以覆盖 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/ will get cached,not only http://127.0.0.1:8000/v0/branch/1