simple_cacheable 1.3.2 → 1.3.3

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.
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe Cacheable do
4
+ let(:cache) { Rails.cache }
5
+ let(:user) { User.create(:login => 'flyerhzm') }
6
+
7
+ before :all do
8
+ @post1 = user.posts.create(:title => 'post1')
9
+ @post2 = user.posts.create(:title => 'post2')
10
+ end
11
+
12
+ before :each do
13
+ cache.clear
14
+ user.reload
15
+ end
16
+
17
+ context "with_method" do
18
+ it "should not cache User.last_post" do
19
+ Rails.cache.read("users/#{user.id}/method/last_post").should be_nil
20
+ end
21
+
22
+ it "should cache User#last_post" do
23
+ user.cached_last_post.should == user.last_post
24
+ Rails.cache.read("users/#{user.id}/method/last_post").should == user.last_post
25
+ end
26
+
27
+ it "should cache User#last_post multiple times" do
28
+ user.cached_last_post
29
+ user.cached_last_post.should == user.last_post
30
+ end
31
+ end
32
+
33
+ end
@@ -2,302 +2,28 @@ require 'spec_helper'
2
2
 
3
3
  describe Cacheable do
4
4
  let(:cache) { Rails.cache }
5
+ let(:user) { User.create(:login => 'flyerhzm') }
5
6
 
6
7
  before :all do
7
- @user = User.create(:login => 'flyerhzm')
8
8
  @group1 = Group.create(name: "Ruby On Rails")
9
- @account = @user.create_account(group: @group1)
10
- @post1 = @user.posts.create(:title => 'post1')
11
- @post2 = @user.posts.create(:title => 'post2')
9
+ @account = user.create_account(group: @group1)
10
+ @post1 = user.posts.create(:title => 'post1')
12
11
  @image1 = @post1.images.create
13
- @image2 = @post1.images.create
14
12
  @comment1 = @post1.comments.create
15
- @comment2 = @post1.comments.create
16
13
  @tag1 = @post1.tags.create(title: "Rails")
17
- @tag2 = @post1.tags.create(title: "Caching")
18
14
  end
19
15
 
20
16
  before :each do
21
17
  cache.clear
18
+ user.reload
22
19
  end
23
20
 
