sequel-cacheable 0.0.7 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,161 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples :cacheable do
4
+ let(:model) { described_class }
5
+
6
+ before do
7
+ model.cache_clear(:query)
8
+
9
+ 3.times do
10
+ model.create(
11
+ :string => Forgery::Basic.text,
12
+ :integer => rand(255),
13
+ :float => rand(3000).to_f / 10.0,
14
+ :bignum => (2 + rand(10)) ** 100,
15
+ :numeric => BigDecimal(rand(100).to_s),
16
+ :date => Date.today,
17
+ :datetime => DateTime.now,
18
+ :time => Time.now,
19
+ :bool => rand(2).odd?
20
+ )
21
+ end
22
+ end
23
+
24
+ describe 'ClassMethods' do
25
+ let(:key) { 'cache_key' }
26
+ let(:value) { model.first }
27
+
28
+ describe '#cache_set' do
29
+ subject(:cache_set) { model.cache_set(key, value) }
30
+
31
+ it { should == value }
32
+
33
+ it 'should be stored cache' do
34
+ cache_set
35
+ model.cache_driver.get("#{model.name}:#{key}").should == value
36
+ end
37
+ end
38
+
39
+ describe '#cache_get' do
40
+ before do
41
+ model.cache_set(key, value)
42
+ end
43
+
44
+ subject { model.cache_get(key) }
45
+
46
+ it { should == value }
47
+ end
48
+
49
+ describe '#cache_del' do
50
+ before do
51
+ model.cache_set(key, value)
52
+ end
53
+
54
+ subject(:cache_del) { model.cache_del(key) }
55
+
56
+ it { should be_nil }
57
+
58
+ it 'should be deleted cache' do
59
+ cache_del
60
+ model.cache_get("#{model.name}:#{key}").should be_nil
61
+ end
62
+ end
63
+
64
+ describe '#cache_fetch' do
65
+ it 'should call driver#fetch' do
66
+ model.cache_driver.should_receive(:fetch).at_least(1).times.and_call_original
67
+
68
+ model.cache_fetch('test') do
69
+ value
70
+ end
71
+ end
72
+ end
73
+ end
74
+
75
+ describe 'DatasetMethods' do
76
+ let(:dataset) { model.dataset }
77
+
78
+ describe '#all' do
79
+ subject(:fetch_all) { dataset.all }
80
+
81
+ it { should have(3).records }
82
+
83
+ it { should == model.all }
84
+
85
+ it 'should store query cache' do
86
+ expect { fetch_all }.to change { model.caches[:query].size }.from(0).to(1)
87
+ end
88
+ end
89
+
90
+ describe '#first' do
91
+ subject(:fetch_first) { dataset.first }
92
+
93
+ it { should be_kind_of(model) }
94
+
95
+ it 'should store query cache' do
96
+ expect { fetch_first }.to change { model.caches[:query].size }.from(0).to(1)
97
+ end
98
+ end
99
+ end
100
+
101
+ describe 'InstanceMethods' do
102
+ let(:instance) { model.first }
103
+
104
+ describe '#after_initialize' do
105
+ it 'should call #cache!' do
106
+ model.any_instance.should_receive(:cache!)
107
+ model.first
108
+ end
109
+ end
110
+
111
+ describe '#after_update' do
112
+ it 'should call #recache!' do
113
+ instance.string = 'hoge'
114
+ instance.should_receive(:recache!)
115
+ instance.save
116
+ end
117
+ end
118
+
119
+ describe '#destory' do
120
+ it 'should call #uncache!' do
121
+ instance.should_receive(:uncache!)
122
+ instance.destroy
123
+ end
124
+ end
125
+
126
+ describe '#delete' do
127
+ it 'should call #uncache!' do
128
+ instance.should_receive(:uncache!)
129
+ instance.delete
130
+ end
131
+ end
132
+
133
+ describe '#cache!' do
134
+ it 'should call .cache_set' do
135
+ instance = model.first
136
+ model.should_receive(:cache_set).with(instance.id.to_s, instance)
137
+ instance.cache!
138
+ end
139
+ end
140
+
141
+ describe '#uncache!' do
142
+ it 'should call .cache_del' do
143
+ model.should_receive(:cache_del).at_least(1).times
144
+ instance.uncache!
145
+ end
146
+
147
+ it 'should call .cache_clear(:query)' do
148
+ model.should_receive(:cache_clear).with(:query)
149
+ instance.uncache!
150
+ end
151
+ end
152
+
153
+ describe '#recache!' do
154
+ it 'should call #uncache! and #cache!' do
155
+ instance.should_receive(:uncache!)
156
+ instance.should_receive(:cache!)
157
+ instance.recache!
158
+ end
159
+ end
160
+ end
161
+ end
@@ -0,0 +1,102 @@
1
+ shared_examples :driver do
2
+ let(:pack_lib) { MessagePack }
3
+ subject(:driver) { described_class.new(store, pack_lib) }
4
+
5
+ let(:key) { 'cache_key' }
6
+ let(:val) { 100 }
7
+
8
+ describe '#set' do
9
+ subject { driver.set(key, val) }
10
+
11
+ it { should == val }
12
+
13
+ it 'should be stored in cache' do
14
+ driver.set(key, val)
15
+ store.get(key).should_not be_nil
16
+ end
17
+
18
+ context 'with expire' do
19
+ subject { driver.set(key, val, -1) }
20
+
21
+ it 'should be expired cache' do
22
+ store.get(key).should be_nil
23
+ end
24
+ end
25
+ end
26
+
27
+ describe '#get' do
28
+ before do
29
+ driver.set(key, val)
30
+ end
31
+
32
+ let(:get_key) { key }
33
+
34
+ subject { driver.get(get_key) }
35
+
36
+ context 'be found key' do
37
+ it { should == val }
38
+ end
39
+
40
+ context 'be not found key' do
41
+ let(:get_key) { 'not_found' }
42
+
43
+ it { should be_nil }
44
+ end
45
+ end
46
+
47
+ describe '#del' do
48
+ before do
49
+ driver.set(key, val)
50
+ end
51
+
52
+ subject(:del_method) { driver.del(key) }
53
+
54
+ it { should be_nil }
55
+
56
+ it 'should be deleted cache' do
57
+ store.get(key).should_not be_nil
58
+ del_method
59
+ store.get(key).should be_nil
60
+ end
61
+ end
62
+
63
+ describe '#expire' do
64
+ before do
65
+ driver.set(key, val)
66
+ end
67
+
68
+ it 'should be expired cache' do
69
+ store.get(key).should_not be_nil
70
+ driver.expire(key, -1)
71
+ store.get(key).should be_nil
72
+ end
73
+ end
74
+
75
+ describe '#fetch' do
76
+ subject(:fetch) do
77
+ driver.fetch(key) { val }
78
+ end
79
+
80
+ context 'be found key' do
81
+ before do
82
+ driver.set(key, val)
83
+ end
84
+
85
+ it { should == val }
86
+
87
+ it 'should not call #set' do
88
+ store.should_not_receive(:set)
89
+ fetch
90
+ end
91
+ end
92
+
93
+ context 'be not found key' do
94
+ it { should == val }
95
+
96
+ it 'should call #set' do
97
+ store.should_receive(:set).and_call_original
98
+ fetch
99
+ end
100
+ end
101
+ end
102
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,38 +1,35 @@
1
1
  require 'rubygems'
