/
active_record_31.rb
370 lines (314 loc) · 12.7 KB
/
active_record_31.rb
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
module RecordCache
module ActiveRecord
module Base
class << self
def included(klass)
klass.extend ClassMethods
klass.class_eval do
class << self
alias_method_chain :find_by_sql, :record_cache
end
end
include InstanceMethods
end
end
module ClassMethods
# add cache invalidation hooks on initialization
def record_cache_init
after_commit :record_cache_create, :on => :create
after_commit :record_cache_update, :on => :update
after_commit :record_cache_destroy, :on => :destroy
end
# Retrieve the records, possibly from cache
def find_by_sql_with_record_cache(*args)
# no caching please
return find_by_sql_without_record_cache(*args) unless record_cache?
# check the piggy-back'd ActiveRelation record to see if the query can be retrieved from cache
arel = args[0]
arel = arel.instance_variable_get(:@arel) if arel.is_a?(String)
query = arel ? RecordCache::Arel::QueryVisitor.new(args[1]).accept(arel.ast) : nil
cacheable = query && record_cache.cacheable?(query)
# log only in debug mode!
RecordCache::Base.logger.debug{ "#{cacheable ? 'Fetch from cache' : 'Not cacheable'} (#{query}): SQL = #{arel.to_sql}" }
# retrieve the records from cache if the query is cacheable otherwise go straight to the DB
cacheable ? record_cache.fetch(query) : find_by_sql_without_record_cache(*args)
end
end
module InstanceMethods
end
end
end
module Arel
# The method <ActiveRecord::Base>.find_by_sql is used to actually
# retrieve the data from the DB.
# Unfortunately the ActiveRelation record is not accessible from
# there, so it is piggy-back'd in the SQL string.
module TreeManager
def self.included(klass)
klass.extend ClassMethods
klass.send(:include, InstanceMethods)
klass.class_eval do
alias_method_chain :to_sql, :record_cache
end
end
module ClassMethods
end
module InstanceMethods
def to_sql_with_record_cache
sql = to_sql_without_record_cache
sql.instance_variable_set(:@arel, self)
sql
end
end
end
# Visitor for the ActiveRelation to extract a simple cache query
# Only accepts single select queries with equality where statements
# Rejects queries with grouping / having / offset / etc.
class QueryVisitor < ::Arel::Visitors::Visitor
DESC = "DESC".freeze
BINDING_MARKER_1 = "?".freeze
BINDING_MARKER_2 = "\u0000".freeze
COMMA = ",".freeze
def initialize(bindings)
super()
@bindings = (bindings || []).inject({}){ |h, cv| column, value = cv; h[column.name] = value; h}
@cacheable = true
@query = ::RecordCache::Query.new
end
def accept ast
super
@cacheable && !ast.lock ? @query : nil
end
private
def not_cacheable o
@cacheable = false
end
alias :visit_Arel_Nodes_Ordering :not_cacheable
alias :visit_Arel_Nodes_TableAlias :not_cacheable
alias :visit_Arel_Nodes_Lock :not_cacheable
alias :visit_Arel_Nodes_Sum :not_cacheable
alias :visit_Arel_Nodes_Max :not_cacheable
alias :visit_Arel_Nodes_Avg :not_cacheable
alias :visit_Arel_Nodes_Count :not_cacheable
alias :visit_Arel_Nodes_StringJoin :not_cacheable
alias :visit_Arel_Nodes_InnerJoin :not_cacheable
alias :visit_Arel_Nodes_OuterJoin :not_cacheable
alias :visit_Arel_Nodes_DeleteStatement :not_cacheable
alias :visit_Arel_Nodes_InsertStatement :not_cacheable
alias :visit_Arel_Nodes_UpdateStatement :not_cacheable
alias :unary :not_cacheable
alias :visit_Arel_Nodes_Group :unary
alias :visit_Arel_Nodes_Having :unary
alias :visit_Arel_Nodes_Not :unary
alias :visit_Arel_Nodes_On :unary
alias :visit_Arel_Nodes_UnqualifiedColumn :unary
def visit_Arel_Nodes_Offset o
@cacheable = false unless o.expr == 0
end
def visit_Arel_Nodes_Values o
visit o.expressions if @cacheable
end
def visit_Arel_Nodes_Limit o
@query.limit = o.expr
end
alias :visit_Arel_Nodes_Top :visit_Arel_Nodes_Limit
def visit_Arel_Nodes_Grouping o
return unless @cacheable
# `calendars`.account_id = 5
if @table_name && o.expr =~ /^`#{@table_name}`\.`?(\w*)`?\s*=\s*(\d+)$/
@cacheable = @query.where($1, $2.to_i)
# `service_instances`.`id` IN (118,80,120,82)
elsif o.expr =~ /^`#{@table_name}`\.`?(\w*)`?\s*IN\s*\(([\d\s,]+)\)$/
@cacheable = @query.where($1, $2.split(',').map(&:to_i))
else
@cacheable = false
end
end
def visit_Arel_Nodes_SelectCore o
@cacheable = false unless o.groups.empty?
visit o.froms if @cacheable
visit o.wheres if @cacheable
# skip o.projections
end
def visit_Arel_Nodes_SelectStatement o
@cacheable = false if o.cores.size > 1
if @cacheable
visit o.offset
o.orders.map { |x| handle_order_by(visit x) } if @cacheable && o.orders.size > 0
visit o.limit
visit o.cores
end
end
def handle_order_by(order)
order.to_s.split(COMMA).each do |o|
# simple sort order (+peope.id+ can be replaced by +id+, as joins are not allowed anyways)
if o.match(/^\s*([\w\.]*)\s*(|ASC|DESC|)\s*$/)
asc = $2 == DESC ? false : true
@query.order_by($1.split('.').last, asc)
else
@cacheable = false
end
end
end
def visit_Arel_Table o
@table_name = o.name
end
def visit_Arel_Nodes_Ordering o
[visit(o.expr), o.descending]
end
def visit_Arel_Attributes_Attribute o
o.name.to_sym
end
alias :visit_Arel_Attributes_Integer :visit_Arel_Attributes_Attribute
alias :visit_Arel_Attributes_Float :visit_Arel_Attributes_Attribute
alias :visit_Arel_Attributes_String :visit_Arel_Attributes_Attribute
alias :visit_Arel_Attributes_Time :visit_Arel_Attributes_Attribute
alias :visit_Arel_Attributes_Boolean :visit_Arel_Attributes_Attribute
def visit_Arel_Nodes_Equality o
key, value = visit(o.left), visit(o.right)
# both ? and \u0000 are used to mark query bindings (thanks to Arkadiusz Kuryłowicz for the \u0000)
if value.to_s == BINDING_MARKER_1 || value.to_s == BINDING_MARKER_2
# puts "bindings: #{@bindings.inspect}, key = #{key.to_s}"
value = @bindings[key.to_s] || value
end
# puts " =====> equality found: #{key.inspect}@#{key.class.name} => #{value.inspect}@#{value.class.name}"
@query.where(key, value)
end
alias :visit_Arel_Nodes_In :visit_Arel_Nodes_Equality
def visit_Arel_Nodes_And o
visit(o.left)
visit(o.right)
end
alias :visit_Arel_Nodes_Or :not_cacheable
alias :visit_Arel_Nodes_NotEqual :not_cacheable
alias :visit_Arel_Nodes_GreaterThan :not_cacheable
alias :visit_Arel_Nodes_GreaterThanOrEqual :not_cacheable
alias :visit_Arel_Nodes_Assignment :not_cacheable
alias :visit_Arel_Nodes_LessThan :not_cacheable
alias :visit_Arel_Nodes_LessThanOrEqual :not_cacheable
alias :visit_Arel_Nodes_Between :not_cacheable
alias :visit_Arel_Nodes_NotIn :not_cacheable
alias :visit_Arel_Nodes_DoesNotMatch :not_cacheable
alias :visit_Arel_Nodes_Matches :not_cacheable
def visit_Fixnum o
o.to_i
end
alias :visit_Bignum :visit_Fixnum
def visit_Symbol o
o.to_sym
end
def visit_Object o
o
end
alias :visit_Arel_Nodes_SqlLiteral :visit_Object
alias :visit_Arel_SqlLiteral :visit_Object # This is deprecated
alias :visit_String :visit_Object
alias :visit_NilClass :visit_Object
alias :visit_TrueClass :visit_Object
alias :visit_FalseClass :visit_Object
alias :visit_Arel_SqlLiteral :visit_Object
alias :visit_BigDecimal :visit_Object
alias :visit_Float :visit_Object
alias :visit_Time :visit_Object
alias :visit_Date :visit_Object
alias :visit_DateTime :visit_Object
alias :visit_Hash :visit_Object
def visit_Array o
o.map{ |x| visit x }
end
end
end
end
module RecordCache
# Patch ActiveRecord::Relation to make sure update_all will invalidate all referenced records
module ActiveRecord
module UpdateAll
class << self
def included(klass)
klass.extend ClassMethods
klass.send(:include, InstanceMethods)
klass.class_eval do
alias_method_chain :update_all, :record_cache
end
end
end
module ClassMethods
end
module InstanceMethods
def update_all_with_record_cache(updates, conditions = nil, options = {})
result = update_all_without_record_cache(updates, conditions, options)
if record_cache?
# when this condition is met, the arel.update method will be called on the current scope, see ActiveRecord::Relation#update_all
unless conditions || options.present? || @limit_value.present? != @order_values.present?
# get all attributes that contian a unique index for this model
unique_index_attributes = RecordCache::Strategy::UniqueIndexCache.attributes(self)
# go straight to SQL result (without instantiating records) for optimal performance
connection.execute(select(unique_index_attributes.map(&:to_s).join(',')).to_sql).each do |row|
# invalidate the unique index for all attributes
unique_index_attributes.each_with_index do |attribute, index|
record_cache.invalidate(attribute, (row.is_a?(Hash) ? row[attribute.to_s] : row[index]) )
end
end
end
end
result
end
end
end
end
# Patch ActiveRecord::Associations::HasManyAssociation to make sure the index_cache is updated when records are
# deleted from the collection
module ActiveRecord
module HasMany
class << self
def included(klass)
klass.extend ClassMethods
klass.send(:include, InstanceMethods)
klass.class_eval do
alias_method_chain :delete_records, :record_cache
end
end
end
module ClassMethods
end
module InstanceMethods
def delete_records_with_record_cache(records, method)
# invalidate :id cache for all records
records.each{ |record| record.class.record_cache.invalidate(record.id) if record.class.record_cache? unless record.new_record? }
# invalidate the referenced class for the attribute/value pair on the index cache
@reflection.klass.record_cache.invalidate(@reflection.foreign_key.to_sym, @owner.id) if @reflection.klass.record_cache?
delete_records_without_record_cache(records, method)
end
end
end
module HasOne
class << self
def included(klass)
klass.extend ClassMethods
klass.send(:include, InstanceMethods)
klass.class_eval do
alias_method_chain :delete, :record_cache
end
end
end
module ClassMethods
end
module InstanceMethods
def delete_with_record_cache(method = options[:dependent])
# invalidate :id cache for all record
if load_target
target.class.record_cache.invalidate(record.id) if target.class.record_cache? unless target.new_record?
end
# invalidate the referenced class for the attribute/value pair on the index cache
@reflection.klass.record_cache.invalidate(@reflection.foreign_key.to_sym, @owner.id) if @reflection.klass.record_cache?
delete_without_record_cache(method)
end
end
end
end
end
ActiveRecord::Base.send(:include, RecordCache::ActiveRecord::Base)
Arel::TreeManager.send(:include, RecordCache::Arel::TreeManager)
ActiveRecord::Relation.send(:include, RecordCache::ActiveRecord::UpdateAll)
ActiveRecord::Associations::HasManyAssociation.send(:include, RecordCache::ActiveRecord::HasMany)
ActiveRecord::Associations::HasOneAssociation.send(:include, RecordCache::ActiveRecord::HasOne)