24
- context "with_key" do
25
- it "should not cache key" do
26
- Rails.cache.read("users/#{@user.id}").should be_nil
27
- end
28
-
29
- it "should cache by User#id" do
30
- User.find_cached(@user.id).should == @user
31
- Rails.cache.read("users/#{@user.id}").should == @user
32
- end
33
-
34
- it "should get cached by User#id multiple times" do
35
- User.find_cached(@user.id)
36
- User.find_cached(@user.id).should == @user
37
- end
38
- end
39
-
40
- context "with_attribute" do
41
- it "should not cache User.find_by_login" do
42
- Rails.cache.read("users/attribute/login/flyerhzm").should be_nil
43
- end
44
-
45
- it "should cache by User.find_by_login" do
46
- User.find_cached_by_login("flyerhzm").should == @user
47
- Rails.cache.read("users/attribute/login/flyerhzm").should == @user
48
- end
49
-
50
- it "should get cached by User.find_by_login multiple times" do
51
- User.find_cached_by_login("flyerhzm")
52
- User.find_cached_by_login("flyerhzm").should == @user
53
- end
54
-
55
- it "should escape whitespace" do
56
- new_user = User.create(:login => "user space")
57
- User.find_cached_by_login("user space").should == new_user
58
- end
59
-
60
- it "should handle fixed numbers" do
61
- Post.find_cached_by_user_id(@user.id).should == @post1
62
- Rails.cache.read("posts/attribute/user_id/#{@user.id}").should == @post1
63
- end
64
-
65
- context "find_all" do
66
- it "should not cache Post.find_all_by_user_id" do
67
- Rails.cache.read("posts/attribute/user_id/all/#{@user.id}").should be_nil
68
- end
69
-
70
- it "should cache by Post.find_cached_all_by_user_id" do
71
- Post.find_cached_all_by_user_id(@user.id).should == [@post1, @post2]
72
- Rails.cache.read("posts/attribute/user_id/all/#{@user.id}").should == [@post1, @post2]
73
- end
74
-
75
- it "should get cached by Post.find_cached_all_by_user_id multiple times" do
76
- Post.find_cached_all_by_user_id(@user.id)
77
- Post.find_cached_all_by_user_id(@user.id).should == [@post1, @post2]
78
- end
79
-
80
- end
81
- end
82
-
83
-
84
- context "with_method" do
85
- it "should not cache User.last_post" do
86
- Rails.cache.read("users/#{@user.id}/method/last_post").should be_nil
87
- end
88
-
89
- it "should cache User#last_post" do
90
- @user.cached_last_post.should == @user.last_post
91
- Rails.cache.read("users/#{@user.id}/method/last_post").should == @user.last_post
92
- end
93
-
94
- it "should cache User#last_post multiple times" do
95
- @user.cached_last_post
96
- @user.cached_last_post.should == @user.last_post
97
- end
98
- end
99
-
100
- context "with_class_method" do
101
- it "should not cache Post.default_post" do
102
- Rails.cache.read("posts/class_method/default_post").should be_nil
103
- end
104
-
105
- it "should cache Post.default_post" do
106
- Post.cached_default_post.should == @post1
107
- Rails.cache.read("posts/class_method/default_post").should == @post1
108
- end
109
-
110
- it "should cache Post.default_post multiple times" do
111
- Post.cached_default_post
112
- Post.cached_default_post.should == @post1
113
- end
114
- end
115
-
116
- context "with_association" do
117
- context "belongs_to" do
118
- it "should not cache association" do
119
- Rails.cache.read("users/#{@user.id}").should be_nil
120
- end
121
-
122
- it "should cache Post#user" do
123
- @post1.cached_user.should == @user
124
- Rails.cache.read("users/#{@user.id}").should == @user
125
- end
126
-
127
- it "should cache Post#user multiple times" do
128
- @post1.cached_user
129
- @post1.cached_user.should == @user
130
- end
131
-
132
- it "should cache Comment#commentable with polymorphic" do
133
- Rails.cache.read("posts/#{@post1.id}").should be_nil
134
- @comment1.cached_commentable.should == @post1
135
- Rails.cache.read("posts/#{@post1.id}").should == @post1
136
- end
137
- end
138
-
139
- context "has_many" do
140
- it "should not cache associations" do
141
- Rails.cache.read("users/#{@user.id}/association/posts").should be_nil
142
- end
143
-
144
- it "should cache User#posts" do
145
- @user.cached_posts.should == [@post1, @post2]
146
- Rails.cache.read("users/#{@user.id}/association/posts").should == [@post1, @post2]
147
- end
148
-
149
- it "should cache User#posts multiple times" do
150
- @user.cached_posts
151
- @user.cached_posts.should == [@post1, @post2]
152
- end
153
- end
154
-
155
- context "has_many with polymorphic" do
156
- it "should not cache associations" do
157
- Rails.cache.read("posts/#{@post1.id}/association/comments").should be_nil
158
- end
159
-
160
- it "should cache Post#comments" do
161
- @post1.cached_comments.should == [@comment1, @comment2]
162
- Rails.cache.read("posts/#{@post1.id}/association/comments").should == [@comment1, @comment2]
163
- end
164
-
165
- it "should cache Post#comments multiple times" do
166
- @post1.cached_comments
167
- @post1.cached_comments.should == [@comment1, @comment2]
168
- end
169
- end
170
-
171
- context "has_one" do
172
- it "should not cache associations" do
173
- Rails.cache.read("users/#{@user.id}/association/account").should be_nil
174
- end
175
-
176
- it "should cache User#posts" do
177
- @user.cached_account.should == @account
178
- Rails.cache.read("users/#{@user.id}/association/account").should == @account
179
- end
180
-
181
- it "should cache User#posts multiple times" do
182
- @user.cached_account
183
- @user.cached_account.should == @account
184
- end
185
- end
186
-
187
- context "has_many through" do
188
- it "should not cache associations" do
189
- Rails.cache.read("users/#{@user.id}/association/images").should be_nil
190
- end
191
-
192
- it "should cache User#images" do
193
- @user.cached_images.should == [@image1, @image2]
194
- Rails.cache.read("users/#{@user.id}/association/images").should == [@image1, @image2]
195
- end
196
-
197
- it "should cache User#images multiple times" do
198
- @user.cached_images
199
- @user.cached_images.should == [@image1, @image2]
200
- end
201
-
202
- context "expiry" do
203
- it "should have the correct collection" do
204
- @image3 = @post1.images.create
205
- Rails.cache.read("users/#{@user.id}/association/images").should be_nil
206
- @user.cached_images.should == [@image1, @image2, @image3]
207
- Rails.cache.read("users/#{@user.id}/association/images").should == [@image1, @image2, @image3]
208
- end
209
- end
210
- end
211
-
212
- context "has_one through belongs_to" do
213
- it "should not cache associations" do
214
- Rails.cache.read("users/#{@user.id}/association/group").should be_nil
215
- end
216
-
217
- it "should cache User#group" do
218
- @user.cached_group.should == @group1
219
- Rails.cache.read("users/#{@user.id}/association/group").should == @group1
220
- end
221
-
222
- it "should cache User#group multiple times" do
223
- @user.cached_group
224
- @user.cached_group.should == @group1
225
- end
226
-
227
- end
228
-
229
- context "has_and_belongs_to_many" do
230
-
231
- it "should not cache associations off the bat" do
232
- Rails.cache.read("posts/#{@post1.id}/association/tags").should be_nil
233
- end
234
-
235
- it "should cache Post#tags" do
236
- @post1.cached_tags.should == [@tag1, @tag2]
237
- Rails.cache.read("posts/#{@post1.id}/association/tags").should == [@tag1, @tag2]
238
- end
239
-
240
- it "should handle multiple requests" do
241
- @post1.cached_tags
242
- @post1.cached_tags.should == [@tag1, @tag2]
243
- end
244
-
245
- context "expiry" do
246
- it "should have the correct collection" do
247
- @tag3 = @post1.tags.create!(title: "Invalidation is hard")
248
- Rails.cache.read("posts/#{@post1.id}/association/tags").should be_nil
249
- @post1.cached_tags.should == [@tag1, @tag2, @tag3]
250
- Rails.cache.read("posts/#{@post1.id}/association/tags").should == [@tag1, @tag2, @tag3]
251
- end
252
- end
253
- end
254
-
255
- end
256
-
257
- context "expire_model_cache" do
258
- it "should delete with_key cache" do
259
- user = User.find_cached(@user.id)
260
- Rails.cache.read("users/#{user.id}").should_not be_nil
261
- user.expire_model_cache
262
- Rails.cache.read("users/#{user.id}").should be_nil
263
- end
264
-
265
- it "should delete with_attribute cache" do
266
- user = User.find_cached_by_login("flyerhzm")
267
- Rails.cache.read("users/attribute/login/flyerhzm").should == @user
268
- @user.expire_model_cache
269
- Rails.cache.read("users/attribute/login/flyerhzm").should be_nil
270
- end
271
-
272
- it "should delete with_method cache" do
273
- @user.cached_last_post
274
- Rails.cache.read("users/#{@user.id}/method/last_post").should_not be_nil
275
- @user.expire_model_cache
276
- Rails.cache.read("users/#{@user.id}/method/last_post").should be_nil
277
- end
278
-
279
- it "should delete with_class_method cache" do
280
- Post.cached_default_post
281
- Rails.cache.read("posts/class_method/default_post").should_not be_nil
282
- @post1.expire_model_cache
283
- Rails.cache.read("posts/class_method/default_post").should be_nil
284
- end
285
-
286
- it "should delete associations cache" do
287
- @user.cached_images
288
- Rails.cache.read("users/#{@user.id}/association/images").should_not be_nil
289
- @user.expire_model_cache
290
- Rails.cache.read("users/#{@user.id}/association/images").should be_nil
291
- end
292
-
293
- end
294
-
295
- context "object#save" do
21
+ context "Association Expires on Save" do
296
22
  it "should delete has_many with_association cache" do
