Skip to content
Snippets Groups Projects
Commit ecee61e3 authored by Kasper Weibel's avatar Kasper Weibel
Browse files

Pass cache store object on init

parent c2c34aa4
No related branches found
No related tags found
No related merge requests found
...@@ -2,17 +2,12 @@ ...@@ -2,17 +2,12 @@
# https://github.com/mperham/dalli # https://github.com/mperham/dalli
# #
# A TTL is set on initialization # A TTL is set on initialization
# Dalli is set up as on Heroku using the Memcachier gem.
# https://devcenter.heroku.com/articles/memcachier#ruby
# On other setups you might have to specify your Memcached server in Dalli::Client.new
require 'dalli/client'
require 'yaml'
class AutoexpireCacheDalli class AutoexpireCacheDalli
def initialize(ttl = 86400) def initialize(store, ttl = 86400)
@store = store
@keys = 'GeocoderDalliClientKeys' @keys = 'GeocoderDalliClientKeys'
@store = Dalli::Client.new(:expires_in => ttl) @ttl = ttl
end end
def [](url) def [](url)
...@@ -25,7 +20,7 @@ class AutoexpireCacheDalli ...@@ -25,7 +20,7 @@ class AutoexpireCacheDalli
if value.nil? if value.nil?
del(url) del(url)
else else
key_cache_add(url) if @store.add(key, YAML::dump(value)) key_cache_add(url) if @store.add(key, YAML::dump(value), @ttl)
end end
value value
end end
...@@ -60,3 +55,8 @@ class AutoexpireCacheDalli ...@@ -60,3 +55,8 @@ class AutoexpireCacheDalli
@store.replace(@keys, YAML::dump(tmp)) @store.replace(@keys, YAML::dump(tmp))
end end
end end
# Here Dalli is set up as on Heroku using the Memcachier gem.
# https://devcenter.heroku.com/articles/memcachier#ruby
# On other setups you might have to specify your Memcached server in Dalli::Client.new
Geocoder.configure(:cache => AutoexpireCacheDalli.new(Dalli::Client.new))
...@@ -2,8 +2,8 @@ ...@@ -2,8 +2,8 @@
# when it creates a key/value pair, it also sends an EXPIRE command with a TTL. # when it creates a key/value pair, it also sends an EXPIRE command with a TTL.
# It should be fairly simple to do the same thing with Memcached. # It should be fairly simple to do the same thing with Memcached.
class AutoexpireCacheRedis class AutoexpireCacheRedis
def initialize(ttl = 86400) def initialize(store, ttl = 86400)
@store = Redis.new @store = store
@ttl = ttl @ttl = ttl
end end
...@@ -24,3 +24,5 @@ class AutoexpireCacheRedis ...@@ -24,3 +24,5 @@ class AutoexpireCacheRedis
@store.del(url) @store.del(url)
end end
end end
Geocoder.configure(:cache => AutoexpireCacheRedis.new(Redis.new))
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment