Module: IdentityCache

Extended by:
ActiveSupport::Concern, CacheHash
Includes:
ArTransactionChanges, BelongsToCaching, CacheInvalidation, CacheKeyGeneration, ConfigurationDSL, ParentModelExpiration, QueryAPI, ShouldUseCache
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/should_use_cache.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/without_primary_index.rb,
lib/identity_cache/parent_model_expiration.rb

Defined Under Namespace

Modules: BelongsToCaching, CacheHash, CacheInvalidation, CacheKeyGeneration, ConfigurationDSL, ParentModelExpiration, QueryAPI, ShouldUseCache, WithoutPrimaryIndex 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.5.1"
CACHE_VERSION =
7

Constants included from CacheInvalidation

CacheInvalidation::CACHE_KEY_NAMES

Constants included from CacheKeyGeneration

CacheKeyGeneration::DEFAULT_NAMESPACE

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from CacheHash

memcache_hash

Methods included from ParentModelExpiration

#add_parents_to_cache_expiry_set, #add_record_to_cache_expiry_set, #expire_parent_caches, #parents_to_expire_on_changes, #should_expire_identity_cache_parent?

Methods included from CacheInvalidation

#reload

Methods included from CacheKeyGeneration

#attribute_cache_key_for_attribute_and_current_values, #attribute_cache_key_for_attribute_and_previous_values, #current_values_for_fields, denormalized_schema_hash, #old_values_for_fields, #primary_cache_index_key, schema_to_string

Class Attribute Details

.loggerObject



93
94
95
# File 'lib/identity_cache.rb', line 93

def logger
  @logger || Rails.logger
end

.readonlyObject

Returns the value of attribute readonly.



50
51
52
# File 'lib/identity_cache.rb', line 50

def readonly
  @readonly
end

Class Method Details

.cacheObject



89
90
91
# File 'lib/identity_cache.rb', line 89

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



81
82
83
84
85
86
87
# File 'lib/identity_cache.rb', line 81

def cache_backend=(cache_adaptor)
  if defined?(@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



113
114
115
116
117
118
119
# File 'lib/identity_cache.rb', line 113

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



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/identity_cache.rb', line 134

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:



68
69
70
71
72
73
# File 'lib/identity_cache.rb', line 68

def included(base) #:nodoc:
  raise AlreadyIncludedError if base.respond_to?(:cached_model)
  base.class_attribute :cached_model
  base.cached_model = base
  super
end

.map_cached_nil_for(value) ⇒ Object



121
122
123
# File 'lib/identity_cache.rb', line 121

def map_cached_nil_for(value)
  value.nil? ? IdentityCache::CACHED_NIL : value
end

.should_fill_cache?Boolean

:nodoc:

Returns:

  • (Boolean)


97
98
99
# File 'lib/identity_cache.rb', line 97

def should_fill_cache? # :nodoc:
  !readonly
end

.should_use_cache?Boolean

:nodoc:

Returns:

  • (Boolean)


101
102
103
104
# File 'lib/identity_cache.rb', line 101

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



125
126
127
# File 'lib/identity_cache.rb', line 125

def unmap_cached_nil_for(value)
  value == IdentityCache::CACHED_NIL ? nil : value
end

.with_fetch_read_only_records(value = true) ⇒ Object



164
165
166
167
168
169
170
# File 'lib/identity_cache.rb', line 164

def with_fetch_read_only_records(value = true)
  old_value = self.fetch_read_only_records
  self.fetch_read_only_records = value
  yield
ensure
  self.fetch_read_only_records = old_value
end

.with_never_set_inverse_association(value = true) ⇒ Object



155
156
157
158
159
160
161
# File 'lib/identity_cache.rb', line 155

def with_never_set_inverse_association(value = true)
  old_value = self.never_set_inverse_association
  self.never_set_inverse_association = value
  yield
ensure
  self.never_set_inverse_association = old_value
end