Python – Images caching in browser – app-engine-patch application

app-engine-patchbrowsercachinggoogle-app-enginepython

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'

Best Answer

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