TileCache默认是Disk缓存Tile数据,存储和检索的效率远不及数据库,所以要开发数据库Cache,读完TileCache代码,发现其系统结构设计的还算可以
tilecache-2.10\TileCache\Caches目录下就是TileCache自带的Cache类型,Cache有个抽象基类,我的数据Cache只要实现这些Cache的接口便能完成到数据库的Tile存取。
1 class Cache (object):
2 def __init__ (self, timeout = 30.0, stale_interval = 300.0, readonly = False, **kwargs):
3 self.stale = float(stale_interval)
4 self.timeout = float(timeout)
5 self.readonly = readonly
6
7 def lock (self, tile, blocking = True):
8 start_time = time.time()
9 result = self.attemptLock(tile)
10 if result:
11 return True
12 elif not blocking:
13 return False
14 while result is not True:
15 if time.time() - start_time > self.timeout:
16 raise Exception("You appear to have a stuck lock. You may wish to remove the lock named:\n%s" % self.getLockName(tile))
17 time.sleep(0.25)
18 result = self.attemptLock(tile)
19 return True
20
21 def getLockName (self, tile):
22 return self.getKey(tile) + ".lck"
23
24 def getKey (self, tile):
25 raise NotImplementedError()
26
27 def attemptLock (self, tile):
28 raise NotImplementedError()
29
30 def unlock (self, tile):
31 raise NotImplementedError()
32
33 def get (self, tile):
34 raise NotImplementedError()
35
36 def set (self, tile, data):
37 raise NotImplementedError()
38
39 def delete(self, tile):
40 raise NotImplementedError()
接口够简单了吧 ,最主要的实现的 是get,set,getKey这3个主要接口
tilecache.cfg的Cache.type设置为DB_POSTGRES