代码之家  ›  专栏  ›  技术社区  ›  Ilian Iliev

浏览器中的图像缓存-应用程序引擎修补程序

  •  4
  • Ilian Iliev  · 技术社区  · 16 年前

    我在为我的应用程序引擎应用程序在浏览器中缓存图像时遇到了一个小问题 我发送的是上次修改,过期和缓存控制头,但图像每次都从服务器加载。 下面是代码的头部分:

    response['content-type']='图像/jpg'

    response['last-modified']=当前时间。strftime('%a、%d%b%y%h:%m:%s GMT')

    响应['expires']=当前时间+时间增量(天=30)

    response['cache-control']=“公共,最大使用年限=2592000”

    1 回复  |  直到 9 年前
        1
  •  7
  •   Ilian Iliev    16 年前

    下面是我在dpaste中的修复副本的示例代码 here

    def view_image(request, key):
      data = memcache.get(key)  
      if data is not None:  
        if(request.META.get('HTTP_IF_MODIFIED_SINCE') >= data['Last-Modified']):  
          data.status_code = 304  
        return data  
      else:  
        image_content_blob = #some code to get the image from the data store  
        current_time = datetime.utcnow()
        response = HttpResponse()
        last_modified = current_time - timedelta(days=1)
        response['Content-Type'] = 'image/jpg'
        response['Last-Modified'] = last_modified.strftime('%a, %d %b %Y %H:%M:%S GMT')
        response['Expires'] = current_time + timedelta(days=30)
        response['Cache-Control']  = 'public, max-age=315360000'
        response['Date']           = current_time
        response.content = image_content_blob
    
        memcache.add(image_key, response, 86400)
        return response