2
2
  require 'bundler'
3
- Bundler.setup(:default, :development)
3
+ Bundler.require(:default, :test)
4
4
 
5
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
- $LOAD_PATH.unshift(File.dirname(__FILE__))
7
- require 'rspec'
8
- require 'sequel-cacheable'
9
- require 'redis'
10
- require 'memcache'
11
- require 'queencheck/rspec'
5
+ DB = Sequel.sqlite
6
+ DB.create_table(:spec) do
7
+ primary_key :id, :auto_increment => true
8
+ String :string
9
+ Integer :integer
10
+ Float :float
11
+ Bignum :bignum
12
+ BigDecimal :numeric
13
+ Date :date
14
+ DateTime :datetime
15
+ Time :time, :only_time=>true
16
+ TrueClass :bool
17
+ end
12
18
 
13
- # Requires supporting files with custom matchers and macros, etc,
14
- # in ./support/ and its subdirectories.
15
19
  Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
20
+ Dir["#{File.dirname(__FILE__)}/shared/**/*.rb"].each {|f| require f}
16
21
 
17
22
  RSpec.configure do |config|
18
- DB = Sequel.sqlite
19
- DB.create_table(:spec) do
20
- primary_key :id, :auto_increment => true
21
- String :string
22
- Integer :integer
23
- Float :float
24
- Bignum :bignum
25
- BigDecimal :numeric
26
- Date :date
27
- DateTime :datetime
28
- Time :time, :only_time=>true
29
- TrueClass :bool
30
- end
31
- RedisCli = Redis.new(:host => 'localhost', :port => 6379)
32
- MemcacheCli = Memcache.new(:server => 'localhost:11211')
33
-
34
- config.after(:all) {
23
+ config.after(:each) do
35
24
  RedisCli.flushall
36
25
  MemcacheCli.flush_all
37
- }
26
+ DalliCli.flush_all
27
+ end
28
+
29
+ config.around(:each) do |e|
30
+ DB.transaction do
31
+ e.run
32
+ raise Sequel::Rollback
33
+ end
34
+ end
38
35
  end
