simple_cacheable 1.0.0 → 1.1.0
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.md +26 -5
- data/Rakefile +2 -0
- data/lib/cacheable/version.rb +1 -1
- data/lib/cacheable.rb +34 -2
- data/spec/cacheable_spec.rb +92 -20
- data/spec/models/account.rb +3 -0
- data/spec/models/post.rb +1 -1
- data/spec/models/user.rb +2 -0
- data/spec/spec_helper.rb +4 -0
- metadata +4 -2
data/README.md
CHANGED
@@ -6,6 +6,11 @@ extracted from [rails-bestpractices.com][1].
|
|
6
6
|
|
7
7
|
it supports activerecord >= 3.0.0
|
8
8
|
|
9
|
+
Introduction
|
10
|
+
------------
|
11
|
+
|
12
|
+
Here is a blog post to introduce simple_cacheable gem, <http://rails-bestpractices.com/blog/posts/24-simple_cacheable>
|
13
|
+
|
9
14
|
Usage
|
10
15
|
-----
|
11
16
|
|
@@ -13,11 +18,13 @@ Usage
|
|
13
18
|
include Cacheable
|
14
19
|
|
15
20
|
has_many :posts
|
21
|
+
has_one :account
|
16
22
|
|
17
23
|
model_cache do
|
18
|
-
with_key
|
19
|
-
with_attribute :login
|
20
|
-
with_method :last_post
|
24
|
+
with_key # User.find_cached(1)
|
25
|
+
with_attribute :login # User.find_cached_by_login('flyerhzm')
|
26
|
+
with_method :last_post # user.cached_last_post
|
27
|
+
with_association :posts, :account # user.cached_posts, user.cached_account
|
21
28
|
end
|
22
29
|
|
23
30
|
def last_post
|
@@ -25,6 +32,10 @@ Usage
|
|
25
32
|
end
|
26
33
|
end
|
27
34
|
|
35
|
+
class Account < ActiveRecord::Base
|
36
|
+
belongs_to :user
|
37
|
+
end
|
38
|
+
|
28
39
|
class Post < ActiveRecord::Base
|
29
40
|
include Cacheable
|
30
41
|
|
@@ -32,8 +43,8 @@ Usage
|
|
32
43
|
has_many :comments, :as => :commentable
|
33
44
|
|
34
45
|
model_cache do
|
35
|
-
with_key
|
36
|
-
with_association :user
|
46
|
+
with_key # post.find_cached(1)
|
47
|
+
with_association :user, :comments # post.cached_user, post.cached_comments
|
37
48
|
end
|
38
49
|
end
|
39
50
|
|
@@ -47,5 +58,15 @@ Usage
|
|
47
58
|
end
|
48
59
|
end
|
49
60
|
|
61
|
+
Install
|
62
|
+
-------
|
63
|
+
|
64
|
+
add the following code to your Gemfile
|
65
|
+
|
66
|
+
gem "simple_cacheable", :require => "cacheable"
|
67
|
+
|
68
|
+
|
69
|
+
Copyright © 2011 Richard Huang (flyerhzm@gmail.com), released under the MIT license
|
70
|
+
|
50
71
|
|
51
72
|
[1]:https://github.com/flyerhzm/rails-bestpractices.com
|
data/Rakefile
CHANGED
data/lib/cacheable/version.rb
CHANGED
data/lib/cacheable.rb
CHANGED
@@ -63,7 +63,31 @@ module Cacheable
|
|
63
63
|
polymorphic = association.options[:polymorphic]
|
64
64
|
class_eval <<-EOF
|
65
65
|
def cached_#{association_name}
|
66
|
-
Rails.cache.fetch
|
66
|
+
Rails.cache.fetch belong_association_cache_key("#{association_name}", #{polymorphic}) do
|
67
|
+
#{association_name}
|
68
|
+
end
|
69
|
+
end
|
70
|
+
EOF
|
71
|
+
else
|
72
|
+
reverse_association = association.klass.reflect_on_all_associations(:belongs_to).find { |reverse_association|
|
73
|
+
reverse_association.options[:polymorphic] ? reverse_association.name == association.options[:as] : reverse_association.klass == self
|
74
|
+
}
|
75
|
+
association.klass.class_eval <<-EOF
|
76
|
+
after_save :expire_#{association_name}_cache
|
77
|
+
after_destroy :expire_#{association_name}_cache
|
78
|
+
|
79
|
+
def expire_#{association_name}_cache
|
80
|
+
if respond_to? :cached_#{reverse_association.name}
|
81
|
+
cached_#{reverse_association.name}.expire_association_cache(:#{association_name})
|
82
|
+
else
|
83
|
+
#{reverse_association.name}.expire_association_cache(:#{association_name})
|
84
|
+
end
|
85
|
+
end
|
86
|
+
EOF
|
87
|
+
|
88
|
+
class_eval <<-EOF
|
89
|
+
def cached_#{association_name}
|
90
|
+
Rails.cache.fetch have_association_cache_key("#{association_name}") do
|
67
91
|
#{association_name}
|
68
92
|
end
|
69
93
|
end
|
@@ -107,6 +131,10 @@ module Cacheable
|
|
107
131
|
end
|
108
132
|
end
|
109
133
|
|
134
|
+
def expire_association_cache(name)
|
135
|
+
Rails.cache.delete have_association_cache_key(name)
|
136
|
+
end
|
137
|
+
|
110
138
|
def model_cache_key
|
111
139
|
"#{self.class.name.tableize}/#{self.id.to_i}"
|
112
140
|
end
|
@@ -115,11 +143,15 @@ module Cacheable
|
|
115
143
|
"#{model_cache_key}/method/#{meth}"
|
116
144
|
end
|
117
145
|
|
118
|
-
def
|
146
|
+
def belong_association_cache_key(name, polymorphic=nil)
|
119
147
|
if polymorphic
|
120
148
|
"#{self.send("#{name}_type").tableize}/#{self.send("#{name}_id")}"
|
121
149
|
else
|
122
150
|
"#{name.tableize}/#{self.send(name + "_id")}"
|
123
151
|
end
|
124
152
|
end
|
153
|
+
|
154
|
+
def have_association_cache_key(name)
|
155
|
+
"#{model_cache_key}/association/#{name}"
|
156
|
+
end
|
125
157
|
end
|
data/spec/cacheable_spec.rb
CHANGED
@@ -5,13 +5,14 @@ describe Cacheable do
|
|
5
5
|
|
6
6
|
before :all do
|
7
7
|
@user = User.create(:login => 'flyerhzm')
|
8
|
+
@account = @user.create_account
|
8
9
|
@post1 = @user.posts.create(:title => 'post1')
|
9
10
|
@post2 = @user.posts.create(:title => 'post2')
|
10
11
|
@comment1 = @post1.comments.create
|
11
|
-
@comment2 = @
|
12
|
+
@comment2 = @post1.comments.create
|
12
13
|
end
|
13
14
|
|
14
|
-
|
15
|
+
before :each do
|
15
16
|
cache.clear
|
16
17
|
end
|
17
18
|
|
@@ -64,24 +65,74 @@ describe Cacheable do
|
|
64
65
|
end
|
65
66
|
|
66
67
|
context "with_association" do
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
68
|
+
context "belongs_to" do
|
69
|
+
it "should not cache association" do
|
70
|
+
cache.data["users/#{@user.id}"].should be_nil
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should cache Post#user" do
|
74
|
+
@post1.cached_user.should == @user
|
75
|
+
cache.data["users/#{@user.id}"].should == @user
|
76
|
+
end
|
77
|
+
|
78
|
+
it "should cache Post#user multiple times" do
|
79
|
+
@post1.cached_user
|
80
|
+
@post1.cached_user.should == @user
|
81
|
+
end
|
82
|
+
|
83
|
+
it "should cache Comment#commentable with polymorphic" do
|
84
|
+
cache.data["posts/#{@post1.id}"].should be_nil
|
85
|
+
@comment1.cached_commentable.should == @post1
|
86
|
+
cache.data["posts/#{@post1.id}"].should == @post1
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
context "has_many" do
|
91
|
+
it "should not cache associations" do
|
92
|
+
cache.data["users/#{@user.id}/association/posts"].should be_nil
|
93
|
+
end
|
94
|
+
|
95
|
+
it "should cache User#posts" do
|
96
|
+
@user.cached_posts.should == [@post1, @post2]
|
97
|
+
cache.data["users/#{@user.id}/association/posts"].should == [@post1, @post2]
|
98
|
+
end
|
99
|
+
|
100
|
+
it "should cache User#posts multiple times" do
|
101
|
+
@user.cached_posts
|
102
|
+
@user.cached_posts.should == [@post1, @post2]
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
context "has_many with polymorphic" do
|
107
|
+
it "should not cache associations" do
|
108
|
+
cache.data["posts/#{@post1.id}/association/comments"].should be_nil
|
109
|
+
end
|
110
|
+
|
111
|
+
it "should cache Post#comments" do
|
112
|
+
@post1.cached_comments.should == [@comment1, @comment2]
|
113
|
+
cache.data["posts/#{@post1.id}/association/comments"].should == [@comment1, @comment2]
|
114
|
+
end
|
115
|
+
|
116
|
+
it "should cache Post#comments multiple times" do
|
117
|
+
@post1.cached_comments
|
118
|
+
@post1.cached_comments.should == [@comment1, @comment2]
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
context "has_one" do
|
123
|
+
it "should not cache associations" do
|
124
|
+
cache.data["users/#{@user.id}/association/account"].should be_nil
|
125
|
+
end
|
126
|
+
|
127
|
+
it "should cache User#posts" do
|
128
|
+
@user.cached_account.should == @account
|
129
|
+
cache.data["users/#{@user.id}/association/account"].should == @account
|
130
|
+
end
|
131
|
+
|
132
|
+
it "should cache User#posts multiple times" do
|
133
|
+
@user.cached_account
|
134
|
+
@user.cached_account.should == @account
|
135
|
+
end
|
85
136
|
end
|
86
137
|
end
|
87
138
|
|
@@ -106,5 +157,26 @@ describe Cacheable do
|
|
106
157
|
@user.expire_model_cache
|
107
158
|
cache.data["users/#{@user.id}/method/last_post"].should be_nil
|
108
159
|
end
|
160
|
+
|
161
|
+
it "should delete has_many with_association cache" do
|
162
|
+
@user.cached_posts
|
163
|
+
cache.data["users/#{@user.id}/association/posts"].should_not be_nil
|
164
|
+
@post1.save
|
165
|
+
cache.data["users/#{@user.id}/association/posts"].should be_nil
|
166
|
+
end
|
167
|
+
|
168
|
+
it "should delete has_many with polymorphic with_association cache" do
|
169
|
+
@post1.cached_comments
|
170
|
+
cache.data["posts/#{@post1.id}/association/comments"].should_not be_nil
|
171
|
+
@comment1.save
|
172
|
+
cache.data["posts/#{@post1.id}/association/comments"].should be_nil
|
173
|
+
end
|
174
|
+
|
175
|
+
it "should delete has_one with_association cache" do
|
176
|
+
@user.cached_account
|
177
|
+
cache.data["users/#{@user.id}/association/account"].should_not be_nil
|
178
|
+
@account.save
|
179
|
+
cache.data["users/#{@user.id}/association/account"].should be_nil
|
180
|
+
end
|
109
181
|
end
|
110
182
|
end
|
data/spec/models/post.rb
CHANGED
data/spec/models/user.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: simple_cacheable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 1.
|
5
|
+
version: 1.1.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Richard Huang
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-10-
|
13
|
+
date: 2011-10-06 00:00:00 +08:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -77,6 +77,7 @@ files:
|
|
77
77
|
- lib/cacheable.rb
|
78
78
|
- lib/cacheable/version.rb
|
79
79
|
- spec/cacheable_spec.rb
|
80
|
+
- spec/models/account.rb
|
80
81
|
- spec/models/comment.rb
|
81
82
|
- spec/models/post.rb
|
82
83
|
- spec/models/user.rb
|
@@ -111,6 +112,7 @@ specification_version: 3
|
|
111
112
|
summary: a simple cache implementation based on activerecord
|
112
113
|
test_files:
|
113
114
|
- spec/cacheable_spec.rb
|
115
|
+
- spec/models/account.rb
|
114
116
|
- spec/models/comment.rb
|
115
117
|
- spec/models/post.rb
|
116
118
|
- spec/models/user.rb
|