record_cache 0.9.9 → 0.9.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +10 -6
- data/VERSION +1 -1
- data/lib/record_cache.rb +18 -16
- data/record_cache.gemspec +2 -2
- data/test/test_helper.rb +1 -1
- metadata +2 -2
data/README.rdoc
CHANGED
@@ -6,17 +6,21 @@ reduce your database load.
|
|
6
6
|
|
7
7
|
== Usage:
|
8
8
|
|
9
|
-
|
9
|
+
require 'record_cache'
|
10
|
+
|
11
|
+
class User < ActiveRecord
|
10
12
|
record_cache :by => :id
|
11
|
-
record_cache :id, :by => :
|
13
|
+
record_cache :id, :by => :username
|
12
14
|
end
|
13
15
|
|
14
16
|
# These will use the cache now.
|
15
|
-
|
16
|
-
|
17
|
-
|
17
|
+
User.find(1)
|
18
|
+
User.find_by_id(2)
|
19
|
+
User.find_all_by_username('chuck')
|
18
20
|
|
19
|
-
Invalidation is handled for you using callbacks.
|
21
|
+
Invalidation is handled for you using callbacks. Be careful though if you modify records
|
22
|
+
directly using SQL. Both update_all and delete_all handle invalidations for you, but other
|
23
|
+
direct SQL will not.
|
20
24
|
|
21
25
|
== Install:
|
22
26
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.9.
|
1
|
+
0.9.10
|
data/lib/record_cache.rb
CHANGED
@@ -19,9 +19,11 @@ module RecordCache
|
|
19
19
|
end
|
20
20
|
|
21
21
|
def self.db(model_class)
|
22
|
-
# Always use the master connection since we are caching.
|
23
22
|
db = model_class.connection
|
24
|
-
|
23
|
+
|
24
|
+
# Always use the master connection since we are caching.
|
25
|
+
@has_data_fabric ||= defined?(DataFabric::ConnectionProxy)
|
26
|
+
if @has_data_fabric and db.kind_of?(DataFabric::ConnectionProxy)
|
25
27
|
model_class.record_cache_config[:use_slave] ? db.send(:connection) : db.send(:master)
|
26
28
|
else
|
27
29
|
db
|
@@ -31,7 +33,7 @@ module RecordCache
|
|
31
33
|
module InstanceMethods
|
32
34
|
def invalidate_record_cache
|
33
35
|
self.class.each_cached_index do |index|
|
34
|
-
index.invalidate_model(self)
|
36
|
+
index.invalidate_model(self)
|
35
37
|
index.clear_deferred
|
36
38
|
end
|
37
39
|
end
|
@@ -54,14 +56,14 @@ module RecordCache
|
|
54
56
|
['id', 'type'].include?(attr) ? send(attr) : send(:attribute_was, attr)
|
55
57
|
end
|
56
58
|
end
|
57
|
-
|
58
|
-
module ClassMethods
|
59
|
+
|
60
|
+
module ClassMethods
|
59
61
|
def find_with_caching(*args, &block)
|
60
62
|
if args.last.is_a?(Hash)
|
61
63
|
args.last.delete_if {|k,v| v.nil?}
|
62
64
|
args.pop if args.last.empty?
|
63
65
|
end
|
64
|
-
|
66
|
+
|
65
67
|
if [:all, :first, :last].include?(args.first)
|
66
68
|
opts = args.last
|
67
69
|
if opts.is_a?(Hash) and opts.keys == [:conditions]
|
@@ -80,7 +82,7 @@ module RecordCache
|
|
80
82
|
|
81
83
|
find_without_caching(*args, &block)
|
82
84
|
end
|
83
|
-
|
85
|
+
|
84
86
|
def update_all_with_invalidate(updates, conditions = nil)
|
85
87
|
invalidate_from_conditions(conditions, :update) do |conditions|
|
86
88
|
update_all_without_invalidate(updates, conditions)
|
@@ -92,7 +94,7 @@ module RecordCache
|
|
92
94
|
delete_all_without_invalidate(conditions)
|
93
95
|
end
|
94
96
|
end
|
95
|
-
|
97
|
+
|
96
98
|
def invalidate_from_conditions(conditions, flag = nil)
|
97
99
|
if conditions.nil?
|
98
100
|
# Just invalidate all indexes.
|
@@ -119,9 +121,9 @@ module RecordCache
|
|
119
121
|
result = yield(conditions)
|
120
122
|
|
121
123
|
# Finish invalidating with prior attributes.
|
122
|
-
lambdas.each {|l| l.call}
|
124
|
+
lambdas.each {|l| l.call}
|
123
125
|
end
|
124
|
-
|
126
|
+
|
125
127
|
# Invalidate again afterwards if we are updating (or for the first time if no block was given).
|
126
128
|
if flag == :update or not block_given?
|
127
129
|
each_cached_index do |index|
|
@@ -157,7 +159,7 @@ module RecordCache
|
|
157
159
|
def each_cached_index
|
158
160
|
cached_index_names.each do |index_name|
|
159
161
|
yield cached_index(index_name)
|
160
|
-
end
|
162
|
+
end
|
161
163
|
end
|
162
164
|
|
163
165
|
def cached_index_names
|
@@ -213,7 +215,7 @@ module RecordCache
|
|
213
215
|
define_method( "all_#{index.name.pluralize}_by_#{index.index_field}" ) do |keys|
|
214
216
|
index.field_lookup(keys, self, field, :all)
|
215
217
|
end
|
216
|
-
|
218
|
+
|
217
219
|
define_method( "#{index.name.pluralize}_by_#{index.index_field}" ) do |keys|
|
218
220
|
index.field_lookup(keys, self, field)
|
219
221
|
end
|
@@ -222,7 +224,7 @@ module RecordCache
|
|
222
224
|
index.field_lookup(keys, self, field, :first)
|
223
225
|
end
|
224
226
|
end
|
225
|
-
|
227
|
+
|
226
228
|
if index.auto_name?
|
227
229
|
(field_lookup + index.fields).each do |field|
|
228
230
|
next if field == index.index_field
|
@@ -237,20 +239,20 @@ module RecordCache
|
|
237
239
|
define_method( "#{prefix}#{plural_field}_by_#{index.index_field}" ) do |keys|
|
238
240
|
index.field_lookup(keys, self, field)
|
239
241
|
end
|
240
|
-
|
242
|
+
|
241
243
|
define_method( "#{prefix}#{field}_by_#{index.index_field}" ) do |keys|
|
242
244
|
index.field_lookup(keys, self, field, :first)
|
243
245
|
end
|
244
246
|
end
|
245
247
|
end
|
246
|
-
|
248
|
+
|
247
249
|
if first_index
|
248
250
|
alias_method_chain :find, :caching
|
249
251
|
alias_method_chain :update_all, :invalidate
|
250
252
|
alias_method_chain :delete_all, :invalidate
|
251
253
|
end
|
252
254
|
end
|
253
|
-
|
255
|
+
|
254
256
|
if first_index
|
255
257
|
after_save :invalidate_record_cache_deferred
|
256
258
|
after_destroy :invalidate_record_cache_deferred
|
data/record_cache.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{record_cache}
|
8
|
-
s.version = "0.9.
|
8
|
+
s.version = "0.9.10"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Justin Balthrop"]
|
12
|
-
s.date = %q{
|
12
|
+
s.date = %q{2010-05-01}
|
13
13
|
s.description = %q{Active Record caching and indexing in memcache}
|
14
14
|
s.email = %q{code@justinbalthrop.com}
|
15
15
|
s.extra_rdoc_files = [
|
data/test/test_helper.rb
CHANGED
@@ -4,7 +4,7 @@ require 'shoulda'
|
|
4
4
|
require 'mocha'
|
5
5
|
|
6
6
|
$LOAD_PATH.unshift File.dirname(__FILE__) + "/../lib"
|
7
|
-
['
|
7
|
+
['cache_version', 'model_set', 'memcache', 'deferrable'].each do |dir|
|
8
8
|
$LOAD_PATH.unshift File.dirname(__FILE__) + "/../../#{dir}/lib"
|
9
9
|
end
|
10
10
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: record_cache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.10
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Justin Balthrop
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date:
|
12
|
+
date: 2010-05-01 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|