padrino-cache 0.11.4 → 0.12.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,68 @@
1
+ module Padrino
2
+ module Cache
3
+ class LegacyStore < Moneta::Proxy
4
+ ##
5
+ # Return the a value for the given key
6
+ #
7
+ # @param [String] key
8
+ # cache key
9
+ #
10
+ # @example
11
+ # # with MyApp.cache.set('records', records)
12
+ # MyApp.cache.get('records')
13
+ #
14
+ # @api public
15
+ def get(key)
16
+ warn 'cache.get(key) has been deprecated in favour of cache[key]'
17
+ self[key]
18
+ end
19
+
20
+ ##
21
+ # Set the value for a given key and optionally with an expire time
22
+ # Default expiry time is 86400.
23
+ #
24
+ # @param [String] key
25
+ # cache key
26
+ # @param value
27
+ # value of cache key
28
+ #
29
+ # @example
30
+ # MyApp.cache.set('records', records)
31
+ # MyApp.cache.set('records', records, :expires_in => 30) # => 30 seconds
32
+ #
33
+ # @api public
34
+ def set(key, value, opts = nil)
35
+ warn opts ? 'cache.set(key, value, opts) has been deprecated in favour of cache.store(key, value, opts)' :
36
+ 'cache.set(key, value) has been deprecated in favour of cache[key] = value'
37
+ store(key, value, opts || {})
38
+ end
39
+
40
+ ##
41
+ # Reinitialize your cache
42
+ #
43
+ # @example
44
+ # # with: MyApp.cache.set('records', records)
45
+ # MyApp.cache.flush
46
+ # MyApp.cache.get('records') # => nil
47
+ #
48
+ # @api public
49
+ def flush
50
+ warn 'cache.flush has been deprecated in favour of cache.clear'
51
+ clear
52
+ end
53
+
54
+ # (see Moneta::Proxy#store)
55
+ def store(key, value, options = {})
56
+ if options[:expires_in]
57
+ warn 'Option :expires_in has been deprecated in favour of :expires'
58
+ options[:expires] = options.delete(:expires_in).to_i
59
+ end
60
+ if options[:expires] && options[:expires] < 0
61
+ warn 'The use of negative expiration values is deprecated, use :expires => false'
62
+ options[:expires] = false
63
+ end
64
+ adapter.store(key, value, options)
65
+ end
66
+ end
67
+ end
68
+ end
@@ -14,6 +14,7 @@ Gem::Specification.new do |s|
14
14
  s.required_rubygems_version = ">= 1.3.6"
15
15
  s.version = Padrino.version
16
16
  s.date = Time.now.strftime("%Y-%m-%d")
17
+ s.license = "MIT"
17
18
 
18
19
  s.extra_rdoc_files = Dir["*.rdoc"]
19
20
  s.files = `git ls-files`.split("\n")
@@ -24,4 +25,5 @@ Gem::Specification.new do |s|
24
25
 
25
26
  s.add_runtime_dependency("padrino-core", Padrino.version)
26
27
  s.add_runtime_dependency("padrino-helpers", Padrino.version)
28
+ s.add_runtime_dependency("moneta", ["~> 0.7.0"])
27
29
  end
data/test/helper.rb CHANGED
@@ -3,41 +3,4 @@ PADRINO_ROOT = File.dirname(__FILE__) unless defined?(PADRINO_ROOT)
3
3
 
4
4
  require File.expand_path('../../../load_paths', __FILE__)
5
5
  require File.join(File.dirname(__FILE__), '..', '..', 'padrino-core', 'test', 'helper')
6
- require 'uuid'
7
6
  require 'padrino-cache'
