/usr/lib/python2.7/dist-packages/TileStache/Redis.py is in tilestache 1.51.5-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | """ Caches tiles to Redis
Requires redis-py and redis-server
https://pypi.python.org/pypi/redis/
http://redis.io/
sudo apt-get install redis-server
pip install redis
Example configuration:
"cache": {
"name": "Redis",
"host": "localhost",
"port": 6379,
"db": 0,
"key prefix": "unique-id"
}
Redis cache parameters:
host
Defaults to "localhost" if omitted.
port
Integer; Defaults to 6379 if omitted.
db
Integer; Redis database number, defaults to 0 if omitted.
key prefix
Optional string to prepend to generated key.
Useful when running multiple instances of TileStache
that share the same Redis database to avoid key
collisions (though the prefered solution is to use a different
db number). The key prefix will be prepended to the
key name. Defaults to "".
"""
from __future__ import absolute_import
from time import time as _time, sleep as _sleep
# We enabled absolute_import because case insensitive filesystems
# cause this file to be loaded twice (the name of this file
# conflicts with the name of the module we want to import).
# Forcing absolute imports fixes the issue.
try:
import redis
except ImportError:
# at least we can build the documentation
pass
def tile_key(layer, coord, format, key_prefix):
""" Return a tile key string.
"""
name = layer.name()
tile = '%(zoom)d/%(column)d/%(row)d' % coord.__dict__
key = str('%(key_prefix)s/%(name)s/%(tile)s.%(format)s' % locals())
return key
class Cache:
"""
"""
def __init__(self, host="localhost", port=6379, db=0, key_prefix=''):
self.host = host
self.port = port
self.db = db
self.conn = redis.Redis(host=self.host, port=self.port, db=self.db)
self.key_prefix = key_prefix
def lock(self, layer, coord, format):
""" Acquire a cache lock for this tile.
Returns nothing, but blocks until the lock has been acquired.
"""
key = tile_key(layer, coord, format, self.key_prefix) + "-lock"
due = _time() + layer.stale_lock_timeout
while _time() < due:
if self.conn.setnx(key, 'locked.'):
return
_sleep(.2)
self.conn.set(key, 'locked.')
return
def unlock(self, layer, coord, format):
""" Release a cache lock for this tile.
"""
key = tile_key(layer, coord, format, self.key_prefix)
self.conn.delete(key+'-lock')
def remove(self, layer, coord, format):
""" Remove a cached tile.
"""
key = tile_key(layer, coord, format, self.key_prefix)
self.conn.delete(key)
def read(self, layer, coord, format):
""" Read a cached tile.
"""
key = tile_key(layer, coord, format, self.key_prefix)
value = self.conn.get(key)
return value
def save(self, body, layer, coord, format):
""" Save a cached tile.
"""
key = tile_key(layer, coord, format, self.key_prefix)
# note: setting ex=0 will raise an error
cache_lifespan = layer.cache_lifespan
if cache_lifespan == 0:
cache_lifespan = None
self.conn.set(key, body, ex=cache_lifespan)
|