diff --git a/examples/autoexpire_cache_dalli.rb b/examples/autoexpire_cache_dalli.rb index 0e51ba2e9548816fa43048cdffc2cecf274e17b0..16fc77c2f5d736af39ff52711cee5257c559a715 100644 --- a/examples/autoexpire_cache_dalli.rb +++ b/examples/autoexpire_cache_dalli.rb @@ -2,17 +2,12 @@ # https://github.com/mperham/dalli # # 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 - def initialize(ttl = 86400) + def initialize(store, ttl = 86400) + @store = store @keys = 'GeocoderDalliClientKeys' - @store = Dalli::Client.new(:expires_in => ttl) + @ttl = ttl end def [](url) @@ -25,7 +20,7 @@ class AutoexpireCacheDalli if value.nil? del(url) 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 value end @@ -60,3 +55,8 @@ class AutoexpireCacheDalli @store.replace(@keys, YAML::dump(tmp)) 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)) diff --git a/examples/autoexpire_cache_redis.rb b/examples/autoexpire_cache_redis.rb index 6cb53139bdc42cf0964537e7d0fefabc18b2436f..1b67a9eee0febd18c6ba6c11cb35a3acfce18f2b 100644 --- a/examples/autoexpire_cache_redis.rb +++ b/examples/autoexpire_cache_redis.rb @@ -2,8 +2,8 @@ # 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. class AutoexpireCacheRedis - def initialize(ttl = 86400) - @store = Redis.new + def initialize(store, ttl = 86400) + @store = store @ttl = ttl end @@ -23,4 +23,6 @@ class AutoexpireCacheRedis def del(url) @store.del(url) end -end \ No newline at end of file +end + +Geocoder.configure(:cache => AutoexpireCacheRedis.new(Redis.new)) \ No newline at end of file