@@ -0,0 +1,5 @@
1
+ DalliCli = Dalli::Client.new('localhost:11211')
2
+
3
+ class DalliModel < Sequel::Model(:spec)
4
+ plugin :cacheable, DalliCli, :query_cache => true
5
+ end
@@ -0,0 +1,5 @@
1
+ MemcacheCli = Memcache.new(:server => 'localhost:11211')
2
+
3
+ class MemcacheModel < Sequel::Model(:spec)
4
+ plugin :cacheable, MemcacheCli, :query_cache => true
5
+ end
@@ -0,0 +1,5 @@
1
+ RedisCli = Redis.new(:server => 'localhost:6379')
2
+
3
+ class RedisModel < Sequel::Model(:spec)
4
+ plugin :cacheable, RedisCli, :query_cache => true
5
+ end
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sequel-cacheable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
8
- - Sho Kusano <rosylilly>
8
+ - Sho Kusano
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-01 00:00:00.000000000 Z
12
+ date: 2012-12-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sequel
@@ -18,7 +18,7 @@ dependencies:
18
18
  requirements:
19
19
  - - ~>
20
20
  - !ruby/object:Gem::Version
21
- version: '3.34'
21
+ version: '3.42'
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
@@ -26,7 +26,7 @@ dependencies:
26
26
  requirements:
27
27
  - - ~>
28
28
  - !ruby/object:Gem::Version
29
- version: '3.34'
29
+ version: '3.42'
30
30
  - !ruby/object:Gem::Dependency
31
31
  name: msgpack
32
32
  requirement: !ruby/object:Gem::Requirement
@@ -34,7 +34,7 @@ dependencies:
34
34
  requirements:
35
35
  - - ~>
36
36
  - !ruby/object:Gem::Version
37
- version: 0.4.6
37
+ version: 0.5.1
38
38
  type: :runtime
39
39
  prerelease: false
40
40
  version_requirements: !ruby/object:Gem::Requirement
@@ -42,254 +42,52 @@ dependencies:
42
42
  requirements:
43
43
  - - ~>
44
44
  - !ruby/object:Gem::Version