8
-
9
- class MiniTest::Spec
10
-
11
- def executable_on_path(binary)
12
- @matches = []
13
-
14
- ENV['PATH'].split(":").each do |path|
15
- bintest = File.executable?("#{path}/#{binary}")
16
- pathmatch = "#{path}/#{binary}"
17
- @matches << pathmatch if bintest == true
18
- end
19
-
20
- @matches.length == 1 ? @matches.first : false
21
-
22
- end
23
- end
24
-
25
- MiniTest::Spec.class_eval do
26
- def self.shared_examples
27
- @shared_examples ||= {}
28
- end
29
- end
30
-
31
- module MiniTest::Spec::SharedExamples
32
- def shared_examples_for(desc, &block)
33
- MiniTest::Spec.shared_examples[desc] = block
34
- end
35
-
36
- def it_behaves_like(desc)
37
- self.instance_eval do
38
- MiniTest::Spec.shared_examples[desc].call
39
- end
40
- end
41
- end
42
-
43
- Object.class_eval { include(MiniTest::Spec::SharedExamples) }
@@ -0,0 +1,53 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/helper')
2
+
3
+ describe Padrino::Cache::LegacyStore do
4
+ def setup
5
+ @test_key = "val_#{Time.now.to_i}"
6
+ end
7
+
8
+ def teardown
9
+ Padrino.cache.clear
10
+ end
11
+
12
+ class Foo
13
+ def bar; "bar"; end
14
+ end
15
+
16
+ it "return nil trying to get a value that doesn't exist" do
17
+ Padrino.cache.flush
18
+ assert_equal nil, Padrino.cache.get(@test_key)
19
+ end
20
+
21
+ it 'set and get an object with marshal' do
22
+ Padrino.cache.set(@test_key, Foo.new)
23
+ assert_equal "bar", Padrino.cache.get(@test_key).bar
24
+ end
25
+
26
+ it 'set and get a nil value' do
27
+ Padrino.cache.set(@test_key, nil)
28
+ assert_equal '', Padrino.cache.get(@test_key).to_s
29
+ end
30
+
31
+ it 'set and get a raw value' do
32
+ Padrino.cache.set(@test_key, 'foo')
33
+ assert_equal 'foo', Padrino.cache.get(@test_key)
34
+ end
35
+
36
+ it "set a value that expires" do
37
+ init_time = ( Time.now - 20 )
38
+ Time.stub(:now, init_time) { Padrino.cache.set(@test_key, 'test', :expires_in => 1) }
39
+ Time.stub(:now, init_time + 20) { assert_equal nil, Padrino.cache.get(@test_key) }
40
+ end
41
+
42
+ it "be able to cache forever" do
43
+ Padrino.cache.set('forever', 'cached', :expires_in => -1)
44
+ 2.times { |i| assert_equal 'cached', Padrino.cache.get('forever') }
45
+ end
46
+
47
+ it 'delete a value' do
48
+ Padrino.cache.set(@test_key, 'test')
49
+ assert_equal 'test', Padrino.cache.get(@test_key)
50
+ Padrino.cache.delete(@test_key)
51
+ assert_equal nil, Padrino.cache.get(@test_key)
52
+ end
53
+ end
@@ -0,0 +1,53 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/helper')
2
+
3
+ describe 'Padrino::Cache - Moneta store' do
4
+ def setup
5
+ @test_key = "val_#{Time.now.to_i}"
6
+ end
7
+
8
+ def teardown
9
+ Padrino.cache.clear
10
+ end
11
+
12
+ class Foo
13
+ def bar; "bar"; end
14
+ end
15
+
16
+ it "return nil trying to get a value that doesn't exist" do
17
+ Padrino.cache.clear
18
+ assert_equal nil, Padrino.cache[@test_key]
19
+ end
20
+
21
+ it 'set and get an object with marshal' do
22
+ Padrino.cache[@test_key] = Foo.new
23
+ assert_equal "bar", Padrino.cache[@test_key].bar
24
+ end
25
+
26
+ it 'set and get a nil value' do
27
+ Padrino.cache[@test_key] = nil
28
+ assert_equal '', Padrino.cache[@test_key].to_s
29
+ end
30
+
31
+ it 'set and get a raw value' do
32
+ Padrino.cache[@test_key] = 'foo'
33
+ assert_equal 'foo', Padrino.cache[@test_key]
34
+ end
35
+
36
+ it "set a value that expires" do
37
+ init_time = ( Time.now - 20 )
38
+ Time.stub(:now, init_time) { Padrino.cache.store(@test_key, 'test', :expires => 1) }
39
+ Time.stub(:now, init_time + 20) { assert_equal nil, Padrino.cache[@test_key] }
40
+ end
41
+
42
+ it "be able to cache forever" do
43
+ Padrino.cache.store('forever', 'cached', :expires => false)
44
+ 2.times { |i| assert_equal 'cached', Padrino.cache['forever'] }
45
+ end
46
+
47
+ it 'delete a value' do
48
+ Padrino.cache[@test_key] = 'test'
49
+ assert_equal 'test', Padrino.cache[@test_key]
50
+ Padrino.cache.delete(@test_key)
51
+ assert_equal nil, Padrino.cache[@test_key]
52
+ end
53
+ end
@@ -90,14 +90,14 @@ describe "PadrinoCache" do
90
90
  get "/foo"