297
- @user.cached_posts
298
- Rails.cache.read("users/#{@user.id}/association/posts").should_not be_nil
23
+ user.cached_posts
24
+ Rails.cache.read("users/#{user.id}/association/posts").should_not be_nil
299
25
  @post1.save
300
- Rails.cache.read("users/#{@user.id}/association/posts").should be_nil
26
+ Rails.cache.read("users/#{user.id}/association/posts").should be_nil
301
27
  end
302
28
 
303
29
  it "should delete has_many with polymorphic with_association cache" do
@@ -308,17 +34,17 @@ describe Cacheable do
308
34
  end
309
35
 
310
36
  it "should delete has_many through with_association cache" do
311
- @user.cached_images
312
- Rails.cache.read("users/#{@user.id}/association/images").should_not be_nil
313
- @image2.save
314
- Rails.cache.read("users/#{@user.id}/association/images").should be_nil
37
+ user.cached_images
38
+ Rails.cache.read("users/#{user.id}/association/images").should_not be_nil
39
+ @image1.save
40
+ Rails.cache.read("users/#{user.id}/association/images").should be_nil
315
41
  end
316
42
 
317
43
  it "should delete has_one with_association cache" do
318
- @user.cached_account
319
- Rails.cache.read("users/#{@user.id}/association/account").should_not be_nil
44
+ user.cached_account
45
+ Rails.cache.read("users/#{user.id}/association/account").should_not be_nil
320
46
  @account.save