45
- version: 0.4.6
46
- - !ruby/object:Gem::Dependency
47
- name: hashr
48
- requirement: !ruby/object:Gem::Requirement
49
- none: false
50
- requirements:
51
- - - ~>
52
- - !ruby/object:Gem::Version
53
- version: 0.0.19
54
- type: :runtime
55
- prerelease: false
56
- version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
- requirements:
59
- - - ~>
60
- - !ruby/object:Gem::Version
61
- version: 0.0.19
62
- - !ruby/object:Gem::Dependency
63
- name: bundler
64
- requirement: !ruby/object:Gem::Requirement
65
- none: false
66
- requirements:
67
- - - ! '>='
68
- - !ruby/object:Gem::Version
69
- version: 1.0.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: 1.0.0
78
- - !ruby/object:Gem::Dependency
79
- name: rspec
80
- requirement: !ruby/object:Gem::Requirement
81
- none: false
82
- requirements:
83
- - - ~>
84
- - !ruby/object:Gem::Version
85
- version: '2'
86
- type: :development
87
- prerelease: false
88
- version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
- requirements:
91
- - - ~>
92
- - !ruby/object:Gem::Version
93
- version: '2'
94
- - !ruby/object:Gem::Dependency
95
- name: yard
96
- requirement: !ruby/object:Gem::Requirement
97
- none: false
98
- requirements:
99
- - - ~>
100
- - !ruby/object:Gem::Version
101
- version: '0.7'
102
- type: :development
103
- prerelease: false
104
- version_requirements: !ruby/object:Gem::Requirement
105
- none: false
106
- requirements:
107
- - - ~>
108
- - !ruby/object:Gem::Version
109
- version: '0.7'
110
- - !ruby/object:Gem::Dependency
111
- name: rdoc
112
- requirement: !ruby/object:Gem::Requirement
113
- none: false
114
- requirements:
115
- - - ~>
116
- - !ruby/object:Gem::Version
117
- version: '3.12'
118
- type: :development
119
- prerelease: false
120
- version_requirements: !ruby/object:Gem::Requirement
121
- none: false
122
- requirements:
123
- - - ~>
124
- - !ruby/object:Gem::Version
125
- version: '3.12'
126
- - !ruby/object:Gem::Dependency
127
- name: jeweler
128
- requirement: !ruby/object:Gem::Requirement
129
- none: false
130
- requirements:
131
- - - ~>
132
- - !ruby/object:Gem::Version
133
- version: 1.8.3
134
- type: :development
135
- prerelease: false
136
- version_requirements: !ruby/object:Gem::Requirement
137
- none: false
138
- requirements:
139
- - - ~>
140
- - !ruby/object:Gem::Version
141
- version: 1.8.3
142
- - !ruby/object:Gem::Dependency
143
- name: simplecov
144
- requirement: !ruby/object:Gem::Requirement
145
- none: false
146
- requirements:
147
- - - ~>
148
- - !ruby/object:Gem::Version
149
- version: 0.6.1
150
- type: :development
151
- prerelease: false
152
- version_requirements: !ruby/object:Gem::Requirement
153
- none: false
154
- requirements:
155
- - - ~>
156
- - !ruby/object:Gem::Version
157
- version: 0.6.1
158
- - !ruby/object:Gem::Dependency
159
- name: guard
160
- requirement: !ruby/object:Gem::Requirement
161
- none: false
162
- requirements:
163
- - - ~>
164
- - !ruby/object:Gem::Version
165
- version: 1.0.0
166
- type: :development
167
- prerelease: false
168
- version_requirements: !ruby/object:Gem::Requirement
169
- none: false
170
- requirements:
171
- - - ~>
172
- - !ruby/object:Gem::Version
173
- version: 1.0.0
174
- - !ruby/object:Gem::Dependency
175
- name: guard-rspec
176
- requirement: !ruby/object:Gem::Requirement
177
- none: false
178
- requirements:
179
- - - ~>
180
- - !ruby/object:Gem::Version
181
- version: '0.7'
182
- type: :development
183
- prerelease: false
184
- version_requirements: !ruby/object:Gem::Requirement
185
- none: false
186
- requirements:
187
- - - ~>
188
- - !ruby/object:Gem::Version
189
- version: '0.7'
190
- - !ruby/object:Gem::Dependency
191
- name: growl
192
- requirement: !ruby/object:Gem::Requirement
193
- none: false
194
- requirements:
195
- - - ! '>='
196
- - !ruby/object:Gem::Version
197
- version: '0'
198
- type: :development
199
- prerelease: false
200
- version_requirements: !ruby/object:Gem::Requirement
201
- none: false
202
- requirements:
203
- - - ! '>='
204
- - !ruby/object:Gem::Version
205
- version: '0'
206
- - !ruby/object:Gem::Dependency
207
- name: sqlite3
208
- requirement: !ruby/object:Gem::Requirement
209
- none: false
210
- requirements:
211
- - - ! '>='
212
- - !ruby/object:Gem::Version
213
- version: '0'
214
- type: :development
215
- prerelease: false
216
- version_requirements: !ruby/object:Gem::Requirement
217
- none: false
218
- requirements:
219
- - - ! '>='
220
- - !ruby/object:Gem::Version
221
- version: '0'
222
- - !ruby/object:Gem::Dependency
223
- name: memcache
224
- requirement: !ruby/object:Gem::Requirement
225
- none: false
226
- requirements:
227
- - - ! '>='
228
- - !ruby/object:Gem::Version
229
- version: '0'
230
- type: :development
231
- prerelease: false
232
- version_requirements: !ruby/object:Gem::Requirement
233
- none: false
234
- requirements:
235
- - - ! '>='
236
- - !ruby/object:Gem::Version
237
- version: '0'
238
- - !ruby/object:Gem::Dependency
239
- name: redis
240
- requirement: !ruby/object:Gem::Requirement
241
- none: false
242
- requirements:
243
- - - ! '>='
244
- - !ruby/object:Gem::Version
245
- version: '0'
246
- type: :development
247
- prerelease: false
248
- version_requirements: !ruby/object:Gem::Requirement
249
- none: false
250
- requirements:
251
- - - ! '>='
252
- - !ruby/object:Gem::Version
253
- version: '0'
254
- - !ruby/object:Gem::Dependency
255
- name: queencheck
256
- requirement: !ruby/object:Gem::Requirement
257
- none: false
258
- requirements:
259
- - - ! '>='
260
- - !ruby/object:Gem::Version
261
- version: '0'
262
- type: :development
263
- prerelease: false
264
- version_requirements: !ruby/object:Gem::Requirement
265
- none: false
266
- requirements:
267
- - - ! '>='
268
- - !ruby/object:Gem::Version
269
- version: '0'
45
+ version: 0.5.1
270
46
  description: This plug-in caching mechanism to implement the Model of the Sequel