91
91
  assert_equal 200, status
92
92
  assert_equal 'foo', body
93
- assert_equal 'foo', @app.cache.get(:foo)
93
+ assert_equal 'foo', @app.cache[:foo][:body]
94
94
  get "/foo"
95
95
  assert_equal 'foo', body
96
96
 
97
97
  get "/bar"
98
98
  assert_equal 200, status
99
99
  assert_equal 'bar', body
100
- assert_equal 'bar', @app.cache.get(:bar)
100
+ assert_equal 'bar', @app.cache[:bar][:body]
101
101
  get "/bar"
102
102
  assert_equal 'bar', body
103
103
  end
@@ -160,7 +160,7 @@ describe "PadrinoCache" do
160
160
  enable :caching
161
161
  controller :cache => true do
162
162
  get("/foo") {
163
- expires_in 1
163
+ expires 1
164
164
  called ? 'test again' : (called = 'test')
165
165
  }
166
166
  end
@@ -184,8 +184,8 @@ describe "PadrinoCache" do
184
184
  enable :caching
185
185
  controller do
186
186
  get("/foo") {
187
- expires_in 1
188
- cache(:test, :expires_in => 2) do
187
+ expires 1
188
+ cache(:test, :expires => 2) do
189
189
  called ? 'test again' : (called = 'test')
190
190
  end
191
191
  }
@@ -197,7 +197,7 @@ describe "PadrinoCache" do
197
197
  get "/foo"
198
198
  assert_equal 200, status
199
199
  assert_equal 'test', body
200
- sleep 2
200
+ sleep 3
201
201
  get "/foo"
202
202
  assert_equal 200, status
203
203
  assert_equal 'test again', body
@@ -231,14 +231,14 @@ describe "PadrinoCache" do
231
231
  get '/404'
232
232
  assert_equal 'fancy 404', body
233
233
  assert_equal 404, status
234
- assert_equal nil, @app.cache.get('/404')
234
+ assert_equal nil, @app.cache['/404']
235
235
  get '/404'
236
236
  assert_equal 'fancy 404', body
237
237
  assert_equal 404, status
238
238
  get '/503'
239
239
  assert_equal 'fancy 503', body
240
240
  assert_equal 503, status
241
- assert_equal nil, @app.cache.get('/503')
241
+ assert_equal nil, @app.cache['/503']
242
242
  get '/503'
243
243
  assert_equal 'fancy 503', body
244
244
  assert_equal 503, status
@@ -246,10 +246,10 @@ describe "PadrinoCache" do
246
246
 
247
247
  should 'cache should not hit with unique params' do
248
248
  call_count = 0
249
- mock_app do
249
+ mock_app do
250
250
  register Padrino::Cache
251
251
  enable :caching
252
- before do
252
+ before do
253
253
  param = params[:test] || 'none'
254
254
  cache_key "foo?#{param}"
255
255
  end
@@ -273,7 +273,7 @@ describe "PadrinoCache" do
273
273
 
274
274
  should 'resolve block cache keys' do
275
275
  call_count = 0
276
- mock_app do
276
+ mock_app do
277
277
  register Padrino::Cache
278
278
  enable :caching
279
279
 
@@ -294,7 +294,7 @@ describe "PadrinoCache" do
294
294
  end
295
295
 
296
296
  should 'raise an error if providing both a cache_key and block' do
297
- mock_app do
297
+ mock_app do
298
298
  register Padrino::Cache
299
299
  enable :caching
300
300
 
@@ -306,4 +306,29 @@ describe "PadrinoCache" do
306
306
  assert_raises(RuntimeError) { get '/foo' }
307
307
  end
308
308
 
309
+ should 'cache content_type' do
310
+ called = false
311
+ mock_app do
312
+ register Padrino::Cache
313
+ enable :caching
314
+ get '/foo', :cache => true do
315
+ content_type :json
316
+ if called
317
+ "you'll never see me"
318
+ else
319
+ cache_key :foo
320
+ called = '{"foo":"bar"}'
321
+
322
+ called
323
+ end
324
+ end
325
+ end
326
+ get "/foo"
327
+ assert_equal 200, status
328
+ assert_equal '{"foo":"bar"}', body
329
+ assert_equal '{"foo":"bar"}', @app.cache[:foo][:body]
330
+ get "/foo"
331
+ assert_equal '{"foo":"bar"}', body
332
+ assert_match /json/, last_response.content_type
333
+ end
309
334
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: padrino-cache
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.4
4
+ version: 0.12.0.rc1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Padrino Team
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2013-09-24 00:00:00.000000000 Z
14
+ date: 2013-12-31 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: padrino-core
@@ -19,28 +19,42 @@ dependencies:
19
19
  requirements:
20
20
  - - '='
21
21
  - !ruby/object:Gem::Version
22
- version: 0.11.4
22
+ version: 0.12.0.rc1
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
27
  - - '='
28
28
  - !ruby/object:Gem::Version
29
- version: 0.11.4
29
+ version: 0.12.0.rc1
30
30
  - !ruby/object:Gem::Dependency
31
31
  name: padrino-helpers
32
32
  requirement: !ruby/object:Gem::Requirement
33
33
  requirements:
34
34
  - - '='
35
35
  - !ruby/object:Gem::Version
36
- version: 0.11.4
36
+ version: 0.12.0.rc1
37
37
  type: :runtime
38
38
  prerelease: false
39
39
  version_requirements: !ruby/object:Gem::Requirement
40
40
  requirements:
41
41
  - - '='
42
42
  - !ruby/object:Gem::Version
43
- version: 0.11.4
43
+ version: 0.12.0.rc1
44
+ - !ruby/object:Gem::Dependency
45
+ name: moneta
46
+ requirement: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ~>
49
+ - !ruby/object:Gem::Version
50
+ version: 0.7.0
51
+ type: :runtime
52
+ prerelease: false
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ~>
56
+ - !ruby/object:Gem::Version
57
+ version: 0.7.0
44
58
  description: Caching support for memcached, page and fragment
45
59
  email: padrinorb@gmail.com
46
60
  executables: []
@@ -58,26 +72,15 @@ files:
58
72
  - lib/padrino-cache/helpers/cache_store.rb
59
73
  - lib/padrino-cache/helpers/fragment.rb
60
74
  - lib/padrino-cache/helpers/page.rb
61
- - lib/padrino-cache/parser.rb
62
- - lib/padrino-cache/store.rb
63
- - lib/padrino-cache/store/base.rb
64
- - lib/padrino-cache/store/file.rb
65
- - lib/padrino-cache/store/memcache.rb
66
- - lib/padrino-cache/store/memory.rb
67
- - lib/padrino-cache/store/mongo.rb
68
- - lib/padrino-cache/store/redis.rb
75
+ - lib/padrino-cache/legacy_store.rb
69
76
  - padrino-cache.gemspec
70
77
  - test/helper.rb
71
- - test/stores/shared.rb
72
- - test/stores/test_dalli.rb
73
- - test/stores/test_file.rb
74
- - test/stores/test_memcache.rb
75
- - test/stores/test_memory.rb
76
- - test/stores/test_mongo.rb
77
- - test/stores/test_redis.rb
78
+ - test/test_legacy_store.rb
79
+ - test/test_moneta_store.rb
78
80
  - test/test_padrino_cache.rb
79
81
  homepage: http://www.padrinorb.com
80
- licenses: []
82
+ licenses:
83
+ - MIT
81
84
  metadata: {}
82
85
  post_install_message:
83
86
  rdoc_options:
@@ -91,9 +94,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
91
94
  version: '0'
92
95
  required_rubygems_version: !ruby/object:Gem::Requirement
93
96
  requirements:
94
- - - '>='
97
+ - - '>'
95
98
  - !ruby/object:Gem::Version
96
- version: 1.3.6
99
+ version: 1.3.1
97
100
  requirements: []
98
101
  rubyforge_project: padrino-cache
99
102
  rubygems_version: 2.0.6
@@ -102,12 +105,7 @@ specification_version: 4
102
105
  summary: Page and fragment caching for Padrino
103
106
  test_files:
104
107
  - test/helper.rb
105
- - test/stores/shared.rb
106
- - test/stores/test_dalli.rb
107
- - test/stores/test_file.rb
108
- - test/stores/test_memcache.rb
109
- - test/stores/test_memory.rb
110
- - test/stores/test_mongo.rb
111
- - test/stores/test_redis.rb
108
+ - test/test_legacy_store.rb
109
+ - test/test_moneta_store.rb
112
110
  - test/test_padrino_cache.rb
113
111
  has_rdoc: