开发者

Images caching in browser - app-engine-patch application

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