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/configuration_dsl.rb,
lib/identity_cache/belongs_to_caching.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, CacheKeyGeneration, ConfigurationDSL, ParentModelExpiration, QueryAPI Classes: AlreadyIncludedError, InverseAssociationError, MemoizedCacheProxy

Constant Summary collapse

CACHED_NIL =
:idc_cached_nil
VERSION =
"0.0.5"
CACHE_VERSION =
3

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.logger ⇒ Object



61
62
63
# File 'lib/identity_cache.rb', line 61

def logger
  @logger || Rails.logger
end

.readonly ⇒ Object

Returns the value of attribute readonly.



27
28
29
# File 'lib/identity_cache.rb', line 27

def readonly
  @readonly
end

Class Method Details

.cache ⇒ Object



57
58
59
# File 'lib/identity_cache.rb', line 57

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



49
50
51
52
53
54
55
# File 'lib/identity_cache.rb', line 49

def cache_backend=(cache_adaptor)
  if @cache
    cache.cache_backend = cache_adaptor
  else
    @cache = MemoizedCacheProxy.new(cache_adaptor)
  end
end

.fetch(key, &block) ⇒ 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



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/identity_cache.rb', line 76

def fetch(key, &block)
  result = cache.read(key) if should_cache?

  if result.nil?
    if block_given?
      result = yield
      result = map_cached_nil_for(result)

      if should_cache?
        cache.write(key, result)
      end
    end
  end

  unmap_cached_nil_for(result)
end

.fetch_multi(*keys, &block) ⇒ 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 of key strings



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/identity_cache.rb', line 106

def fetch_multi(*keys, &block)
  return {} if keys.size == 0
  result = {}
  result = cache.read_multi(*keys) if should_cache?

  hit_keys = result.select {|key, value| value.present? }.keys
  missed_keys = keys - hit_keys

  if missed_keys.size > 0
    if block_given?
      replacement_results = nil
      replacement_results = yield missed_keys
      missed_keys.zip(replacement_results) do |(key, replacement_result)|
        if should_cache?
          replacement_result  = map_cached_nil_for(replacement_result )
          cache.write(key, replacement_result)
          logger.debug { "[IdentityCache] cache miss for #{key} (multi)" }
        end
        result[key] = replacement_result
      end
    end
  end


  result.keys.each do |key|
    result[key] = unmap_cached_nil_for(result[key])
  end

  result
end

.included(base) ⇒ Object

:nodoc:



33
34
35
36
37
38
39
40
41
# File 'lib/identity_cache.rb', line 33

def included(base) #:nodoc:
  raise AlreadyIncludedError if base.respond_to? :cache_indexes

  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)
end

.map_cached_nil_for(value) ⇒ Object



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

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

.should_cache? ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


65
66
67
# File 'lib/identity_cache.rb', line 65

def should_cache? # :nodoc:
  !readonly && ActiveRecord::Base.connection.open_transactions == 0
end

.unmap_cached_nil_for(value) ⇒ Object



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

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