Class: AridCache::CacheProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/arid_cache/cache_proxy.rb,
lib/arid_cache/cache_proxy/options.rb,
lib/arid_cache/cache_proxy/utilities.rb,
lib/arid_cache/cache_proxy/result_processor.rb

Defined Under Namespace

Modules: Utilities Classes: CachedResult, Options, ResultProcessor

Constant Summary collapse

OPTIONS_FOR_PAGINATE =
[:page, :per_page, :total_entries, :finder]
OPTIONS_FOR_CACHE_PROXY =
[:raw, :clear]
OPTIONS_FOR_FIND =
[ :conditions, :where, :include, :includes, :joins, :limit, :offset, :order, :select, :readonly, :group, :having, :from, :lock ]
OPTIONS_FOR_CACHE =
[ :expires_in ]
OPTIONS_FOR_CACHE_KEY =
[ :auto_expire ]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(receiver, method, opts = {}, &block) ⇒ CacheProxy

Initialize



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

def initialize(receiver, method, opts={}, &block)
  @receiver = receiver
  @method = method
  @block = block
  @blueprint = AridCache.store.find(@receiver, @method)

  # Combine the options from the blueprint with the options for this call
  opts = opts.symbolize_keys
  @options = Options.new(@blueprint.nil? ? opts : @blueprint.opts.merge(opts))
  @options[:receiver_klass] = Utilities.object_class(receiver)
  @cache_key = @receiver.arid_cache_key(@method, @options.opts_for_cache_key)
end

Class Method Details

.clear_cachesObject

Managing your caches



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

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

.clear_class_caches(object) ⇒ Object



44
45
46
47
# File 'lib/arid_cache/cache_proxy.rb', line 44

def self.clear_class_caches(object)
  key = (Utilities.object_class(object)).name.downcase + '-'
  Rails.cache.delete_matched(%r[arid-cache-#{key}.*])
end

.clear_instance_caches(object) ⇒ Object



49
50
51
52
# File 'lib/arid_cache/cache_proxy.rb', line 49

def self.clear_instance_caches(object)
  key = AridCache::Inflector.pluralize((Utilities.object_class(object)).name).downcase
  Rails.cache.delete_matched(%r[arid-cache-#{key}.*])
end

Instance Method Details

#clear_cachedObject

Clear the cached result for this cache only



55
56
57
# File 'lib/arid_cache/cache_proxy.rb', line 55

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

#fetchObject

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



92
93
94
# File 'lib/arid_cache/cache_proxy.rb', line 92

def fetch
  result_processor.to_result
end

#fetch_countObject

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



82
83
84
85
# File 'lib/arid_cache/cache_proxy.rb', line 82

def fetch_count
  @options[:count_only] = true
  result_processor.to_result
end