simple_cacheable 1.2.0 → 1.2.1
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/.travis.yml +4 -0
- data/cacheable.gemspec +1 -0
- data/lib/cacheable/version.rb +1 -1
- data/lib/cacheable.rb +3 -1
- data/spec/cacheable_spec.rb +33 -28
- data/spec/spec_helper.rb +5 -33
- metadata +21 -4
data/.travis.yml
ADDED
data/cacheable.gemspec
CHANGED
@@ -15,6 +15,7 @@ Gem::Specification.new do |s|
|
|
15
15
|
s.add_dependency("rails", ">= 3.0.0")
|
16
16
|
s.add_development_dependency("rspec")
|
17
17
|
s.add_development_dependency("mocha")
|
18
|
+
s.add_development_dependency("memcached")
|
18
19
|
|
19
20
|
s.files = `git ls-files`.split("\n")
|
20
21
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
data/lib/cacheable/version.rb
CHANGED
data/lib/cacheable.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'uri'
|
2
|
+
|
1
3
|
module Cacheable
|
2
4
|
def self.included(base)
|
3
5
|
base.class_eval do
|
@@ -100,7 +102,7 @@ module Cacheable
|
|
100
102
|
end
|
101
103
|
|
102
104
|
def attribute_cache_key(attribute, value)
|
103
|
-
"#{name.tableize}/attribute/#{attribute}/#{value}"
|
105
|
+
"#{name.tableize}/attribute/#{attribute}/#{URI.escape(value)}"
|
104
106
|
end
|
105
107
|
end
|
106
108
|
end
|
data/spec/cacheable_spec.rb
CHANGED
@@ -18,12 +18,12 @@ describe Cacheable do
|
|
18
18
|
|
19
19
|
context "with_key" do
|
20
20
|
it "should not cache key" do
|
21
|
-
cache.
|
21
|
+
Rails.cache.read("users/#{@user.id}").should be_nil
|
22
22
|
end
|
23
23
|
|
24
24
|
it "should cache by User#id" do
|
25
25
|
User.find_cached(@user.id).should == @user
|
26
|
-
cache.
|
26
|
+
Rails.cache.read("users/#{@user.id}").should == @user
|
27
27
|
end
|
28
28
|
|
29
29
|
it "should get cached by User#id multiple times" do
|
@@ -34,28 +34,33 @@ describe Cacheable do
|
|
34
34
|
|
35
35
|
context "with_attribute" do
|
36
36
|
it "should not cache User.find_by_login" do
|
37
|
-
cache.
|
37
|
+
Rails.cache.read("users/attribute/login/flyerhzm").should be_nil
|
38
38
|
end
|
39
39
|
|
40
40
|
it "should cache by User.find_by_login" do
|
41
41
|
User.find_cached_by_login("flyerhzm").should == @user
|
42
|
-
cache.
|
42
|
+
Rails.cache.read("users/attribute/login/flyerhzm").should == @user
|
43
43
|
end
|
44
44
|
|
45
45
|
it "should get cached by User.find_by_login multiple times" do
|
46
46
|
User.find_cached_by_login("flyerhzm")
|
47
47
|
User.find_cached_by_login("flyerhzm").should == @user
|
48
48
|
end
|
49
|
+
|
50
|
+
it "should escape whitespace" do
|
51
|
+
new_user = User.create(:login => "user space")
|
52
|
+
User.find_cached_by_login("user space").should == new_user
|
53
|
+
end
|
49
54
|
end
|
50
55
|
|
51
56
|
context "with_method" do
|
52
57
|
it "should not cache User.last_post" do
|
53
|
-
cache.
|
58
|
+
Rails.cache.read("users/#{@user.id}/method/last_post").should be_nil
|
54
59
|
end
|
55
60
|
|
56
61
|
it "should cache User#last_post" do
|
57
62
|
@user.cached_last_post.should == @user.last_post
|
58
|
-
cache.
|
63
|
+
Rails.cache.read("users/#{@user.id}/method/last_post").should == @user.last_post
|
59
64
|
end
|
60
65
|
|
61
66
|
it "should cache User#last_post multiple times" do
|
@@ -67,12 +72,12 @@ describe Cacheable do
|
|
67
72
|
context "with_association" do
|
68
73
|
context "belongs_to" do
|
69
74
|
it "should not cache association" do
|
70
|
-
cache.
|
75
|
+
Rails.cache.read("users/#{@user.id}").should be_nil
|
71
76
|
end
|
72
77
|
|
73
78
|
it "should cache Post#user" do
|
74
79
|
@post1.cached_user.should == @user
|
75
|
-
cache.
|
80
|
+
Rails.cache.read("users/#{@user.id}").should == @user
|
76
81
|
end
|
77
82
|
|
78
83
|
it "should cache Post#user multiple times" do
|
@@ -81,20 +86,20 @@ describe Cacheable do
|
|
81
86
|
end
|
82
87
|
|
83
88
|
it "should cache Comment#commentable with polymorphic" do
|
84
|
-
cache.
|
89
|
+
Rails.cache.read("posts/#{@post1.id}").should be_nil
|
85
90
|
@comment1.cached_commentable.should == @post1
|
86
|
-
cache.
|
91
|
+
Rails.cache.read("posts/#{@post1.id}").should == @post1
|
87
92
|
end
|
88
93
|
end
|
89
94
|
|
90
95
|
context "has_many" do
|
91
96
|
it "should not cache associations" do
|
92
|
-
cache.
|
97
|
+
Rails.cache.read("users/#{@user.id}/association/posts").should be_nil
|
93
98
|
end
|
94
99
|
|
95
100
|
it "should cache User#posts" do
|
96
101
|
@user.cached_posts.should == [@post1, @post2]
|
97
|
-
cache.
|
102
|
+
Rails.cache.read("users/#{@user.id}/association/posts").should == [@post1, @post2]
|
98
103
|
end
|
99
104
|
|
100
105
|
it "should cache User#posts multiple times" do
|
@@ -105,12 +110,12 @@ describe Cacheable do
|
|
105
110
|
|
106
111
|
context "has_many with polymorphic" do
|
107
112
|
it "should not cache associations" do
|
108
|
-
cache.
|
113
|
+
Rails.cache.read("posts/#{@post1.id}/association/comments").should be_nil
|
109
114
|
end
|
110
115
|
|
111
116
|
it "should cache Post#comments" do
|
112
117
|
@post1.cached_comments.should == [@comment1, @comment2]
|
113
|
-
cache.
|
118
|
+
Rails.cache.read("posts/#{@post1.id}/association/comments").should == [@comment1, @comment2]
|
114
119
|
end
|
115
120
|
|
116
121
|
it "should cache Post#comments multiple times" do
|
@@ -121,12 +126,12 @@ describe Cacheable do
|
|
121
126
|
|
122
127
|
context "has_one" do
|
123
128
|
it "should not cache associations" do
|
124
|
-
cache.
|
129
|
+
Rails.cache.read("users/#{@user.id}/association/account").should be_nil
|
125
130
|
end
|
126
131
|
|
127
132
|
it "should cache User#posts" do
|
128
133
|
@user.cached_account.should == @account
|
129
|
-
cache.
|
134
|
+
Rails.cache.read("users/#{@user.id}/association/account").should == @account
|
130
135
|
end
|
131
136
|
|
132
137
|
it "should cache User#posts multiple times" do
|
@@ -139,44 +144,44 @@ describe Cacheable do
|
|
139
144
|
context "expire_model_cache" do
|
140
145
|
it "should delete with_key cache" do
|
141
146
|
user = User.find_cached(@user.id)
|
142
|
-
cache.
|
147
|
+
Rails.cache.read("users/#{user.id}").should_not be_nil
|
143
148
|
user.expire_model_cache
|
144
|
-
cache.
|
149
|
+
Rails.cache.read("users/#{user.id}").should be_nil
|
145
150
|
end
|
146
151
|
|
147
152
|
it "should delete with_attribute cache" do
|
148
153
|
user = User.find_cached_by_login("flyerhzm")
|
149
|
-
cache.
|
154
|
+
Rails.cache.read("users/attribute/login/flyerhzm").should == @user
|
150
155
|
@user.expire_model_cache
|
151
|
-
cache.
|
156
|
+
Rails.cache.read("users/attribute/login/flyerhzm").should be_nil
|
152
157
|
end
|
153
158
|
|
154
159
|
it "should delete with_method cache" do
|
155
160
|
@user.cached_last_post
|
156
|
-
cache.
|
161
|
+
Rails.cache.read("users/#{@user.id}/method/last_post").should_not be_nil
|
157
162
|
@user.expire_model_cache
|
158
|
-
cache.
|
163
|
+
Rails.cache.read("users/#{@user.id}/method/last_post").should be_nil
|
159
164
|
end
|
160
165
|
|
161
166
|
it "should delete has_many with_association cache" do
|
162
167
|
@user.cached_posts
|
163
|
-
cache.
|
168
|
+
Rails.cache.read("users/#{@user.id}/association/posts").should_not be_nil
|
164
169
|
@post1.save
|
165
|
-
cache.
|
170
|
+
Rails.cache.read("users/#{@user.id}/association/posts").should be_nil
|
166
171
|
end
|
167
172
|
|
168
173
|
it "should delete has_many with polymorphic with_association cache" do
|
169
174
|
@post1.cached_comments
|
170
|
-
cache.
|
175
|
+
Rails.cache.read("posts/#{@post1.id}/association/comments").should_not be_nil
|
171
176
|
@comment1.save
|
172
|
-
cache.
|
177
|
+
Rails.cache.read("posts/#{@post1.id}/association/comments").should be_nil
|
173
178
|
end
|
174
179
|
|
175
180
|
it "should delete has_one with_association cache" do
|
176
181
|
@user.cached_account
|
177
|
-
cache.
|
182
|
+
Rails.cache.read("users/#{@user.id}/association/account").should_not be_nil
|
178
183
|
@account.save
|
179
|
-
cache.
|
184
|
+
Rails.cache.read("users/#{@user.id}/association/account").should be_nil
|
180
185
|
end
|
181
186
|
end
|
182
187
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -5,6 +5,7 @@ require 'rails'
|
|
5
5
|
require 'active_record'
|
6
6
|
require 'rspec'
|
7
7
|
require 'mocha_standalone'
|
8
|
+
require 'memcached'
|
8
9
|
|
9
10
|
require 'cacheable'
|
10
11
|
|
@@ -16,42 +17,10 @@ $LOAD_PATH.unshift(MODELS)
|
|
16
17
|
|
17
18
|
Dir[ File.join(MODELS, "*.rb") ].sort.each { |file| require File.basename(file) }
|
18
19
|
|
19
|
-
class TestCache
|
20
|
-
attr_reader :data
|
21
|
-
|
22
|
-
def initialize
|
23
|
-
@data = {}
|
24
|
-
end
|
25
|
-
|
26
|
-
def fetch(key, &block)
|
27
|
-
if read(key)
|
28
|
-
read(key)
|
29
|
-
else
|
30
|
-
write(key, block.call)
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
def read(key)
|
35
|
-
@data[key]
|
36
|
-
end
|
37
|
-
|
38
|
-
def write(key, value)
|
39
|
-
@data[key] = value
|
40
|
-
end
|
41
|
-
|
42
|
-
def delete(key)
|
43
|
-
@data.delete key
|
44
|
-
end
|
45
|
-
|
46
|
-
def clear
|
47
|
-
@data.clear
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
20
|
module Rails
|
52
21
|
class <<self
|
53
22
|
def cache
|
54
|
-
@cache ||=
|
23
|
+
@cache ||= Memcached::Rails.new
|
55
24
|
end
|
56
25
|
end
|
57
26
|
end
|
@@ -59,6 +28,9 @@ end
|
|
59
28
|
RSpec.configure do |config|
|
60
29
|
config.mock_with :mocha
|
61
30
|
|
31
|
+
config.filter_run focus: true
|
32
|
+
config.run_all_when_everything_filtered = true
|
33
|
+
|
62
34
|
config.before :all do
|
63
35
|
::ActiveRecord::Schema.define(:version => 1) do
|
64
36
|
create_table :users do |t|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_cacheable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.2.
|
4
|
+
version: 1.2.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-11-25 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -59,6 +59,22 @@ dependencies:
|
|
59
59
|
- - ! '>='
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: memcached
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
62
78
|
description: a simple cache implementation based on activerecord
|
63
79
|
email:
|
64
80
|
- flyerhzm@gmail.com
|
@@ -69,6 +85,7 @@ files:
|
|
69
85
|
- .gitignore
|
70
86
|
- .rspec
|
71
87
|
- .rvmrc
|
88
|
+
- .travis.yml
|
72
89
|
- Gemfile
|
73
90
|
- README.md
|
74
91
|
- Rakefile
|
@@ -95,7 +112,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
95
112
|
version: '0'
|
96
113
|
segments:
|
97
114
|
- 0
|
98
|
-
hash:
|
115
|
+
hash: 2779579293480982592
|
99
116
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
117
|
none: false
|
101
118
|
requirements:
|
@@ -104,7 +121,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
104
121
|
version: '0'
|
105
122
|
segments:
|
106
123
|
- 0
|
107
|
-
hash:
|
124
|
+
hash: 2779579293480982592
|
108
125
|
requirements: []
|
109
126
|
rubyforge_project:
|
110
127
|
rubygems_version: 1.8.24
|