321
- Rails.cache.read("users/#{@user.id}/association/account").should be_nil
47
+ Rails.cache.read("users/#{user.id}/association/account").should be_nil
322
48
  end
323
49
 
324
50
  it "should delete has_and_belongs_to_many with_association cache" do
@@ -330,9 +56,9 @@ describe Cacheable do
330
56
 
331
57
  it "should delete has_one through belongs_to with_association cache" do
332
58
  @group1.save
333
- Rails.cache.read("users/#{@user.id}/association/group").should be_nil
334
- @user.cached_group.should == @group1
335
- Rails.cache.read("users/#{@user.id}/association/group").should == @group1
59
+ Rails.cache.read("users/#{user.id}/association/group").should be_nil
60
+ user.cached_group.should == @group1
61
+ Rails.cache.read("users/#{user.id}/association/group").should == @group1
336
62
  end
337
63
  end
338
64
  end
data/spec/models/post.rb CHANGED
@@ -12,10 +12,19 @@ class Post < ActiveRecord::Base
12
12
  with_key
13
13
  with_attribute :user_id
14
14
  with_association :user, :comments, :images, :tags
15
- with_class_method :default_post
15
+ with_class_method :default_post, :retrieve_with_user_id, :retrieve_with_both
16
16
  end
17
17
 
18
18
  def self.default_post
19
19
  Post.first
20
20
  end
21
+
22
+ def self.retrieve_with_user_id(user_id)
23
+ Post.find_by_user_id(user_id)
24
+ end
25
+
26
+ def self.retrieve_with_both(user_id, post_id)
27
+ Post.find(post_id) == Post.find_by_user_id(user_id)
28
+ end
29
+
21
30
  end
data/spec/spec_helper.rb CHANGED
@@ -7,7 +7,6 @@ require 'rspec'
7
7
  require 'mocha/api'
8
8
  require 'memcached'
9
9
  require 'cacheable'
10
- require 'debugger'
11
10
 
12
11
  # MODELS = File.join(File.dirname(__FILE__), "models")
13
12
  # $LOAD_PATH.unshift(MODELS)
metadata CHANGED
@@ -1,8 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_cacheable
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.2
5
- prerelease:
4
+ version: 1.3.3
6
5
  platform: ruby
7
6
  authors:
8
7
  - Richard Huang
@@ -10,54 +9,48 @@ authors:
10
9
  autorequire:
11
10
  bindir: bin
12
11
  cert_chain: []
13
- date: 2012-12-31 00:00:00.000000000 Z
12
+ date: 2013-07-10 00:00:00.000000000 Z
14
13
  dependencies:
15
14
  - !ruby/object:Gem::Dependency
16
15
  name: rails
17
16
  requirement: !ruby/object:Gem::Requirement
18
- none: false
19
17
  requirements:
20
- - - ! '>='
18
+ - - '>='
21
19
  - !ruby/object:Gem::Version
22
20
  version: 3.0.0
23
21
  type: :runtime
24
22
  prerelease: false
25
23
  version_requirements: !ruby/object:Gem::Requirement
26
- none: false
27
24
  requirements:
28
- - - ! '>='
25
+ - - '>='
29
26
  - !ruby/object:Gem::Version
30
27
  version: 3.0.0
31
28
  - !ruby/object:Gem::Dependency
32
29
  name: rspec
33
30
  requirement: !ruby/object:Gem::Requirement
34
- none: false
35
31
  requirements:
36
- - - ! '>='
32
+ - - '>='
37
33
  - !ruby/object:Gem::Version
38
34
  version: '0'
