Module: IdentityCache
- Extended by:
- CacheHash
- Defined in:
- lib/identity_cache.rb,
lib/identity_cache/version.rb,
lib/identity_cache/query_api.rb,
lib/identity_cache/cache_hash.rb,
lib/identity_cache/cache_fetcher.rb,
lib/identity_cache/fallback_fetcher.rb,
lib/identity_cache/configuration_dsl.rb,
lib/identity_cache/belongs_to_caching.rb,
lib/identity_cache/cache_invalidation.rb,
lib/identity_cache/cache_key_generation.rb,
lib/identity_cache/memoized_cache_proxy.rb,
lib/identity_cache/parent_model_expiration.rb
Defined Under Namespace
Modules: BelongsToCaching, CacheHash, CacheInvalidation, CacheKeyGeneration, ConfigurationDSL, ParentModelExpiration, QueryAPI Classes: AlreadyIncludedError, AssociationError, CacheFetcher, DerivedModelError, FallbackFetcher, InverseAssociationError, MemoizedCacheProxy, UnsupportedAssociationError, UnsupportedScopeError
Constant Summary collapse
- CACHED_NIL =
:idc_cached_nil
- BATCH_SIZE =
1000
- DELETED =
:idc_cached_deleted
- DELETED_TTL =
1000
- VERSION =
"0.3.1"
- CACHE_VERSION =
6
Class Attribute Summary collapse
- .logger ⇒ Object
-
.readonly ⇒ Object
Returns the value of attribute readonly.
Class Method Summary collapse
- .cache ⇒ Object
-
.cache_backend=(cache_adaptor) ⇒ Object
Sets the cache adaptor IdentityCache will be using.
-
.fetch(key) ⇒ Object
Cache retrieval and miss resolver primitive; given a key it will try to retrieve the associated value from the cache otherwise it will return the value of the execution of the block.
-
.fetch_multi(*keys) ⇒ Object
Same as
fetch
, except that it will try a collection of keys, using the multiget operation of the cache adaptor. -
.included(base) ⇒ Object
:nodoc:.
- .map_cached_nil_for(value) ⇒ Object
-
.should_fill_cache? ⇒ Boolean
:nodoc:.
-
.should_use_cache? ⇒ Boolean
:nodoc:.
- .unmap_cached_nil_for(value) ⇒ Object
Methods included from CacheHash
Class Attribute Details
.logger ⇒ Object
72 73 74 |
# File 'lib/identity_cache.rb', line 72 def logger @logger || Rails.logger end |
.readonly ⇒ Object
Returns the value of attribute readonly.
37 38 39 |
# File 'lib/identity_cache.rb', line 37 def readonly @readonly end |
Class Method Details
.cache ⇒ Object
68 69 70 |
# File 'lib/identity_cache.rb', line 68 def cache @cache ||= MemoizedCacheProxy.new end |
.cache_backend=(cache_adaptor) ⇒ Object
Sets the cache adaptor IdentityCache will be using
Parameters
cache_adaptor
- A ActiveSupport::Cache::Store
60 61 62 63 64 65 66 |
# File 'lib/identity_cache.rb', line 60 def cache_backend=(cache_adaptor) if @cache cache.cache_backend = cache_adaptor else @cache = MemoizedCacheProxy.new(cache_adaptor) end end |
.fetch(key) ⇒ Object
Cache retrieval and miss resolver primitive; given a key it will try to retrieve the associated value from the cache otherwise it will return the value of the execution of the block.
Parameters
key
A cache key string
92 93 94 95 96 97 98 |
# File 'lib/identity_cache.rb', line 92 def fetch(key) if should_use_cache? unmap_cached_nil_for(cache.fetch(key) { map_cached_nil_for yield }) else yield end end |
.fetch_multi(*keys) ⇒ Object
Same as fetch
, except that it will try a collection of keys, using the multiget operation of the cache adaptor.
Parameters
keys
A collection or array of key strings
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/identity_cache.rb', line 113 def fetch_multi(*keys) keys.flatten!(1) return {} if keys.size == 0 result = if should_use_cache? fetch_in_batches(keys.uniq) do |missed_keys| results = yield missed_keys results.map {|e| map_cached_nil_for e } end else results = yield keys Hash[keys.zip(results)] end result.each do |key, value| result[key] = unmap_cached_nil_for(value) end result end |
.included(base) ⇒ Object
:nodoc:
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/identity_cache.rb', line 43 def included(base) #:nodoc: raise AlreadyIncludedError if base.include?(IdentityCache::ConfigurationDSL) base.send(:include, ArTransactionChanges) unless base.include?(ArTransactionChanges) base.send(:include, IdentityCache::BelongsToCaching) base.send(:include, IdentityCache::CacheKeyGeneration) base.send(:include, IdentityCache::ConfigurationDSL) base.send(:include, IdentityCache::QueryAPI) base.send(:include, IdentityCache::CacheInvalidation) end |
.map_cached_nil_for(value) ⇒ Object
100 101 102 |
# File 'lib/identity_cache.rb', line 100 def map_cached_nil_for(value) value.nil? ? IdentityCache::CACHED_NIL : value end |
.should_fill_cache? ⇒ Boolean
:nodoc:
76 77 78 |
# File 'lib/identity_cache.rb', line 76 def should_fill_cache? # :nodoc: !readonly end |
.should_use_cache? ⇒ Boolean
:nodoc:
80 81 82 83 |
# File 'lib/identity_cache.rb', line 80 def should_use_cache? # :nodoc: pool = ActiveRecord::Base.connection_pool !pool.active_connection? || pool.connection.open_transactions == 0 end |
.unmap_cached_nil_for(value) ⇒ Object
104 105 106 |
# File 'lib/identity_cache.rb', line 104 def unmap_cached_nil_for(value) value == IdentityCache::CACHED_NIL ? nil : value end |