lib/object/cache.rb in object-cache-0.0.1 vs lib/object/cache.rb in object-cache-0.0.2
- old
+ new
@@ -3,14 +3,15 @@
require 'digest/sha1'
require 'object/cache/version'
# Caching of objects in a Redis store
class Cache
- DEFAULT_TTL = (7 * 24 * 60 * 60) # 1 week
+ @default_ttl = (7 * 24 * 60 * 60) # 1 week
class << self
attr_accessor :backend
+ attr_accessor :default_ttl
# new
#
# Finds the correct value (based on the provided key) in the cache store, or
# calls the original code, and stores the result in cache.
@@ -40,11 +41,11 @@
# bad:
#
# Cache.new { item } # item is only stored once, and then always
# # retrieved, even if it is a different item
#
- def new(key = nil, ttl: DEFAULT_TTL)
+ def new(key = nil, ttl: default_ttl)
return yield unless replica
key = Digest::SHA1.hexdigest([key, Proc.new.source_location].flatten.join)[0..5]
if (cached_value = replica.get(key)).nil?
@@ -74,10 +75,10 @@
primary.del(key)
true
end
- def update_cache(key, value, ttl: DEFAULT_TTL)
+ def update_cache(key, value, ttl: default_ttl)
return unless primary && (value = Marshal.dump(value))
ttl.to_i.zero? ? primary.set(key, value) : primary.setex(key, ttl.to_i, value)
end