lib/httparty-icebox.rb in httparty-icebox-delooks-0.0.7 vs lib/httparty-icebox.rb in httparty-icebox-delooks-0.0.8
- old
+ new
@@ -163,10 +163,41 @@
end
%w{set get exists? stale?}.each do |method_name|
define_method(method_name) { raise NoMethodError, "Please implement method #{method_name} in your store class" }
end
end
+
+ # ==== Store objects in memory
+ # See HTTParty::Icebox::ClassMethods.cache
+ #
+ class MemcachierStore < AbstractStore
+ def initialize(options={})
+ super; @store = {}; self
+ end
+ def set(key, value)
+ Cache.logger.info("Cache: set (#{key})")
+ Rails.cache.write(key, [Time.now, value]); true
+ end
+ def get(key)
+ data = Rails.cache.read(key)
+ data = data[1] if !data.nil?
+ Cache.logger.info("Cache: #{data.nil? ? "miss" : "hit"} (#{key})")
+ data
+ end
+ def exists?(key)
+ Rails.cache.exist?(key)
+ end
+ def stale?(key)
+ return true unless exists?(key)
+ Time.now - created(key) > @timeout
+ end
+ private
+ def created(key)
+ data = Rails.cache.read(key)
+ data[0]
+ end
+ end
# ==== Store objects in memory
# See HTTParty::Icebox::ClassMethods.cache
#
class MemoryStore < AbstractStore
@@ -185,11 +216,10 @@
def exists?(key)
!@store[key].nil?
end
def stale?(key)
return true unless exists?(key)
- #Time.now - created(key) > @timeout
- false
+ Time.now - created(key) > @timeout
end
private
def created(key)
@store[key][0]
end