【问题标题】:how to update tastypie cache in normal view function?如何在普通视图功能中更新tastepie缓存?
【发布时间】:2013-05-28 06:12:11
【问题描述】:

tastypie api不支持上传文件,所以我必须使用普通视图功能,查看我的other question

这是我的图片资源

 class ImageResource(ModelResource):
     album = fields.ForeignKey(AlbumResource, 'album')
     upload_by = fields.ForeignKey(UserResource, 'upload_by')

     class Meta:
         always_return_data=True
         filtering = {
                 "album": ('exact',),
                  }
         queryset = Image.objects.all()
         cache = SimpleCache(timeout=100)
         resource_name = 'image'
         authorization = ImageAuthorization()

现在假设我以普通视图功能上传图片,因为我将缓存超时设置为 100 秒,浏览器不会在 100 秒内更新查询。

我想要的是浏览器在图像上传后立即更新查询,如果没有任何变化,将缓存超时保持为 100 秒。这必须在普通视图功能中完成。

我该怎么做?

【问题讨论】:

  • 你确定好吃的派不支持文件上传吗? This question 似乎表明可以这样做。
  • 好吧,example1,example2,example3 的所有方法我都试过了,还是不行,另一方面在普通视图下上传就是这么简单!跨度>

标签: django view memcached tastypie


【解决方案1】:

您可以根据需要手动清除缓存。 Tastypie 的缓存键看起来像这样(如果您使用的是 simpleCache)

def generate_cache_key(self, *args, **kwargs):
    """
    Creates a unique-enough cache key.

    This is based off the current api_name/resource_name/args/kwargs.
    """
    smooshed = []

    for key, value in kwargs.items():
        smooshed.append("%s=%s" % (key, value))

    # Use a list plus a ``.join()`` because it's faster than concatenation.
    return "%s:%s:%s:%s" % (self._meta.api_name, self._meta.resource_name, ':'.join(args), ':'.join(smooshed))

(直接来自the code,目前位于第 1031 行)

您可以在 shell 中四处游荡,以确保准确命中您要查找的对象。完成后,只需在上传文件时删除该条目即可。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-22
    • 2018-02-12
    • 2017-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多