Class: AridCache::CacheProxy
- Inherits:
-
Object
- Object
- AridCache::CacheProxy
- Defined in:
- lib/arid_cache/cache_proxy.rb
Defined Under Namespace
Classes: Result
Instance Attribute Summary collapse
-
#block ⇒ Object
Returns the value of attribute block.
-
#blueprint ⇒ Object
Returns the value of attribute blueprint.
-
#cache_key ⇒ Object
Returns the value of attribute cache_key.
-
#cached ⇒ Object
Return the cached result for this object’s key.
-
#combined_options ⇒ Object
Returns the value of attribute combined_options.
-
#key ⇒ Object
Returns the value of attribute key.
-
#klass ⇒ Object
Return the class of the cached results i.e.
-
#object ⇒ Object
Returns the value of attribute object.
-
#opts ⇒ Object
Returns the value of attribute opts.
-
#records ⇒ Object
Returns the value of attribute records.
Class Method Summary collapse
-
.clear_caches ⇒ Object
Managing your caches.
- .clear_class_caches(object) ⇒ Object
- .clear_instance_caches(object) ⇒ Object
Instance Method Summary collapse
-
#clear_cached ⇒ Object
Clear the cached result for this cache only.
-
#fetch ⇒ Object
Return a list of records using the options provided.
-
#fetch_count ⇒ Object
Return a count of ids in the cache, or return whatever is in the cache if it is not a CacheProxy::Result.
-
#initialize(object, key, opts = {}, &block) ⇒ CacheProxy
constructor
A new instance of CacheProxy.
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. = 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
#block ⇒ Object
Returns the value of attribute block.
3 4 5 |
# File 'lib/arid_cache/cache_proxy.rb', line 3 def block @block end |
#blueprint ⇒ Object
Returns the value of attribute blueprint.
3 4 5 |
# File 'lib/arid_cache/cache_proxy.rb', line 3 def blueprint @blueprint end |
#cache_key ⇒ Object
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 |
#cached ⇒ Object
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_options ⇒ Object
Returns the value of attribute combined_options.
3 4 5 |
# File 'lib/arid_cache/cache_proxy.rb', line 3 def @combined_options end |
#key ⇒ Object
Returns the value of attribute key.
3 4 5 |
# File 'lib/arid_cache/cache_proxy.rb', line 3 def key @key end |
#klass ⇒ Object
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 |
#object ⇒ Object
Returns the value of attribute object.
3 4 5 |
# File 'lib/arid_cache/cache_proxy.rb', line 3 def object @object end |
#opts ⇒ Object
Returns the value of attribute opts.
3 4 5 |
# File 'lib/arid_cache/cache_proxy.rb', line 3 def opts @opts end |
#records ⇒ Object
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_caches ⇒ Object
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_cached ⇒ Object
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 |
#fetch ⇒ Object
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_count ⇒ Object
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 |