Class: Gravatar::Cache

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(real_cache, duration, namespace = nil, logger = Gravatar.logger) ⇒ Cache

Returns a new instance of Cache.



12
13
14
15
16
17
18
# File 'lib/gravatar/cache.rb', line 12

def initialize(real_cache, duration, namespace = nil, logger = Gravatar.logger)
  @duration = duration
  @real_cache = real_cache
  @namespace = namespace
  @logger = logger
  @rescue_errors = true
end

Instance Attribute Details

#durationObject

Returns the value of attribute duration.



5
6
7
# File 'lib/gravatar/cache.rb', line 5

def duration
  @duration
end

#loggerObject

Returns the value of attribute logger.



5
6
7
# File 'lib/gravatar/cache.rb', line 5

def logger
  @logger
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



4
5
6
# File 'lib/gravatar/cache.rb', line 4

def namespace
  @namespace
end

#real_cacheObject (readonly)

Returns the value of attribute real_cache.



4
5
6
# File 'lib/gravatar/cache.rb', line 4

def real_cache
  @real_cache
end

#rescue_errorsObject

If true, any errors encountered while communicating with the server will be rescued and the error message will be written to #logger, then the cached copy of the result (if any) will be returned. If false, the error will be raised.



10
11
12
# File 'lib/gravatar/cache.rb', line 10

def rescue_errors
  @rescue_errors
end

Instance Method Details

#cache_key(*args) ⇒ Object

Constructs a cache key from the specified *args and @namespace.



80
81
82
# File 'lib/gravatar/cache.rb', line 80

def cache_key(*args)
  ActiveSupport::Cache.expand_cache_key(args, @namespace)
end

#cached(*key) ⇒ Object



69
70
71
72
# File 'lib/gravatar/cache.rb', line 69

def cached(*key)
  copy = read_cache(*key)
  copy &&= copy[:object]
end

#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.

Example:

cache = Gravatar::Cache.new(Rails.cache, 30.minutes)
cache.call(:first_name => "Colin", :last_name => "MacKenzie") { call_webservice(with_some_args) }


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/gravatar/cache.rb', line 27

def call(*key, &block)
  cached_copy = read_cache(*key)
  cached_copy &&= cached_copy[:object]
  
  if expired?(*key) && block_given?
    begin
      yield.tap do |object|
        write_cache(object, *key)
      end
    rescue
      log_error $!
      cached_copy
    end
  else
    cached_copy
  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.



47
48
49
# File 'lib/gravatar/cache.rb', line 47

def clear!
  @real_cache.delete_matched(/^#{Regexp::escape @namespace}/)
end

#expire!(*key) ⇒ Object

forces the specified key to become expired



52
53
54
55
56
# File 'lib/gravatar/cache.rb', line 52

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.

Returns:

  • (Boolean)


59
60
61
62
# File 'lib/gravatar/cache.rb', line 59

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.



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/gravatar/cache.rb', line 86

def log_error(error)
  raise error unless rescue_errors
  if logger.respond_to?(:error)
    logger.error error.message
    error.backtrace.each { |line| logger.error "  #{line}" }
  elsif logger.respond_to?(:write)
    logger.write(([error.message] + error.backtrace).join("\n  ") + "\n")
  else
    raise error
  end
end

#read_cache(*key) ⇒ Object

Reads an object from the cache based on the cache key constructed from *key.



65
66
67
# File 'lib/gravatar/cache.rb', line 65

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.



75
76
77
# File 'lib/gravatar/cache.rb', line 75

def write_cache(object, *key)
  @real_cache.write(cache_key(*key), { :expires_at => Time.now + duration, :object => object })
end