39
35
  type: :development
40
36
  prerelease: false
41
37
  version_requirements: !ruby/object:Gem::Requirement
42
- none: false
43
38
  requirements:
44
- - - ! '>='
39
+ - - '>='
45
40
  - !ruby/object:Gem::Version
46
41
  version: '0'
47
42
  - !ruby/object:Gem::Dependency
48
43
  name: mocha
49
44
  requirement: !ruby/object:Gem::Requirement
50
- none: false
51
45
  requirements:
52
- - - ! '>='
46
+ - - '>='
53
47
  - !ruby/object:Gem::Version
54
48
  version: '0'
55
49
  type: :development
56
50
  prerelease: false
57
51
  version_requirements: !ruby/object:Gem::Requirement
58
- none: false
59
52
  requirements:
60
- - - ! '>='
53
+ - - '>='
61
54
  - !ruby/object:Gem::Version
62
55
  version: '0'
63
56
  description: a simple cache implementation based on activerecord
@@ -70,14 +63,31 @@ extra_rdoc_files: []
70
63
  files:
71
64
  - .gitignore
72
65
  - .rspec
73
- - .rvmrc
66
+ - .ruby-gemset
67
+ - .ruby-version
74
68
  - .travis.yml
69
+ - ChangeLog
75
70
  - Gemfile
71
+ - Gemfile.lock
76
72
  - README.md
77
73
  - Rakefile
78
74
  - cacheable.gemspec
79
75
  - lib/cacheable.rb
76
+ - lib/cacheable/caches.rb
77
+ - lib/cacheable/expiry.rb
78
+ - lib/cacheable/keys.rb
79
+ - lib/cacheable/types/association_cache.rb
80
+ - lib/cacheable/types/attribute_cache.rb
81
+ - lib/cacheable/types/class_method_cache.rb
82
+ - lib/cacheable/types/key_cache.rb
83
+ - lib/cacheable/types/method_cache.rb
80
84
  - lib/cacheable/version.rb
85
+ - spec/cacheable/expiry_cache_spec.rb
86
+ - spec/cacheable/types/association_cache_spec.rb
87
+ - spec/cacheable/types/attribute_cache_spec.rb
88
+ - spec/cacheable/types/class_method_cache_spec.rb
89
+ - spec/cacheable/types/key_cache_spec.rb
90
+ - spec/cacheable/types/method_cache_spec.rb
81
91
  - spec/cacheable_spec.rb
82
92
  - spec/models/account.rb
83
93
  - spec/models/comment.rb
@@ -89,29 +99,34 @@ files:
89
99
  - spec/spec_helper.rb
90
100
  homepage: https://github.com/flyerhzm/simple-cacheable
91
101
  licenses: []
102
+ metadata: {}
92
103
  post_install_message:
93
104
  rdoc_options: []
94
105
  require_paths:
95
106
  - lib
96
107
  required_ruby_version: !ruby/object:Gem::Requirement
97
- none: false
98
108
  requirements:
99
- - - ! '>='
109
+ - - '>='
100
110
  - !ruby/object:Gem::Version
101
111
  version: '0'
102
112
  required_rubygems_version: !ruby/object:Gem::Requirement
103
- none: false
104
113
  requirements:
105
- - - ! '>='
114
+ - - '>='
106
115
  - !ruby/object:Gem::Version
107
116
  version: '0'
108
117
  requirements: []
109
118
  rubyforge_project:
110
- rubygems_version: 1.8.24
119
+ rubygems_version: 2.0.3
111
120
  signing_key:
112
- specification_version: 3
121
+ specification_version: 4
113
122
  summary: a simple cache implementation based on activerecord
114
123
  test_files:
124
+ - spec/cacheable/expiry_cache_spec.rb
125
+ - spec/cacheable/types/association_cache_spec.rb
126
+ - spec/cacheable/types/attribute_cache_spec.rb
127
+ - spec/cacheable/types/class_method_cache_spec.rb
128
+ - spec/cacheable/types/key_cache_spec.rb
129
+ - spec/cacheable/types/method_cache_spec.rb
115
130
  - spec/cacheable_spec.rb
116
131
  - spec/models/account.rb
117
132
  - spec/models/comment.rb
data/.rvmrc DELETED
@@ -1,2 +0,0 @@
1
- rvm_gemset_create_on_use_flag=1
2
- rvm gemset use cacheable