lib/object/cache.rb in object-cache-0.0.2 vs lib/object/cache.rb in object-cache-0.0.3

- old
+ new

@@ -8,10 +8,11 @@ @default_ttl = (7 * 24 * 60 * 60) # 1 week class << self attr_accessor :backend attr_accessor :default_ttl + attr_accessor :default_key_prefix # 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. @@ -41,14 +42,14 @@ # 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, key_prefix: default_key_prefix) return yield unless replica - key = Digest::SHA1.hexdigest([key, Proc.new.source_location].flatten.join)[0..5] + key = build_key(key, key_prefix, Proc.new) if (cached_value = replica.get(key)).nil? yield.tap { |value| update_cache(key, value, ttl: ttl) } else Marshal.load(cached_value) @@ -91,8 +92,27 @@ [backend.is_a?(Hash) ? backend[:replicas] : backend].flatten end def replica replicas.sample + end + + def build_key(key, key_prefix, proc) + hash = Digest::SHA1.hexdigest([key, proc.source_location].flatten.join)[0..5] + prefix = build_key_prefix(key_prefix, proc) + + [prefix, hash].compact.join('_') + end + + def build_key_prefix(key_prefix, proc) + case key_prefix + when :method_name + location = caller_locations.find { |l| "#{l.path}#{l.lineno}" == proc.source_location.join } + location&.base_label + when :class_name + proc.binding.receiver.class.to_s + else + key_prefix + end end end end