-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy pathidentity_cache.rb
More file actions
333 lines (281 loc) · 10.9 KB
/
Copy pathidentity_cache.rb
File metadata and controls
333 lines (281 loc) · 10.9 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# frozen_string_literal: true
require "active_record"
require "active_support/core_ext/module/attribute_accessors"
require "ar_transaction_changes"
require "identity_cache/version"
require "identity_cache/record_not_found"
require "identity_cache/encoder"
require "identity_cache/cache_key_loader"
require "identity_cache/load_strategy/load_request"
require "identity_cache/load_strategy/multi_load_request"
require "identity_cache/load_strategy/eager"
require "identity_cache/load_strategy/lazy"
require "identity_cache/cached"
require "identity_cache/cached/prefetcher"
require "identity_cache/cached/embedded_fetching"
require "identity_cache/cached/association"
require "identity_cache/cached/attribute"
require "identity_cache/cached/attribute_by_one"
require "identity_cache/cached/attribute_by_multi"
require "identity_cache/cached/belongs_to"
require "identity_cache/cached/primary_index"
require "identity_cache/cached/recursive/association"
require "identity_cache/cached/recursive/has_one"
require "identity_cache/cached/recursive/has_many"
require "identity_cache/cached/reference/association"
require "identity_cache/cached/reference/has_one"
require "identity_cache/cached/reference/has_many"
require "identity_cache/expiry_hook"
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"
require "identity_cache/with_primary_index"
module IdentityCache
extend ActiveSupport::Concern
autoload :MemCacheStoreCAS, "identity_cache/mem_cache_store_cas"
include WithPrimaryIndex
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 NestedDeferredParentBlockError < StandardError; end
class NestedDeferredCacheExpirationBlockError < StandardError; end
class UnsupportedScopeError < StandardError; end
class UnsupportedAssociationError < StandardError; end
class DerivedModelError < StandardError; end
class LockWaitTimeout < StandardError; end
mattr_accessor :cache_namespace
self.cache_namespace = "IDC:#{CACHE_VERSION}:"
# 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.
@fetch_read_only_records = true
class << self
include IdentityCache::CacheHash
attr_writer :fetch_read_only_records
attr_accessor :readonly
attr_writer :logger
def append_features(base) # :nodoc:
raise AlreadyIncludedError if base.include?(IdentityCache)
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
# Rails wraps each of your tests in a transaction, so that any changes
# made to the database during the test can be rolled back afterwards.
# These transactions are flagged as "unjoinable", which tries to make
# your application behave as if they weren't there. In particular:
#
# - Opening another transaction during the test creates a savepoint,
# which can be rolled back independently of the main transaction.
# - When those nested transactions complete, any `after_commit`
# callbacks for records modified during the transaction will run,
# even though the changes haven't actually been committed yet.
#
# By ignoring unjoinable transactions, IdentityCache's behaviour
# during your test suite will more closely match production.
#
# When there are no open transactions, `current_transaction` returns a
# special `NullTransaction` object that is unjoinable, meaning we will
# use the cache.
if ActiveRecord::ConnectionAdapters::ConnectionPool.method_defined?(:active_connection)
def should_use_cache? # :nodoc:
ActiveRecord::Base.connection_handler.connection_pool_list(ActiveRecord::Base.current_role).none? do |pool|
pool.active_connection? &&
pool.active_connection.current_transaction.joinable?
end
end
else
def should_use_cache? # :nodoc:
ActiveRecord::Base.connection_handler.connection_pool_list(ActiveRecord::Base.current_role).none? do |pool|
pool.active_connection? &&
pool.connection.current_transaction.joinable?
end
end
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
# +cache_fetcher_options+ A hash of options to pass to the cache backend
#
def fetch(key, cache_fetcher_options = {})
if should_use_cache?
unmap_cached_nil_for(cache.fetch(key, cache_fetcher_options) do
map_cached_nil_for(yield)
end)
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.empty?
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
# Executes a block with deferred parent expiration, ensuring that the parent
# records' cache expiration is deferred until the block completes. When the block
# completes, it triggers expiration of the primary index for the parent records.
# Raises a NestedDeferredParentBlockError if a deferred parent expiration block
# is already active on the current thread.
#
# == Parameters:
# No parameters.
#
# == Raises:
# NestedDeferredParentBlockError if a deferred parent expiration block is already active.
#
# == Yield:
# Runs the provided block with deferred parent expiration.
#
# == Returns:
# The result of executing the provided block.
#
# == Ensures:
# Cleans up thread-local variables related to deferred parent expiration regardless
# of whether the block raises an exception.
def with_deferred_parent_expiration
raise NestedDeferredParentBlockError if Thread.current[:idc_deferred_parent_expiration]
if Thread.current[:idc_deferred_expiration]
deprecator.deprecation_warning("`with_deferred_parent_expiration`")
end
Thread.current[:idc_deferred_parent_expiration] = true
Thread.current[:idc_parent_records_for_cache_expiry] = Set.new
result = yield
Thread.current[:idc_deferred_parent_expiration] = nil
Thread.current[:idc_parent_records_for_cache_expiry].each(&:expire_primary_index)
result
ensure
Thread.current[:idc_deferred_parent_expiration] = nil
Thread.current[:idc_parent_records_for_cache_expiry]&.clear
end
# Executes a block with deferred cache expiration, ensuring that the records' (parent,
# children and attributes) cache expiration is deferred until the block completes. When
# the block completes, it issues delete_multi calls for all the records and attributes
# that were marked for expiration.
#
# == Parameters:
# No parameters.
#
# == Raises:
# NestedDeferredCacheExpirationBlockError if a deferred cache expiration block is already active.
#
# == Yield:
# Runs the provided block with deferred cache expiration.
#
# == Returns:
# The result of executing the provided block.
#
# == Ensures:
# Cleans up thread-local variables related to deferred cache expiration regardless
# of whether the block raises an exception.
def with_deferred_expiration
raise NestedDeferredCacheExpirationBlockError if Thread.current[:idc_deferred_expiration]
if Thread.current[:idc_deferred_parent_expiration]
deprecator.deprecation_warning("`with_deferred_parent_expiration`")
end
Thread.current[:idc_deferred_expiration] = true
Thread.current[:idc_records_to_expire] = Set.new
Thread.current[:idc_attributes_to_expire] = Set.new
result = yield
Thread.current[:idc_deferred_expiration] = nil
if Thread.current[:idc_records_to_expire].any?
IdentityCache.cache.delete_multi(
Thread.current[:idc_records_to_expire]
)
end
if Thread.current[:idc_attributes_to_expire].any?
IdentityCache.cache.delete_multi(
Thread.current[:idc_attributes_to_expire]
)
end
result
ensure
Thread.current[:idc_deferred_expiration] = nil
Thread.current[:idc_records_to_expire].clear
Thread.current[:idc_attributes_to_expire].clear
end
def with_fetch_read_only_records(value = true)
old_value = Thread.current[:identity_cache_fetch_read_only_records]
Thread.current[:identity_cache_fetch_read_only_records] = value
yield
ensure
Thread.current[:identity_cache_fetch_read_only_records] = old_value
end
def fetch_read_only_records
v = Thread.current[:identity_cache_fetch_read_only_records]
return v unless v.nil?
@fetch_read_only_records
end
def eager_load!
ParentModelExpiration.install_all_pending_parent_expiry_hooks
end
def deprecator
@deprecator ||= ActiveSupport::Deprecation.new("1.7.0", "IdentityCache")
end
private
def fetch_in_batches(keys, &block)
keys.each_slice(BATCH_SIZE).each_with_object({}) do |slice, result|
result.merge!(cache.fetch_multi(*slice, &block))
end
end
end
end
require "identity_cache/railtie" if defined?(Rails)