activerecord_cache 0.0.8 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +29 -29
- data/lib/activerecord_cache.rb +0 -1
- data/lib/activerecord_cache/base.rb +15 -2
- data/lib/activerecord_cache/finder_methods.rb +1 -1
- data/lib/activerecord_cache/railtie.rb +0 -1
- data/lib/activerecord_cache/version.rb +1 -1
- data/test/activerecord_cache_test.rb +28 -4
- metadata +3 -4
- data/lib/activerecord_cache/relation.rb +0 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fecb86dd42b882d6a12115f86055b869d3df8e5b
|
4
|
+
data.tar.gz: 3045d66663380cb8c03727af1a22c90e57858aa5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ba80e163493cda91b090529fd1d1b81f27d7fc21fa4e99e77bcf8dd010b8592c0c2ddf45391022f1de9ef0aa19bd1b7293bd33eba0146e68720c463d2f538605
|
7
|
+
data.tar.gz: 928688eaed5dc868736abf2c02504996a4dee9da9c77a5364784436a50de61c14de030bafb16a4c6e7c998fa31132a694c8ed355d0e200ca9f2a91634e73d4ca
|
data/Gemfile.lock
CHANGED
@@ -1,33 +1,33 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
activerecord_cache (0.0.
|
4
|
+
activerecord_cache (0.0.9)
|
5
5
|
rails (~> 4.0)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: http://rubygems.org/
|
9
9
|
specs:
|
10
|
-
actionmailer (4.1.
|
11
|
-
actionpack (= 4.1.
|
12
|
-
actionview (= 4.1.
|
10
|
+
actionmailer (4.1.5)
|
11
|
+
actionpack (= 4.1.5)
|
12
|
+
actionview (= 4.1.5)
|
13
13
|
mail (~> 2.5.4)
|
14
|
-
actionpack (4.1.
|
15
|
-
actionview (= 4.1.
|
16
|
-
activesupport (= 4.1.
|
14
|
+
actionpack (4.1.5)
|
15
|
+
actionview (= 4.1.5)
|
16
|
+
activesupport (= 4.1.5)
|
17
17
|
rack (~> 1.5.2)
|
18
18
|
rack-test (~> 0.6.2)
|
19
|
-
actionview (4.1.
|
20
|
-
activesupport (= 4.1.
|
19
|
+
actionview (4.1.5)
|
20
|
+
activesupport (= 4.1.5)
|
21
21
|
builder (~> 3.1)
|
22
22
|
erubis (~> 2.7.0)
|
23
|
-
activemodel (4.1.
|
24
|
-
activesupport (= 4.1.
|
23
|
+
activemodel (4.1.5)
|
24
|
+
activesupport (= 4.1.5)
|
25
25
|
builder (~> 3.1)
|
26
|
-
activerecord (4.1.
|
27
|
-
activemodel (= 4.1.
|
28
|
-
activesupport (= 4.1.
|
26
|
+
activerecord (4.1.5)
|
27
|
+
activemodel (= 4.1.5)
|
28
|
+
activesupport (= 4.1.5)
|
29
29
|
arel (~> 5.0.0)
|
30
|
-
activesupport (4.1.
|
30
|
+
activesupport (4.1.5)
|
31
31
|
i18n (~> 0.6, >= 0.6.9)
|
32
32
|
json (~> 1.7, >= 1.7.7)
|
33
33
|
minitest (~> 5.1)
|
@@ -46,7 +46,7 @@ GEM
|
|
46
46
|
treetop (~> 1.4.8)
|
47
47
|
metaclass (0.0.4)
|
48
48
|
mime-types (1.25.1)
|
49
|
-
minitest (5.4.
|
49
|
+
minitest (5.4.2)
|
50
50
|
mocha (0.14.0)
|
51
51
|
metaclass (~> 0.0.1)
|
52
52
|
multi_json (1.10.1)
|
@@ -54,19 +54,19 @@ GEM
|
|
54
54
|
rack (1.5.2)
|
55
55
|
rack-test (0.6.2)
|
56
56
|
rack (>= 1.0)
|
57
|
-
rails (4.1.
|
58
|
-
actionmailer (= 4.1.
|
59
|
-
actionpack (= 4.1.
|
60
|
-
actionview (= 4.1.
|
61
|
-
activemodel (= 4.1.
|
62
|
-
activerecord (= 4.1.
|
63
|
-
activesupport (= 4.1.
|
57
|
+
rails (4.1.5)
|
58
|
+
actionmailer (= 4.1.5)
|
59
|
+
actionpack (= 4.1.5)
|
60
|
+
actionview (= 4.1.5)
|
61
|
+
activemodel (= 4.1.5)
|
62
|
+
activerecord (= 4.1.5)
|
63
|
+
activesupport (= 4.1.5)
|
64
64
|
bundler (>= 1.3.0, < 2.0)
|
65
|
-
railties (= 4.1.
|
65
|
+
railties (= 4.1.5)
|
66
66
|
sprockets-rails (~> 2.0)
|
67
|
-
railties (4.1.
|
68
|
-
actionpack (= 4.1.
|
69
|
-
activesupport (= 4.1.
|
67
|
+
railties (4.1.5)
|
68
|
+
actionpack (= 4.1.5)
|
69
|
+
activesupport (= 4.1.5)
|
70
70
|
rake (>= 0.8.7)
|
71
71
|
thor (>= 0.18.1, < 2.0)
|
72
72
|
rake (10.3.2)
|
@@ -75,7 +75,7 @@ GEM
|
|
75
75
|
multi_json (~> 1.0)
|
76
76
|
rack (~> 1.0)
|
77
77
|
tilt (~> 1.1, != 1.3.0)
|
78
|
-
sprockets-rails (2.1.
|
78
|
+
sprockets-rails (2.1.4)
|
79
79
|
actionpack (>= 3.0)
|
80
80
|
activesupport (>= 3.0)
|
81
81
|
sprockets (~> 2.8)
|
@@ -86,7 +86,7 @@ GEM
|
|
86
86
|
treetop (1.4.15)
|
87
87
|
polyglot
|
88
88
|
polyglot (>= 0.3.1)
|
89
|
-
tzinfo (1.2.
|
89
|
+
tzinfo (1.2.2)
|
90
90
|
thread_safe (~> 0.1)
|
91
91
|
|
92
92
|
PLATFORMS
|
data/lib/activerecord_cache.rb
CHANGED
@@ -5,7 +5,6 @@ require File.join(File.dirname(__FILE__), 'activerecord_cache/base')
|
|
5
5
|
require File.join(File.dirname(__FILE__), 'activerecord_cache/finder_methods')
|
6
6
|
require File.join(File.dirname(__FILE__), 'activerecord_cache/belongs_to_association')
|
7
7
|
require File.join(File.dirname(__FILE__), 'activerecord_cache/belongs_to_preloader')
|
8
|
-
require File.join(File.dirname(__FILE__), 'activerecord_cache/relation')
|
9
8
|
require File.join(File.dirname(__FILE__), 'activerecord_cache/errors')
|
10
9
|
|
11
10
|
require 'activerecord_cache/railtie.rb' if defined?(Rails)
|
@@ -32,7 +32,15 @@ module ActiveRecordCache
|
|
32
32
|
raise ActiveRecordCache::CacheNotEnabled, message
|
33
33
|
end
|
34
34
|
|
35
|
-
Rails.cache.read(cache_key(id))
|
35
|
+
record = Rails.cache.read(cache_key(id))
|
36
|
+
|
37
|
+
unless record
|
38
|
+
# use where to bypass cached finders
|
39
|
+
record = where(primary_key => id).first
|
40
|
+
record.write_to_cache if record
|
41
|
+
end
|
42
|
+
|
43
|
+
record
|
36
44
|
end
|
37
45
|
|
38
46
|
def find_some_through_cache(ids)
|
@@ -53,7 +61,12 @@ module ActiveRecordCache
|
|
53
61
|
end
|
54
62
|
|
55
63
|
if cache_misses.present?
|
56
|
-
|
64
|
+
# use where to bypass cached finders
|
65
|
+
loaded_records = where(primary_key => cache_misses).load
|
66
|
+
|
67
|
+
loaded_records.each(&:write_to_cache)
|
68
|
+
|
69
|
+
records += loaded_records
|
57
70
|
end
|
58
71
|
|
59
72
|
records
|
@@ -14,7 +14,7 @@ module ActiveRecordCache
|
|
14
14
|
|
15
15
|
results = find_some_through_cache(ids)
|
16
16
|
|
17
|
-
raise_record_not_found_exception!(ids,
|
17
|
+
raise_record_not_found_exception!(ids, results.size, ids.size) unless results.size == ids.size
|
18
18
|
|
19
19
|
results
|
20
20
|
end
|
@@ -8,7 +8,6 @@ class ActiveRecordCache::Railtie < Rails::Railtie
|
|
8
8
|
ActiveRecord::Base.send(:include, ActiveRecordCache::Base)
|
9
9
|
ActiveRecord::Associations::BelongsToAssociation.send(:include, ActiveRecordCache::BelongsToAssociation)
|
10
10
|
ActiveRecord::Associations::Preloader::BelongsTo.send(:include, ActiveRecordCache::BelongsToPreloader)
|
11
|
-
ActiveRecord::Relation.send(:include, ActiveRecordCache::Relation)
|
12
11
|
|
13
12
|
# FinderMethods only gets included in ActiveRecord::Relation, so extend it there.
|
14
13
|
# This gets around some headaches caused by extending modules already included in other classes.
|
@@ -14,18 +14,33 @@ class ActiveRecordCacheTest < ActiveSupport::TestCase
|
|
14
14
|
CachedRecord.find(1, 2)
|
15
15
|
end
|
16
16
|
|
17
|
-
test "loading all records should persist them to the cache" do
|
18
|
-
Rails.cache.expects(:write).
|
17
|
+
test "loading all records should NOT persist them to the cache" do
|
18
|
+
Rails.cache.expects(:write).never
|
19
19
|
CachedRecord.all.load
|
20
20
|
end
|
21
21
|
|
22
|
-
test "loading records using a where should persist them to the cache" do
|
23
|
-
Rails.cache.expects(:write).
|
22
|
+
test "loading records using a where should NOT persist them to the cache" do
|
23
|
+
Rails.cache.expects(:write).never
|
24
24
|
CachedRecord.where(:name => 'John').load
|
25
25
|
end
|
26
26
|
|
27
27
|
|
28
28
|
|
29
|
+
# Errors should still be raised
|
30
|
+
test "loading missing records should still throw errors" do
|
31
|
+
bogus_id = 12345
|
32
|
+
|
33
|
+
assert_raises ActiveRecord::RecordNotFound do
|
34
|
+
CachedRecord.find(bogus_id)
|
35
|
+
end
|
36
|
+
|
37
|
+
assert_raises ActiveRecord::RecordNotFound do
|
38
|
+
CachedRecord.find([1, bogus_id])
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
|
43
|
+
|
29
44
|
# Finders should use the cache
|
30
45
|
test "#find(id) should use the cache" do
|
31
46
|
cache_records(1)
|
@@ -62,6 +77,15 @@ class ActiveRecordCacheTest < ActiveSupport::TestCase
|
|
62
77
|
assert records.empty?, 'should not find cached record'
|
63
78
|
end
|
64
79
|
|
80
|
+
# where + id gets treated like a complex query
|
81
|
+
test "#where(:id => id) does NOT use the cache" do
|
82
|
+
cache_records(1)
|
83
|
+
|
84
|
+
assert_query_count 1 do
|
85
|
+
records = CachedRecord.where(:id => 1).load
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
65
89
|
test "#select(:id) should NOT use the cache" do
|
66
90
|
cache_records(1)
|
67
91
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activerecord_cache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Doan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-11-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -44,7 +44,6 @@ files:
|
|
44
44
|
- lib/activerecord_cache/errors.rb
|
45
45
|
- lib/activerecord_cache/finder_methods.rb
|
46
46
|
- lib/activerecord_cache/railtie.rb
|
47
|
-
- lib/activerecord_cache/relation.rb
|
48
47
|
- lib/activerecord_cache/version.rb
|
49
48
|
- test/activerecord_cache_test.rb
|
50
49
|
- test/dummy/README.rdoc
|
@@ -115,7 +114,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
115
114
|
version: '0'
|
116
115
|
requirements: []
|
117
116
|
rubyforge_project:
|
118
|
-
rubygems_version: 2.2.
|
117
|
+
rubygems_version: 2.2.2
|
119
118
|
signing_key:
|
120
119
|
specification_version: 4
|
121
120
|
summary: A basic caching plugin for ActiveRecord
|
@@ -1,16 +0,0 @@
|
|
1
|
-
module ActiveRecordCache
|
2
|
-
module Relation
|
3
|
-
extend ActiveSupport::Concern
|
4
|
-
|
5
|
-
included do
|
6
|
-
alias_method_chain :exec_queries, :caching
|
7
|
-
end
|
8
|
-
|
9
|
-
def exec_queries_with_caching
|
10
|
-
records = exec_queries_without_caching
|
11
|
-
records.each(&:write_to_cache) if @klass.use_activerecord_cache && select_values.empty?
|
12
|
-
|
13
|
-
records
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|