271
- email: rosylilly@aduca.org
47
+ email:
48
+ - rosylilly@aduca.org
272
49
  executables: []
273
50
  extensions: []
274
- extra_rdoc_files:
275
- - LICENSE.txt
276
- - README.rdoc
51
+ extra_rdoc_files: []
277
52
  files:
278
53
  - .document
54
+ - .gitignore
55
+ - .pryrc
56
+ - .rbenv-version
279
57
  - .rspec
280
58
  - .travis.yml
281
59
  - Gemfile
282
- - Gemfile.lock
283
60
  - Guardfile
284
61
  - LICENSE.txt
62
+ - Procfile
285
63
  - README.rdoc
286
64
  - Rakefile
287
- - VERSION
288
65
  - lib/sequel-cacheable.rb
66
+ - lib/sequel-cacheable/class_methods.rb
67
+ - lib/sequel-cacheable/dataset_methods.rb
68
+ - lib/sequel-cacheable/driver.rb
69
+ - lib/sequel-cacheable/driver/dalli.rb
70
+ - lib/sequel-cacheable/driver/memcache.rb
71
+ - lib/sequel-cacheable/driver/redis.rb
72
+ - lib/sequel-cacheable/instance_methods.rb
73
+ - lib/sequel-cacheable/packer.rb
74
+ - lib/sequel-cacheable/version.rb
289
75
  - sequel-cacheable.gemspec
76
+ - spec/lib/sequel-cacheable/driver/dalli_driver_spec.rb
77
+ - spec/lib/sequel-cacheable/driver/memcache_driver_spec.rb
78
+ - spec/lib/sequel-cacheable/driver/redis_driver_spec.rb
79
+ - spec/lib/sequel-cacheable/driver_spec.rb
290
80
  - spec/lib/sequel-cacheable_spec.rb
81
+ - spec/models/dalli_spec.rb
82
+ - spec/models/memcache_spec.rb
83
+ - spec/models/redis_spec.rb
84
+ - spec/shared/cacheable.rb
85
+ - spec/shared/driver.rb
291
86
  - spec/spec_helper.rb
292
- homepage: http://github.com/rosylilly/sequel-cacheable
87
+ - spec/support/models/dalli.rb
88
+ - spec/support/models/memcache.rb
89
+ - spec/support/models/redis.rb
90
+ homepage: https://github.com/rosylilly/sequel-cacheable
293
91
  licenses:
294
92
  - MIT
295
93
  post_install_message:
@@ -304,17 +102,35 @@ required_ruby_version: !ruby/object:Gem::Requirement
304
102
  version: '0'
305
103
  segments:
306
104
  - 0
307
- hash: 1514191392276094341
105
+ hash: -455532070542796088
308
106
  required_rubygems_version: !ruby/object:Gem::Requirement
309
107
  none: false
310
108
  requirements:
311
109
  - - ! '>='
312
110
  - !ruby/object:Gem::Version
313
111
  version: '0'
112
+ segments:
113
+ - 0
114
+ hash: -455532070542796088
314
115
  requirements: []
315
116
  rubyforge_project:
316
- rubygems_version: 1.8.22
117
+ rubygems_version: 1.8.23
317
118
  signing_key:
318
119
  specification_version: 3
319
120
  summary: This plug-in caching mechanism to implement the Model of the Sequel
320
- test_files: []
121
+ test_files:
122
+ - spec/lib/sequel-cacheable/driver/dalli_driver_spec.rb
123
+ - spec/lib/sequel-cacheable/driver/memcache_driver_spec.rb
124
+ - spec/lib/sequel-cacheable/driver/redis_driver_spec.rb
125
+ - spec/lib/sequel-cacheable/driver_spec.rb
126
+ - spec/lib/sequel-cacheable_spec.rb
127
+ - spec/models/dalli_spec.rb
128
+ - spec/models/memcache_spec.rb
129
+ - spec/models/redis_spec.rb
130
+ - spec/shared/cacheable.rb
131
+ - spec/shared/driver.rb
132
+ - spec/spec_helper.rb
133
+ - spec/support/models/dalli.rb
134
+ - spec/support/models/memcache.rb
135
+ - spec/support/models/redis.rb
136
+ has_rdoc: