Class: Gravatar::Cache
- Inherits:
-
Object
- Object
- Gravatar::Cache
- Defined in:
- lib/gravatar/cache.rb
Overview
A wrapper around any given Cache object which provides Gravatar-specific helpers. Used internally.
Instance Attribute Summary collapse
-
#duration ⇒ Object
Returns the value of attribute duration.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#namespace ⇒ Object
readonly
Returns the value of attribute namespace.
-
#real_cache ⇒ Object
readonly
Returns the value of attribute real_cache.
Instance Method Summary collapse
-
#cache_key(*args) ⇒ Object
Constructs a cache key from the specified *args and @namespace.
-
#call(*key, &block) ⇒ Object
Provide a series of arguments to be used as a cache key, and a block to be executed when the cache is expired or needs to be populated.
-
#clear! ⇒ Object
Clears out the entire cache for this object’s namespace.
-
#expire!(*key) ⇒ Object
forces the specified key to become expired.
-
#expired?(*key) ⇒ Boolean
Returns true if the cached copy is nil or expired based on @duration.
-
#initialize(real_cache, duration, namespace = nil, logger = Gravatar.logger) ⇒ Cache
constructor
A new instance of Cache.
-
#log_error(error) ⇒ Object
Logs an error message, as long as self.logger responds to :error or :write.
-
#read_cache(*key) ⇒ Object
Reads an object from the cache based on the cache key constructed from *key.
-
#write_cache(object, *key) ⇒ Object
Writes an object to the cache based on th cache key constructed from *key.
Constructor Details
#initialize(real_cache, duration, namespace = nil, logger = Gravatar.logger) ⇒ Cache
Returns a new instance of Cache.
7 8 9 10 11 12 |
# File 'lib/gravatar/cache.rb', line 7 def initialize(real_cache, duration, namespace = nil, logger = Gravatar.logger) @duration = duration @real_cache = real_cache @namespace = namespace @logger = logger end |
Instance Attribute Details
#duration ⇒ Object
Returns the value of attribute duration.
5 6 7 |
# File 'lib/gravatar/cache.rb', line 5 def duration @duration end |
#logger ⇒ Object
Returns the value of attribute logger.
5 6 7 |
# File 'lib/gravatar/cache.rb', line 5 def logger @logger end |
#namespace ⇒ Object (readonly)
Returns the value of attribute namespace.
4 5 6 |
# File 'lib/gravatar/cache.rb', line 4 def namespace @namespace end |
#real_cache ⇒ Object (readonly)
Returns the value of attribute real_cache.
4 5 6 |
# File 'lib/gravatar/cache.rb', line 4 def real_cache @real_cache end |
Instance Method Details
#cache_key(*args) ⇒ Object
Constructs a cache key from the specified *args and @namespace.
67 68 69 |
# File 'lib/gravatar/cache.rb', line 67 def cache_key(*args) ActiveSupport::Cache.(args, @namespace) end |
#call(*key, &block) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/gravatar/cache.rb', line 21 def call(*key, &block) cached_copy = read_cache(*key) if expired?(*key) && block_given? begin returning(yield) do |object| write_cache(object, *key) end rescue log_error($!) cached_copy.nil? ? nil : cached_copy[:object] end else cached_copy.nil? ? nil : cached_copy[:object] end end |
#clear! ⇒ Object
Clears out the entire cache for this object’s namespace. This actually removes the objects, instead of simply marking them as expired, so it will be as if the object never existed.
39 40 41 |
# File 'lib/gravatar/cache.rb', line 39 def clear! @real_cache.delete_matched(/^#{Regexp::escape @namespace}/) end |
#expire!(*key) ⇒ Object
forces the specified key to become expired
44 45 46 47 48 |
# File 'lib/gravatar/cache.rb', line 44 def expire!(*key) unless expired?(*key) @real_cache.write(cache_key(*key), { :expires_at => 1.minute.ago, :object => read_cache(*key)[:object] }) end end |
#expired?(*key) ⇒ Boolean
Returns true if the cached copy is nil or expired based on @duration.
51 52 53 54 |
# File 'lib/gravatar/cache.rb', line 51 def expired?(*key) cached_copy = read_cache(*key) cached_copy.nil? || cached_copy[:expires_at] < Time.now end |
#log_error(error) ⇒ Object
Logs an error message, as long as self.logger responds to :error or :write. Otherwise, re-raises the error.
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/gravatar/cache.rb', line 73 def log_error(error) if logger.respond_to?(:error) logger.error error. error.backtrace.each { |line| logger.error " #{line}" } elsif logger.respond_to?(:write) logger.write(([error.]+error.backtrace).join("\n ")) logger.write("\n") else raise error end end |
#read_cache(*key) ⇒ Object
Reads an object from the cache based on the cache key constructed from *key.
57 58 59 |
# File 'lib/gravatar/cache.rb', line 57 def read_cache(*key) @real_cache.read(cache_key(*key)) end |
#write_cache(object, *key) ⇒ Object
Writes an object to the cache based on th cache key constructed from *key.
62 63 64 |
# File 'lib/gravatar/cache.rb', line 62 def write_cache(object, *key) @real_cache.write(cache_key(*key), { :expires_at => Time.now + duration, :object => object }) end |