在浏览器中缓存图片 - app-engine-patch应用程序 [英] Images caching in browser - app-engine-patch application

查看:176
本文介绍了在浏览器中缓存图片 - app-engine-patch应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

response ['Content-Type'] ='image / jpg'

response ['Last-Modified'] = current_time.strftime('%a,%d%b%Y%H:%M:%S GMT')

response ['Expires'] = current_time + timedelta(days = 30)
$ b response ['Cache-Control'] ='public,max- age = 2592000'

解决方案

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

  def view_image(request,key ):
data = memcache.get(key)
如果数据不是无:
if(request.META.get('HTTP_IF_MODIFIED_SINCE')> = data ['Last-Modified' ]):
data.status_code = 304
返回数据
else:
image_content_blob = #some代码从数据存储中获取图像
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)
返回响应


I have a little problem with caching the images in the browser for my app-engine application I`m sending last-modified, expires and cache-control headers but image is loaded from the server every time. Here is the header part of the code:

response['Content-Type'] = 'image/jpg'

response['Last-Modified'] = current_time.strftime('%a, %d %b %Y %H:%M:%S GMT')

response['Expires'] = current_time + timedelta(days=30)

response['Cache-Control'] = 'public, max-age=2592000'

解决方案

Here is an example code for my fix copy in 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

这篇关于在浏览器中缓存图片 - app-engine-patch应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