Class: AridCache::CacheProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/arid_cache/cache_proxy.rb

Defined Under Namespace

Classes: Result

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, key, opts = {}, &block) ⇒ CacheProxy

Returns a new instance of CacheProxy.



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/arid_cache/cache_proxy.rb', line 46

def initialize(object, key, opts={}, &block)
  self.object = object
  self.key = key
  self.opts = opts.symbolize_keys
  self.blueprint = AridCache.store.find(object, key)
  self.block = block
  self.records = nil

  # The options from the blueprint merged with the options for this call
  self.combined_options = self.blueprint.nil? ? self.opts : self.blueprint.opts.merge(self.opts)
  self.cache_key = object.arid_cache_key(key, opts_for_cache_key)
end

Instance Attribute Details

#blockObject

Returns the value of attribute block.



3
4
5
# File 'lib/arid_cache/cache_proxy.rb', line 3

def block
  @block
end

#blueprintObject

Returns the value of attribute blueprint.



3
4
5
# File 'lib/arid_cache/cache_proxy.rb', line 3

def blueprint
  @blueprint
end

#cache_keyObject

Returns the value of attribute cache_key.



3
4
5
# File 'lib/arid_cache/cache_proxy.rb', line 3

def cache_key
  @cache_key
end

#cachedObject

Return the cached result for this object’s key



108
109
110
# File 'lib/arid_cache/cache_proxy.rb', line 108

def cached
  @cached
end

#combined_optionsObject

Returns the value of attribute combined_options.



3
4
5
# File 'lib/arid_cache/cache_proxy.rb', line 3

def combined_options
  @combined_options
end

#keyObject

Returns the value of attribute key.



3
4
5
# File 'lib/arid_cache/cache_proxy.rb', line 3

def key
  @key
end

#klassObject

Return the class of the cached results i.e. if the cached result is a list of Album records, then klass returns Album. If there is nothing in the cache, then the class is inferred to be the class of the object that the cached method is being called on.



116
117
118
# File 'lib/arid_cache/cache_proxy.rb', line 116

def klass
  @klass
end

#objectObject

Returns the value of attribute object.



3
4
5
# File 'lib/arid_cache/cache_proxy.rb', line 3

def object
  @object
end

#optsObject

Returns the value of attribute opts.



3
4
5
# File 'lib/arid_cache/cache_proxy.rb', line 3

def opts
  @opts
end

#recordsObject

Returns the value of attribute records.



3
4
5
# File 'lib/arid_cache/cache_proxy.rb', line 3

def records
  @records
end

Class Method Details

.clear_cachesObject

Managing your caches



32
33
34
# File 'lib/arid_cache/cache_proxy.rb', line 32

def self.clear_caches
  Rails.cache.delete_matched(%r[arid-cache-.*])
end

.clear_class_caches(object) ⇒ Object



36
37
38
39
# File 'lib/arid_cache/cache_proxy.rb', line 36

def self.clear_class_caches(object)
  key = (object.is_a?(Class) ? object : object.class).name.downcase + '-'
  Rails.cache.delete_matched(%r[arid-cache-#{key}.*])
end

.clear_instance_caches(object) ⇒ Object



41
42
43
44
# File 'lib/arid_cache/cache_proxy.rb', line 41

def self.clear_instance_caches(object)
  key = (object.is_a?(Class) ? object : object.class).name.pluralize.downcase
  Rails.cache.delete_matched(%r[arid-cache-#{key}.*])
end

Instance Method Details

#clear_cachedObject

Clear the cached result for this cache only



103
104
105
# File 'lib/arid_cache/cache_proxy.rb', line 103

def clear_cached
  Rails.cache.delete(self.cache_key, opts_for_cache)
end

#fetchObject

Return a list of records using the options provided. If the item in the cache is not a CacheProxy::Result it is returned as-is. If there is nothing in the cache the block defining the cache is exectued. If the :raw option is true, returns the CacheProxy::Result unmodified, ignoring other options, except where those options are used to initialize the cache.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/arid_cache/cache_proxy.rb', line 84

def fetch
  @raw_result = opts_for_cache_proxy[:raw] == true

  result = if refresh_cache?
    execute_find(@raw_result)
  elsif cached.is_a?(AridCache::CacheProxy::Result)
    if cached.has_ids? && @raw_result
      self.cached         # return it unmodified
    elsif cached.has_ids?
      fetch_from_cache    # return a list of active records after applying options
    else                  # true if we have only calculated the count thus far
      execute_find(@raw_result)
    end
  else
    cached                # some base type, return it unmodified
  end
end

#fetch_countObject

Return a count of ids in the cache, or return whatever is in the cache if it is not a CacheProxy::Result



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/arid_cache/cache_proxy.rb', line 65

def fetch_count
  if refresh_cache?
    execute_count
  elsif cached.is_a?(AridCache::CacheProxy::Result)
    cached.has_count? ? cached.count : execute_count
  elsif cached.is_a?(Fixnum)
    cached
  elsif cached.respond_to?(:count)
    cached.count
  else
    cached # what else can we do? return it
  end
end