From 941515e73f8ae6dabff852dd7cf9603755e95f23 Mon Sep 17 00:00:00 2001 From: Alex Reisner <alex@alexreisner.com> Date: Fri, 28 Sep 2012 12:16:16 -0400 Subject: [PATCH] Move auto-expiring cache to /examples. --- README.rdoc | 35 ++--------------------------------- examples/autoexpire_cache.rb | 30 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 33 deletions(-) create mode 100644 examples/autoexpire_cache.rb diff --git a/README.rdoc b/README.rdoc index 7ba62338..0e319f33 100644 --- a/README.rdoc +++ b/README.rdoc @@ -401,41 +401,10 @@ If you need to expire cached content: Do *not* include the prefix when passing a URL to be expired. Expiring <tt>:all</tt> will only expire keys with the configured prefix (won't kill every entry in your key/value store). -<i>Before you implement caching in your app please be sure that doing so does not violate the Terms of Service for your geocoding service.</i> - -If you want to use Redis as a cache store, with values that expire automatically every 24 hours (or any other TTL), you can do this in the initialize : +For an example of a cache store with URL expiry please see examples/autoexpire_cache.rb - class GeocoderAutoexpireCache - def initialize(store) - @store = store - @ttl = 86400 - end - - def [](url) - @store.[](url) - end - - def []=(url, value) - @store.[]=(url, value) - @store.expire(url, @ttl) - end - - def keys - @store.keys - end - - def del(url) - @store.del(url) - end - end - - Geocoder.configure do |config| - config.cache = GeocoderAutoexpireCache.new(Redis.new) - end - -It's a simple delegation to the Redis store, but when it creates a key/value pair, it is also sending an `EXPIRE` command with a `TTL` +<i>Before you implement caching in your app please be sure that doing so does not violate the Terms of Service for your geocoding service.</i> -It should be fairly simple to do the same thing with `Memcached`. == Forward and Reverse Geocoding in the Same Model diff --git a/examples/autoexpire_cache.rb b/examples/autoexpire_cache.rb new file mode 100644 index 00000000..cace8911 --- /dev/null +++ b/examples/autoexpire_cache.rb @@ -0,0 +1,30 @@ +# This class implements a cache with simple delegation to the Redis store, but +# 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 AutoexpireCache + def initialize(store) + @store = store + @ttl = 86400 + end + + def [](url) + @store.[](url) + end + + def []=(url, value) + @store.[]=(url, value) + @store.expire(url, @ttl) + end + + def keys + @store.keys + end + + def del(url) + @store.del(url) + end +end + +Geocoder.configure do |config| + config.cache = AutoexpireCache.new(Redis.new) +end -- GitLab