Skip to content

Commit

Permalink
add layer config item expired to "expire" tiles based on a timestamp
Browse files Browse the repository at this point in the history
test the mtime of each tile against the layer config expired setting and rebuild if nessasary
Disk cache ONLY!
  • Loading branch information
winkey committed Nov 6, 2011
1 parent 7b5319f commit 83bdfa8
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
35 changes: 35 additions & 0 deletions tilecache/TileCache/Caches/Disk.py
Expand Up @@ -46,6 +46,33 @@ def access(self, path, type='read'):
else:
return os.access(path, os.W_OK)

###########################################################################
##
## @brief test a tile to see if it has expired
##
## @param path the full path to the tile to test
## @param TileCache::Layer the layer
##
## @return true if expire time on the layer is less than the tiles mtime,
## or false in any other case
##
###########################################################################

def isExpired(self, path, layer):

if layer.expired != None:

try:
mtime = os.stat(path).st_mtime
except Exception, E:
raise Exception( "isExpired %s %s\n", (path, E) )
return False

if mtime < layer.expired:
return True

return False

def getKey (self, tile):
components = ( self.basedir,
tile.layer.name,
Expand All @@ -72,8 +99,16 @@ def getKey (self, tile):
###########################################################################

def get (self, tile):

filename = self.getKey(tile)
if self.access(filename, 'read'):

##### test if the tile has expired #####

if self.isExpired(filename, tile.layer) and not self.readonly:
os.unlink(filename)
return None

if self.sendfile:
return filename
else:
Expand Down
22 changes: 19 additions & 3 deletions tilecache/TileCache/Layer.py
@@ -1,6 +1,6 @@
# BSD Licensed, Copyright (c) 2006-2010 TileCache Contributors

import os, sys
import os, sys, time
from warnings import warn
from Client import WMS
from Service import TileCacheException
Expand Down Expand Up @@ -100,7 +100,8 @@ class Layer (object):
"watermarkimage", "watermarkopacity",
"extent_type", "tms_type", "units", "mime_type",
"paletted",
"spherical_mercator", "metadata")
"spherical_mercator", "metadata",
"expired")

config_properties = [
{'name':'spherical_mercator', 'description':'Layer is in spherical mercator. (Overrides bbox, maxresolution, SRS, Units)', 'type': 'boolean'},
Expand All @@ -109,6 +110,7 @@ class Layer (object):
{'name':'bbox', 'description':'Bounding box of the layer grid', 'default':'-180,-90,180,90'},
{'name':'srs', 'description':'Spatial Reference System for the layer', 'default':'EPSG:4326'},
{'name':'data_extent', 'description':'Bounding box of the layer data. (Same SRS as the layer grid.)', 'default':"", 'type': 'map'},
{'name':'expired', 'description':'Timestamp of latest change to source data.', 'default':""}
]

def __init__ (self, name, layers = None, bbox = (-180, -90, 180, 90),
Expand All @@ -118,7 +120,8 @@ def __init__ (self, name, layers = None, bbox = (-180, -90, 180, 90),
extension = "png", mime_type = None, cache = None, debug = True,
watermarkimage = None, watermarkopacity = 0.2,
spherical_mercator = False,
extent_type = "strict", units = "degrees", tms_type = "", **kwargs ):
extent_type = "strict", units = "degrees", tms_type = "",
expired = None, **kwargs ):
"""Take in parameters, usually from a config file, and create a Layer.
>>> l = Layer("Name", bbox="-12,17,22,36", debug="no")
Expand Down Expand Up @@ -201,6 +204,19 @@ def __init__ (self, name, layers = None, bbox = (-180, -90, 180, 90),
self.watermarkopacity = float(watermarkopacity)

self.metadata = {}

if expired == None:
self.expired = None

else:
try:
os.environ['TZ'] = 'UTC'
time.tzset()
mytime = time.strptime(expired, "%Y-%m-%dT%H:%M:%SZ")
self.expired = time.mktime(mytime)
except:
sys.stderr.write("invalid expired timestamp format on layer %s\n" % name)
self.expired = None;

prefix_len = len("metadata_")
for key in kwargs:
Expand Down

0 comments on commit 83bdfa8

Please sign in to comment.