Skip to content Skip to sidebar Skip to footer

Send A "304 Not Modified" For Images Stored In The Datastore

I store user-uploaded images in the Google App Engine datastore as db.Blob, as proposed in the docs. I then serve those images on /images/.jpg. The server always sends a

Solution 1:

Bloggart uses this technique. Have a look at this blog post.

classStaticContentHandler(webapp.RequestHandler):
  defoutput_content(self, content, serve=True):
    self.response.headers['Content-Type'] = content.content_type
    last_modified = content.last_modified.strftime(HTTP_DATE_FMT)
    self.response.headers['Last-Modified'] = last_modified
    self.response.headers['ETag'] = '"%s"' % (content.etag,)
    if serve:
      self.response.out.write(content.body)
    else:
      self.response.set_status(304)

  defget(self, path):
    content = get(path)
    ifnot content:
      self.error(404)
      return

    serve = Trueif'If-Modified-Since'in self.request.headers:
      last_seen = datetime.datetime.strptime(
          self.request.headers['If-Modified-Since'],
          HTTP_DATE_FMT)
      if last_seen >= content.last_modified.replace(microsecond=0):
        serve = Falseif'If-None-Match'in self.request.headers:
      etags = [x.strip('" ')
               for x in self.request.headers['If-None-Match'].split(',')]
      if content.etag in etags:
        serve = False
    self.output_content(content, serve)

Solution 2:

There might be a simpler solution here. This requires that you never overwrite the data associated with any identifier, e.g. modifying the image would create a new id (and hence a new URL).

Simply set the Expires header from your request handler to the far future, e.g. now + a year. This would result in clients caching the image and not asking for an update until that time comes.

This approach has some tradeoffs, like ensuring new URLs are embedded when images are modified, so you have to decide for yourself. What jbochi is proposing is the other alternative that puts more logic into the image request handler.

Solution 3:

By the way, thanks to webob, webapp.RequestHandler provides easy way to check If-None-Match.

if etag in self.request.if_none_match:
    pass# do something

Solution 4:

why would the code use this:

self.response.headers['ETag'] = '"%s"' % (content.etag,)

instead of this:

self.response.headers['ETag'] = '"%s"' % content.etag

I think it is the same and will use the 2nd unless someone explains the reasoning.

Post a Comment for "Send A "304 Not Modified" For Images Stored In The Datastore"