【发布时间】:2016-11-14 04:07:29
【问题描述】:
我使用 Memcached 作为我的 django 应用程序的后端。此代码在正常的 django 查询中工作正常:
def get_myobj():
cache_key = 'mykey'
result = cache.get(cache_key, None)
if not result:
result = Product.objects.all().filter(draft=False)
cache.set(cache_key, result)
return result
但与 django-rest-framework api 调用一起使用时它不起作用:
class ProductListAPIView(generics.ListAPIView):
def get_queryset(self):
product_list = Product.objects.all()
return product_list
serializer_class = ProductSerializer
我即将尝试提供缓存功能的 DRF 扩展:
https://github.com/chibisov/drf-extensions
但 github 上的构建状态目前显示“构建失败”。
我的应用在 api 调用上的读取量很大。有没有办法缓存这些调用?
谢谢。
【问题讨论】:
-
你用“@cache_response()”装饰方法了吗?
-
嗨。 @cache_response 来自 DRF 扩展,我还没有尝试实现它,因为构建状态在他们的 github 页面上显示“构建失败”:github.com/chibisov/drf-extensions
-
你意识到你粘贴的视图没有调用缓存?
-
是的,我在 admin 中修改了值并重新加载了 drf web-browsable api。刷新后值总是改变。如果我没记错的话,默认超时应该是 5 分钟
-
但是网站上的产品列表如果在 5 分钟内刷新,不会改变。所以我假设缓存正在工作(用于网站)
标签: python django caching django-rest-framework memcached