From ecee61e32bebf3ac0cc72869187b1aa23e7bb68b Mon Sep 17 00:00:00 2001
From: Kasper Weibel <weibel@gmail.com>
Date: Sat, 20 Apr 2013 19:08:38 +0200
Subject: [PATCH] Pass cache store object on init

---
 examples/autoexpire_cache_dalli.rb | 18 +++++++++---------
 examples/autoexpire_cache_redis.rb |  8 +++++---
 2 files changed, 14 insertions(+), 12 deletions(-)

diff --git a/examples/autoexpire_cache_dalli.rb b/examples/autoexpire_cache_dalli.rb
index 0e51ba2e..16fc77c2 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 6cb53139..1b67a9ee 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
-- 
GitLab