[Tilecache] PATCH: X-SendFile and Expires headers

Christopher Schmidt crschmidt at metacarta.com
Tue Oct 6 01:47:37 EDT 2009


On Mon, Oct 05, 2009 at 05:24:25PM -0400, mdeneen+tiles at saucontech.com wrote:
> 
> This patch adds X-SendFile and the ability to set an Expires header.  It is a patch against tilecache 2.10.

Looks like the patch only patches the WSGI handler. It is possible to
add this to the rest of the handlers in time, right? I don't mind applying
it as is, with a caveat that I'd like to see a documentation option for each
of these, and specifying that they only work with wsgi at the oment,
with possible instructions on how difficult it would be to add support to 
the cgi and mod python handlers.

-- Chris 

> Once applied, you can specify two new options under [cache] in the tilecache.cfg:
> 
> sendfile=yes
>     This instructs tilecache to not read the tile image and send the fully qualified file name back to the web server in a X-SendFile header.  This avoids both python and the web server from holding the tile in memory at the same time.  The web server serves the tile directly to the client.
> expires=n
>     ...where nnnn is the number of hours from access time you wish to have the tiles possibly cached in the client browser.
> 
> I've tested this with lighttpd and apache (with mod_xsendfile http://tn123.ath.cx/mod_xsendfile/), and it works perfectly.  I've not tried seeding the cache with this.  You might have to disable sendfile to do that.  I'll test tomorrow.
> 
> Best Regards,
> Mark Deneen
> Saucon Technologies
> diff -ru tilecache-2.10/TileCache/Caches/Disk.py ../tilecache.mod/TileCache/Caches/Disk.py
> --- tilecache-2.10/TileCache/Caches/Disk.py	2008-12-14 09:32:29.000000000 -0500
> +++ ../tilecache.mod/TileCache/Caches/Disk.py	2009-10-05 15:50:57.000000000 -0400
> @@ -4,11 +4,14 @@
>  import sys, os, time, warnings
>  
>  class Disk (Cache):
> -    def __init__ (self, base = None, umask = '002', **kwargs):
> +    def __init__ (self, base = None, sendfile = False, expire = False,  umask = '002', **kwargs):
>          Cache.__init__(self, **kwargs)
>          self.basedir = base
>          self.umask = int(umask, 0)
> -        
> +        self.sendfile = sendfile and sendfile.lower() in ["yes", "y", "t", "true"]
> +        self.expire = expire
> +        if expire != False:
> +            self.expire = long(expire) * 3600
>          if sys.platform.startswith("java"):
>              from java.io import File
>              self.file_module = File
> @@ -64,8 +67,11 @@
>      def get (self, tile):
>          filename = self.getKey(tile)
>          if self.access(filename, 'read'):
> -            tile.data = file(filename, "rb").read()
> -            return tile.data
> +            if self.sendfile:
> +                return filename
> +            else:
> +                tile.data = file(filename, "rb").read()
> +                return tile.data
>          else:
>              return None
>  
> diff -ru tilecache-2.10/TileCache/Service.py ../tilecache.mod/TileCache/Service.py
> --- tilecache-2.10/TileCache/Service.py	2009-01-18 21:11:52.000000000 -0500
> +++ ../tilecache.mod/TileCache/Service.py	2009-10-05 16:12:34.000000000 -0400
> @@ -4,7 +4,7 @@
>  
>  class TileCacheException(Exception): pass
>  
> -import sys, cgi, time, os, traceback, ConfigParser
> +import sys, cgi, time, os, traceback, email, ConfigParser
>  import Cache, Caches
>  import Layer, Layers
>  
> @@ -291,8 +291,17 @@
>          fields = parse_formvars(environ)
>  
>          format, image = service.dispatchRequest( fields, path_info, req_method, host )
> -        start_response("200 OK", [('Content-Type',format)])
> -        return [image]
> +        headers = [('Content-Type',format)]
> +        if service.cache.sendfile:
> +            headers.append(('X-SendFile', image))
> +        if service.cache.expire:
> +            headers.append(('Expires', email.Utils.formatdate(time.time() + service.cache.expire, False, True)))
> +
> +        start_response("200 OK", headers)
> +        if service.cache.sendfile:
> +            return []
> +        else:
> +            return [image]
>  
>      except TileCacheException, E:
>          start_response("404 Tile Not Found", [('Content-Type','text/plain')])

> _______________________________________________
> Tilecache mailing list
> Tilecache at openlayers.org
> http://openlayers.org/mailman/listinfo/tilecache




More information about the Tilecache mailing list