sequel-cacheable 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.2
@@ -44,7 +44,7 @@ module Sequel::Plugins
44
44
 
45
45
  ttl = ttl || cache_options.ttl
46
46
  if cache_options.pack_lib?
47
- obj = obj.map{|o| o.msgpack_hash } if obj.kind_of?(Array)
47
+ obj = obj.map{|o| o.id } if obj.kind_of?(Array)
48
48
  obj = cache_options.pack_lib.pack(obj)
49
49
  end
50
50
 
@@ -70,6 +70,26 @@ module Sequel::Plugins
70
70
  obj
71
71
  end
72
72
 
73
+ def cache_mget(*keys)
74
+ if cache_options.ignore_exceptions?
75
+ objs = cache_store.mget(*keys) rescue nil
76
+ else
77
+ objs = cache_store.mget(*keys)
78
+ end
79
+
80
+ objs = nil if objs == [nil]
81
+
82
+ if objs && cache_options.pack_lib?
83
+ objs.map!{|obj|
84
+ key = keys.shift
85
+ (obj && restore_cache(cache_options.pack_lib.unpack(obj))) ||
86
+ model[key.sub(/^#{model}::/, '')]
87
+ }
88
+ end
89
+
90
+ objs || []
91
+ end
92
+
73
93
  def cache_del(key)
74
94
  cache_store.send(cache_store_type.delete_method, key)
75
95
  end
@@ -85,7 +105,7 @@ module Sequel::Plugins
85
105
  def restore_cache(object)
86
106
  return object if object.nil?
87
107
 
88
- return object.map{|o| restore_cache(o) } if object.kind_of?(Array)
108
+ return cache_mget(*object.map{|id| "#{model}::#{id}" }) if object.kind_of?(Array)
89
109
 
90
110
  object.keys.each do | key |
91
111
  value = object.delete(key)
@@ -100,6 +120,7 @@ module Sequel::Plugins
100
120
  end
101
121
 
102
122
  def clear_query_cache
123
+ return unless cache_options.query_cache?
103
124
  cache_store.keys("#{model.name}::Query::*").each do | key |
104
125
  cache_del(key)
105
126
  end
@@ -130,7 +151,7 @@ module Sequel::Plugins
130
151
  end
131
152
 
132
153
  def after_initialize
133
- store_cache
154
+ store_cache unless id.nil?
134
155
  super
135
156
  end
136
157
 
@@ -0,0 +1,97 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "sequel-cacheable"
8
+ s.version = "0.0.2"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Sho Kusano <rosylilly>"]
12
+ s.date = "2012-02-26"
13
+ s.description = "This plug-in caching mechanism to implement the Model of the Sequel"
14
+ s.email = "rosylilly@aduca.org"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ "Gemfile",
23
+ "Gemfile.lock",
24
+ "Guardfile",
25
+ "LICENSE.txt",
26
+ "README.rdoc",
27
+ "Rakefile",
28
+ "VERSION",
29
+ "lib/sequel-cacheable.rb",
30
+ "sequel-cacheable.gemspec",
31
+ "spec/lib/sequel-cacheable_spec.rb",
32
+ "spec/spec_helper.rb"
33
+ ]
34
+ s.homepage = "http://github.com/rosylilly/sequel-cacheable"
35
+ s.licenses = ["MIT"]
36
+ s.require_paths = ["lib"]
37
+ s.rubygems_version = "1.8.11"
38
+ s.summary = "This plug-in caching mechanism to implement the Model of the Sequel"
39
+
40
+ if s.respond_to? :specification_version then
41
+ s.specification_version = 3
42
+
43
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
44
+ s.add_runtime_dependency(%q<sequel>, ["~> 3.32.0"])
45
+ s.add_runtime_dependency(%q<msgpack>, ["~> 0.4.6"])
46
+ s.add_runtime_dependency(%q<hashr>, ["~> 0.0.19"])
47
+ s.add_development_dependency(%q<bundler>, [">= 1.0.0"])
48
+ s.add_development_dependency(%q<rspec>, ["~> 2.8.0"])
49
+ s.add_development_dependency(%q<yard>, ["~> 0.7"])
50
+ s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
51
+ s.add_development_dependency(%q<jeweler>, ["~> 1.8.3"])
52
+ s.add_development_dependency(%q<simplecov>, ["~> 0.6.1"])
53
+ s.add_development_dependency(%q<guard>, ["~> 1.0.0"])
54
+ s.add_development_dependency(%q<guard-rspec>, ["~> 0.6.0"])
55
+ s.add_development_dependency(%q<growl>, [">= 0"])
56
+ s.add_development_dependency(%q<sqlite3>, [">= 0"])
57
+ s.add_development_dependency(%q<memcache>, [">= 0"])
58
+ s.add_development_dependency(%q<redis>, [">= 0"])
59
+ s.add_development_dependency(%q<queencheck>, [">= 0"])
60
+ else
61
+ s.add_dependency(%q<sequel>, ["~> 3.32.0"])
62
+ s.add_dependency(%q<msgpack>, ["~> 0.4.6"])
63
+ s.add_dependency(%q<hashr>, ["~> 0.0.19"])
64
+ s.add_dependency(%q<bundler>, [">= 1.0.0"])
65
+ s.add_dependency(%q<rspec>, ["~> 2.8.0"])
66
+ s.add_dependency(%q<yard>, ["~> 0.7"])
67
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
68
+ s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
69
+ s.add_dependency(%q<simplecov>, ["~> 0.6.1"])
70
+ s.add_dependency(%q<guard>, ["~> 1.0.0"])
71
+ s.add_dependency(%q<guard-rspec>, ["~> 0.6.0"])
72
+ s.add_dependency(%q<growl>, [">= 0"])
73
+ s.add_dependency(%q<sqlite3>, [">= 0"])
74
+ s.add_dependency(%q<memcache>, [">= 0"])
75
+ s.add_dependency(%q<redis>, [">= 0"])
76
+ s.add_dependency(%q<queencheck>, [">= 0"])
77
+ end
78
+ else
79
+ s.add_dependency(%q<sequel>, ["~> 3.32.0"])
80
+ s.add_dependency(%q<msgpack>, ["~> 0.4.6"])
81
+ s.add_dependency(%q<hashr>, ["~> 0.0.19"])
82
+ s.add_dependency(%q<bundler>, [">= 1.0.0"])
83
+ s.add_dependency(%q<rspec>, ["~> 2.8.0"])
84
+ s.add_dependency(%q<yard>, ["~> 0.7"])
85
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
86
+ s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
87
+ s.add_dependency(%q<simplecov>, ["~> 0.6.1"])
88
+ s.add_dependency(%q<guard>, ["~> 1.0.0"])
89
+ s.add_dependency(%q<guard-rspec>, ["~> 0.6.0"])
90
+ s.add_dependency(%q<growl>, [">= 0"])
91
+ s.add_dependency(%q<sqlite3>, [">= 0"])
92
+ s.add_dependency(%q<memcache>, [">= 0"])
93
+ s.add_dependency(%q<redis>, [">= 0"])
94
+ s.add_dependency(%q<queencheck>, [">= 0"])
95
+ end
96
+ end
97
+
@@ -70,6 +70,54 @@ describe Sequel::Plugins::Cacheable do
70
70
  MemcacheModel.cache_get('MemcacheModel::test').should be_nil
71
71
  end
72
72
  end
73
+
74
+ describe "act as cache" do
75
+ context "Model[40]" do
76
+ before do
77
+ @obj = MemcacheModel[40]
78
+ end
79
+
80
+ it "stored cache" do
81
+ MemcacheModel.cache_get(@obj.cache_key).should == @obj
82
+ end
83
+
84
+ it "restoreble cache data" do
85
+ cached = MessagePack.unpack(MemcacheCli.get(@obj.cache_key))
86
+ cached['string'].should == @obj.string
87
+ Time.at(cached['time'][0], cached['time'][1]).should === @obj.time
88
+ end
89
+
90
+ it "update cache data" do
91
+ @obj.string = 'modified++'
92
+ cached = MessagePack.unpack(MemcacheCli.get(@obj.cache_key))
93
+ cached['string'].should_not == @obj.string
94
+ @obj.save
95
+ cached = MessagePack.unpack(MemcacheCli.get(@obj.cache_key))
96
+ cached['string'].should == @obj.string
97
+ end
98
+
99
+ it "delete cache data" do
100
+ cache_key = @obj.cache_key; @obj.delete
101
+ MemcacheCli.get(cache_key).should be_nil
102
+ MemcacheModel[40].should be_nil
103
+ end
104
+
105
+ it "destroy cache data" do
106
+ @obj = MemcacheModel[41]
107
+ cache_key = @obj.cache_key; @obj.destroy
108
+ MemcacheCli.get(cache_key).should be_nil
109
+ MemcacheModel[41].should be_nil
110
+ end
111
+ end
112
+ end
113
+
114
+ describe "query cache" do
115
+ it "get" do
116
+ MemcacheModel.all.should == MemcacheModel.all
117
+ end
118
+
119
+ pending "not supported query cache on Memcache Client"
120
+ end
73
121
  end
74
122
 
75
123
  context RedisModel do
@@ -115,8 +163,14 @@ describe Sequel::Plugins::Cacheable do
115
163
  @obj = RedisModel[50]
116
164
  end
117
165
 
166
+ it "unstored cache on new" do
167
+ before_count = RedisCli.keys.length
168
+ RedisModel.new({:string => 'test'})
169
+ RedisCli.keys.length.should == before_count
170
+ end
171
+
118
172
  it "stored cache" do
119
- RedisCli.keys(@obj.cache_key).should == [@obj.cache_key]
173
+ RedisModel.cache_get(@obj.cache_key).should == @obj
120
174
  end
121
175
 
122
176
  it "restoreble cache data" do
@@ -151,12 +205,14 @@ describe Sequel::Plugins::Cacheable do
151
205
 
152
206
  describe "query cache" do
153
207
  it "set and get" do
208
+ RedisModel.clear_query_cache
154
209
  models = RedisModel.limit(3).all
155
210
  cache_key = RedisCli.keys('RedisModel::Query::*')[0]
156
211
  RedisModel.cache_get(cache_key).should == models
157
212
  end
158
213
 
159
214
  it "clear on update" do
215
+ RedisModel.clear_query_cache
160
216
  RedisModel.all
161
217
  cache_key = RedisCli.keys('RedisModel::Query::*')
162
218
  cache_key.should_not be_empty
@@ -165,12 +221,22 @@ describe Sequel::Plugins::Cacheable do
165
221
  end
166
222
 
167
223
  it "clear on delete" do
224
+ RedisModel.clear_query_cache
168
225
  RedisModel.all
169
226
  cache_key = RedisCli.keys('RedisModel::Query::*')
170
227
  cache_key.should_not be_empty
171
228
  RedisModel[2].delete
172
229
  RedisCli.keys('RedisModel::Query::*').should be_empty
173
230
  end
231
+
232
+ it "cacheout rescue" do
233
+ RedisModel.clear_query_cache
234
+ caches = RedisModel.limit(20).all
235
+ cache_key = RedisCli.keys('RedisModel::Query::*')[0]
236
+ cache = caches[rand(caches.length-1)]
237
+ RedisModel.cache_del(cache.cache_key)
238
+ RedisModel.cache_get(cache_key).should == caches
239
+ end
174
240
  end
175
241
  end
176
242
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sequel-cacheable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-02-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sequel
16
- requirement: &70115283680940 !ruby/object:Gem::Requirement
16
+ requirement: &70185903522420 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 3.32.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70115283680940
24
+ version_requirements: *70185903522420
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: msgpack
27
- requirement: &70115283680240 !ruby/object:Gem::Requirement
27
+ requirement: &70185903521800 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 0.4.6
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70115283680240
35
+ version_requirements: *70185903521800
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: hashr
38
- requirement: &70115283678820 !ruby/object:Gem::Requirement
38
+ requirement: &70185903536900 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 0.0.19
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70115283678820
46
+ version_requirements: *70185903536900
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: bundler
49
- requirement: &70115283693140 !ruby/object:Gem::Requirement
49
+ requirement: &70185903534780 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 1.0.0
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70115283693140
57
+ version_requirements: *70185903534780
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: rspec
60
- requirement: &70115283690620 !ruby/object:Gem::Requirement
60
+ requirement: &70185903532880 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ~>
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: 2.8.0
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70115283690620
68
+ version_requirements: *70185903532880
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: yard
71
- requirement: &70115283689440 !ruby/object:Gem::Requirement
71
+ requirement: &70185903531420 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ~>
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0.7'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *70115283689440
79
+ version_requirements: *70185903531420
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: rdoc
82
- requirement: &70115283688660 !ruby/object:Gem::Requirement
82
+ requirement: &70185903530120 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ~>
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: '3.12'
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *70115283688660
90
+ version_requirements: *70185903530120
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: jeweler
93
- requirement: &70115283688000 !ruby/object:Gem::Requirement
93
+ requirement: &70185903529220 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ~>
@@ -98,10 +98,10 @@ dependencies:
98
98
  version: 1.8.3
99
99
  type: :development
100
100
  prerelease: false
101
- version_requirements: *70115283688000
101
+ version_requirements: *70185903529220
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: simplecov
104
- requirement: &70115283687380 !ruby/object:Gem::Requirement
104
+ requirement: &70185903544780 !ruby/object:Gem::Requirement
105
105
  none: false
106
106
  requirements:
107
107
  - - ~>
@@ -109,10 +109,10 @@ dependencies:
109
109
  version: 0.6.1
110
110
  type: :development
111
111
  prerelease: false
112
- version_requirements: *70115283687380
112
+ version_requirements: *70185903544780
113
113
  - !ruby/object:Gem::Dependency
114
114
  name: guard
115
- requirement: &70115283686480 !ruby/object:Gem::Requirement
115
+ requirement: &70185903544040 !ruby/object:Gem::Requirement
116
116
  none: false
117
117
  requirements:
118
118
  - - ~>
@@ -120,10 +120,10 @@ dependencies:
120
120
  version: 1.0.0
121
121
  type: :development
122
122
  prerelease: false
123
- version_requirements: *70115283686480
123
+ version_requirements: *70185903544040
124
124
  - !ruby/object:Gem::Dependency
125
125
  name: guard-rspec
126
- requirement: &70115283701860 !ruby/object:Gem::Requirement
126
+ requirement: &70185903543480 !ruby/object:Gem::Requirement
127
127
  none: false
128
128
  requirements:
129
129
  - - ~>
@@ -131,10 +131,10 @@ dependencies:
131
131
  version: 0.6.0
132
132
  type: :development
133
133
  prerelease: false
134
- version_requirements: *70115283701860
134
+ version_requirements: *70185903543480
135
135
  - !ruby/object:Gem::Dependency
136
136
  name: growl
137
- requirement: &70115283700860 !ruby/object:Gem::Requirement
137
+ requirement: &70185903542320 !ruby/object:Gem::Requirement
138
138
  none: false
139
139
  requirements:
140
140
  - - ! '>='
@@ -142,10 +142,10 @@ dependencies:
142
142
  version: '0'
143
143
  type: :development
144
144
  prerelease: false
145
- version_requirements: *70115283700860
145
+ version_requirements: *70185903542320
146
146
  - !ruby/object:Gem::Dependency
147
147
  name: sqlite3
148
- requirement: &70115283699440 !ruby/object:Gem::Requirement
148
+ requirement: &70185903541080 !ruby/object:Gem::Requirement
149
149
  none: false
150
150
  requirements:
151
151
  - - ! '>='
@@ -153,10 +153,10 @@ dependencies:
153
153
  version: '0'
154
154
  type: :development
155
155
  prerelease: false
156
- version_requirements: *70115283699440
156
+ version_requirements: *70185903541080
157
157
  - !ruby/object:Gem::Dependency
158
158
  name: memcache
159
- requirement: &70115283698020 !ruby/object:Gem::Requirement
159
+ requirement: &70185903539780 !ruby/object:Gem::Requirement
160
160
  none: false
161
161
  requirements:
162
162
  - - ! '>='
@@ -164,10 +164,10 @@ dependencies:
164
164
  version: '0'
165
165
  type: :development
166
166
  prerelease: false
167
- version_requirements: *70115283698020
167
+ version_requirements: *70185903539780
168
168
  - !ruby/object:Gem::Dependency
169
169
  name: redis
170
- requirement: &70115283696780 !ruby/object:Gem::Requirement
170
+ requirement: &70185903553420 !ruby/object:Gem::Requirement
171
171
  none: false
172
172
  requirements:
173
173
  - - ! '>='
@@ -175,10 +175,10 @@ dependencies:
175
175
  version: '0'
176
176
  type: :development
177
177
  prerelease: false
178
- version_requirements: *70115283696780
178
+ version_requirements: *70185903553420
179
179
  - !ruby/object:Gem::Dependency
180
180
  name: queencheck
181
- requirement: &70115283695460 !ruby/object:Gem::Requirement
181
+ requirement: &70185903552000 !ruby/object:Gem::Requirement
182
182
  none: false
183
183
  requirements:
184
184
  - - ! '>='
@@ -186,7 +186,7 @@ dependencies:
186
186
  version: '0'
187
187
  type: :development
188
188
  prerelease: false
189
- version_requirements: *70115283695460
189
+ version_requirements: *70185903552000
190
190
  description: This plug-in caching mechanism to implement the Model of the Sequel
191
191
  email: rosylilly@aduca.org
192
192
  executables: []
@@ -205,6 +205,7 @@ files:
205
205
  - Rakefile
206
206
  - VERSION
207
207
  - lib/sequel-cacheable.rb
208
+ - sequel-cacheable.gemspec
208
209
  - spec/lib/sequel-cacheable_spec.rb
209
210
  - spec/spec_helper.rb
210
211
  homepage: http://github.com/rosylilly/sequel-cacheable
@@ -222,7 +223,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
222
223
  version: '0'
223
224
  segments:
224
225
  - 0
225
- hash: -213750302155396124
226
+ hash: -3808227649424025746
226
227
  required_rubygems_version: !ruby/object:Gem::Requirement
227
228
  none: false
228
229
  requirements: