Class: Tairu::Cache::RedisCache

Inherits:
Object
  • Object
show all
Defined in:
lib/tairu/cache/redis_cache.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {'host' => 'localhost', 'port' => 6379, 'db' => 0}) ⇒ RedisCache

Returns a new instance of RedisCache.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/tairu/cache/redis_cache.rb', line 11

def initialize(options={'host' => 'localhost', 'port' => 6379, 'db' => 0})
  if options['url']
    redis_url = options['url']
  else
    db = options['db'] || 0
    redis_url = "redis://#{options['host']}:#{options['port']}/#{db}"
  end

  @pool = ConnectionPool::Wrapper.new(timeout: 1, size: 4) do
    Redis.connect(url: redis_url, driver: 'ruby')
  end
end

Instance Method Details

#add(name, coord, tile, age = 300) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/tairu/cache/redis_cache.rb', line 24

def add(name, coord, tile, age=300)
  key = "#{name}_#{coord.zoom}_#{coord.column}_#{coord.row}"
  expire = Time.now.to_i + age
  tile = Base64.encode64(tile.data)
  @pool.set(key, MultiJson.dump({tile: tile, format: Tairu.config.layers[name]['format']}))
  @pool.expire(key, expire)
end

#get(name, coord) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/tairu/cache/redis_cache.rb', line 32

def get(name, coord)
  key = "#{name}_#{coord.zoom}_#{coord.column}_#{coord.row}"
  value = @pool.get(key)

  if value.nil?
    return nil
  else
    tile_hash = MultiJson.load(value)
    Tairu::Tile.new(Base64.decode64(tile_hash['tile']), tile_hash['format'])
  end
end