-
Notifications
You must be signed in to change notification settings - Fork 173
Expand file tree
/
Copy pathidentity_cache.rb
More file actions
173 lines (145 loc) · 4.93 KB
/
identity_cache.rb
File metadata and controls
173 lines (145 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
require 'active_record'
require 'active_support/core_ext/module/attribute_accessors'
require 'ar_transaction_changes'
require "identity_cache/version"
require 'identity_cache/memoized_cache_proxy'
require 'identity_cache/belongs_to_caching'
require 'identity_cache/cache_key_generation'
require 'identity_cache/configuration_dsl'
require 'identity_cache/should_use_cache'
require 'identity_cache/parent_model_expiration'
require 'identity_cache/query_api'
require "identity_cache/cache_hash"
require "identity_cache/cache_invalidation"
require "identity_cache/cache_fetcher"
require "identity_cache/fallback_fetcher"
require 'identity_cache/without_primary_index'
module IdentityCache
extend ActiveSupport::Concern
include ArTransactionChanges
include IdentityCache::BelongsToCaching
include IdentityCache::CacheKeyGeneration
include IdentityCache::ConfigurationDSL
include IdentityCache::QueryAPI
include IdentityCache::CacheInvalidation
include IdentityCache::ShouldUseCache
include ParentModelExpiration
CACHED_NIL = :idc_cached_nil
BATCH_SIZE = 1000
DELETED = :idc_cached_deleted
DELETED_TTL = 1000
class AlreadyIncludedError < StandardError; end
class AssociationError < StandardError; end
class InverseAssociationError < StandardError; end
class UnsupportedScopeError < StandardError; end
class UnsupportedAssociationError < StandardError; end
class DerivedModelError < StandardError; end
class << self
include IdentityCache::CacheHash
attr_accessor :readonly
attr_writer :logger
mattr_accessor :cache_namespace
self.cache_namespace = "IDC:#{CACHE_VERSION}:".freeze
# Fetched records are not read-only and this could sometimes prevent IDC from
# reflecting what's truly in the database when fetch_read_only_records is false.
# When set to true, it will only return read-only records when cache is used.
mattr_accessor :fetch_read_only_records
self.fetch_read_only_records = true
# Raise an exception if the record returned from the cache don't match the id
# the user expected
mattr_accessor :raise_on_id_mismatch
self.raise_on_id_mismatch = false
mattr_accessor :lazy_load_associated_classes
self.lazy_load_associated_classes = Gem::Version.new(IdentityCache::VERSION) >= Gem::Version.new("0.6")
def included(base) #:nodoc:
raise AlreadyIncludedError if base.respond_to?(:cached_model)
base.class_attribute :cached_model
base.cached_model = base
super
end
# Sets the cache adaptor IdentityCache will be using
#
# == Parameters
#
# +cache_adaptor+ - A ActiveSupport::Cache::Store
#
def cache_backend=(cache_adaptor)
if defined?(@cache)
cache.cache_backend = cache_adaptor
else
@cache = MemoizedCacheProxy.new(cache_adaptor)
end
end
def cache
@cache ||= MemoizedCacheProxy.new
end
def logger
@logger || Rails.logger
end
def should_fill_cache? # :nodoc:
!readonly
end
def should_use_cache? # :nodoc:
pool = ActiveRecord::Base.connection_pool
!pool.active_connection? || pool.connection.open_transactions == 0
end
# 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
#
def fetch(key)
if should_use_cache?
unmap_cached_nil_for(cache.fetch(key) { map_cached_nil_for yield })
else
yield
end
end
def map_cached_nil_for(value)
value.nil? ? IdentityCache::CACHED_NIL : value
end
def unmap_cached_nil_for(value)
value == IdentityCache::CACHED_NIL ? nil : value
end
# 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
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
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
def eager_load!
ParentModelExpiration.install_all_pending_parent_expiry_hooks
end
private
def fetch_in_batches(keys)
keys.each_slice(BATCH_SIZE).each_with_object Hash.new do |slice, result|
result.merge! cache.fetch_multi(*slice) {|missed_keys| yield missed_keys }
end
end
end
end