padrino-cache 0.11.2 → 0.11.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -53,7 +53,7 @@ module Padrino
53
53
  began_at = Time.now
54
54
  if value = settings.cache.get(key.to_s)
55
55
  logger.debug "GET Fragment", began_at, key.to_s if defined?(logger)
56
- concat_content(value)
56
+ concat_content(value.html_safe)
57
57
  else
58
58
  value = capture_html(&block)
59
59
  settings.cache.set(key.to_s, value, opts)
@@ -70,6 +70,8 @@ module Padrino
70
70
  #
71
71
  # @param [Symbol] name
72
72
  # cache key
73
+ # @param [Proc] block
74
+ # block to be evaluated to cache key
73
75
  #
74
76
  # @example
75
77
  # controller '/blog', :cache => true do
@@ -80,9 +82,17 @@ module Padrino
80
82
  # end
81
83
  # end
82
84
  #
85
+ # @example
86
+ # get '/foo', :cache => true do
87
+ # cache_key { param[:id] }
88
+ # "my id is #{param[:id}"
89
+ # end
90
+ # end
91
+ #
83
92
  # @api public
84
- def cache_key(name)
85
- @route.cache_key = name
93
+ def cache_key(name = nil, &block)
94
+ raise "Can not provide both cache_key and a block" if name && block
95
+ @route.cache_key = block_given? ? block : name
86
96
  end
87
97
 
88
98
  # @private
@@ -90,8 +100,9 @@ module Padrino
90
100
  if route.cache and %w(GET HEAD).include?(verb)
91
101
  route.before_filters do
92
102
  if settings.caching?
93
- began_at = Time.now
94
- value = settings.cache.get(@route.cache_key || env['PATH_INFO'])
103
+ began_at = Time.now
104
+
105
+ value = settings.cache.get(resolve_cache_key || env['PATH_INFO'])
95
106
  logger.debug "GET Cache", began_at, @route.cache_key || env['PATH_INFO'] if defined?(logger) && value
96
107
 
97
108
  if value
@@ -103,15 +114,14 @@ module Padrino
103
114
 
104
115
  route.after_filters do
105
116
  if settings.caching? && @_response_buffer.kind_of?(String)
106
- began_at = Time.now
107
-
108
- content = @_response_buffer
117
+ began_at = Time.now
118
+ content = @_response_buffer
109
119
 
110
120
  if @_last_expires_in
111
- settings.cache.set(@route.cache_key || env['PATH_INFO'], content, :expires_in => @_last_expires_in)
121
+ settings.cache.set(resolve_cache_key || env['PATH_INFO'], content, :expires_in => @_last_expires_in)
112
122
  @_last_expires_in = nil
113
123
  else
114
- settings.cache.set(@route.cache_key || env['PATH_INFO'], content)
124
+ settings.cache.set(resolve_cache_key || env['PATH_INFO'], content)
115
125
  end
116
126
 
117
127
  logger.debug "SET Cache", began_at, @route.cache_key || env['PATH_INFO'] if defined?(logger)
@@ -119,6 +129,15 @@ module Padrino
119
129
  end
120
130
  end
121
131
  end
132
+
133
+ private
134
+ ##
135
+ # Resolve the cache_key when it's a block in the correct context
136
+ #@api private
137
+ def resolve_cache_key
138
+ @route.cache_key.is_a?(Proc) ? instance_eval(&@route.cache_key) : @route.cache_key
139
+ end
140
+
122
141
  end # Page
123
142
  end # Helpers
124
143
  end # Cache
@@ -53,9 +53,25 @@ module Padrino
53
53
 
54
54
  # @private
55
55
  def initialize(options={})
56
+ @never = -1
56
57
  self.parser = options[:parser] || :plain
57
58
  end
58
59
 
60
+ private
61
+
62
+ def get_expiry( opts )
63
+ if opts && opts[:expires_in] && opts[:expires_in] != -1
64
+ expires_in = opts[:expires_in].to_i
65
+ expires_in = EXPIRES_EDGE if expires_in > EXPIRES_EDGE
66
+ Time.now.to_i + expires_in
67
+ else
68
+ @never
69
+ end
70
+ end
71
+
72
+ def now_before?( expiry )
73
+ expiry.to_i == @never || expiry.to_i > Time.now.to_i
74
+ end
59
75
  end # Base
60
76
  end # Store
61
77
  end # Cache
@@ -40,9 +40,8 @@ module Padrino
40
40
  if ::File.exist?(path_for_key(key))
41
41
  read_method = ::File.respond_to?(:binread) ? :binread : :read
42
42
  contents = ::File.send(read_method, path_for_key(key))
43
- expires_in, body = contents.split("\n", 2)
44
- expires_in = expires_in.to_i
45
- if expires_in == -1 or Time.new.to_i < expires_in
43
+ expiry, body = contents.split("\n", 2)
44
+ if now_before? expiry
46
45
  parser.decode(body) if body
47
46
  else # expire the key
48
47
  delete(key)
@@ -69,14 +68,8 @@ module Padrino
69
68
  # @api public
70
69
  def set(key, value, opts = nil)
71
70
  init
72
- if opts && opts[:expires_in]
73
- expires_in = opts[:expires_in].to_i
74
- expires_in = Time.new.to_i + expires_in if expires_in < EXPIRES_EDGE
75
- else
76
- expires_in = -1
77
- end
78
71
  value = parser.encode(value) if value
79
- ::File.open(path_for_key(key), 'wb') { |f| f << expires_in.to_s << "\n" << value } if value
72
+ ::File.open(path_for_key(key), 'wb') { |f| f << get_expiry(opts).to_s << "\n" << value } if value
80
73
  end
81
74
 
82
75
  ##
@@ -22,6 +22,7 @@ module Padrino
22
22
  def initialize(client, options={})
23
23
  @backend = client
24
24
  super(options)
25
+ @never = 0 # never TTL in Memcache is 0
25
26
  end
26
27
 
27
28
  ##
@@ -56,13 +57,7 @@ module Padrino
56
57
  #
57
58
  # @api public
58
59
  def set(key, value, opts = nil)
59
- if opts && opts[:expires_in]
60
- expires_in = opts[:expires_in].to_i
61
- expires_in = (@backend.class.name == "MemCache" ? expires_in : Time.new.to_i + expires_in) if expires_in < EXPIRES_EDGE
62
- @backend.set(key, value, expires_in)
63
- else
64
- @backend.set(key, value)
65
- end
60
+ @backend.set(key, value, get_expiry(opts))
66
61
  end
67
62
 
68
63
  ##
@@ -35,9 +35,8 @@ module Padrino
35
35
  # @api public
36
36
  def get(key)
37
37
  if @index.key?(key) and value = @index[key]
38
- expires_in, body = value
39
- if expires_in == -1 or Time.new.to_i < expires_in
40
- set(key, body, :expires_in => expires_in)
38
+ expiry, body = value
39
+ if now_before? expiry
41
40
  body
42
41
  else
43
42
  delete(key)
@@ -63,16 +62,9 @@ module Padrino
63
62
  #
64
63
  # @api public
65
64
  def set(key, value, opts = nil)
66
- delete(key) if @index.key?(key)
67
- if opts && opts[:expires_in]
68
- expires_in = opts[:expires_in].to_i
69
- expires_in = Time.new.to_i + expires_in if expires_in < EXPIRES_EDGE
70
- else
71
- expires_in = -1
72
- end
65
+ delete(key)
73
66
  @entries.push(key)
74
- @index[key] = [expires_in, value]
75
-
67
+ @index[key] = [get_expiry(opts), value]
76
68
  while @entries.size > @size
77
69
  delete(@entries.shift)
78
70
  end
@@ -48,9 +48,15 @@ module Padrino
48
48
  # MyApp.cache.get('records')
49
49
  #
50
50
  def get(key)
51
- doc = @backend.find_one(:_id => key, :expires_in => {'$gt' => Time.now.utc})
51
+ doc = @backend.find_one( :_id => key, '$or' => [ { :expires_at => { '$gt' => Time.now.to_i } }, { :expires_at => -1 } ] )
52
52
  return nil if doc.nil?
53
- parser.decode(doc['value'].to_s)
53
+ expiry = doc['expires_at']
54
+ if now_before? expiry
55
+ parser.decode(doc['value'].to_s)
56
+ else
57
+ delete(key)
58
+ nil
59
+ end
54
60
  end
55
61
 
56
62
  ##
@@ -70,15 +76,9 @@ module Padrino
70
76
  def set(key, value, opts = nil)
71
77
  key = key.to_s
72
78
  value = BSON::Binary.new(parser.encode(value)) if value
73
- if opts && opts[:expires_in]
74
- expires_in = opts[:expires_in].to_i
75
- expires_in = Time.now.utc + expires_in if expires_in < EXPIRES_EDGE
76
- else
77
- expires_in = Time.now.utc + EXPIRES_EDGE
78
- end
79
79
  @backend.update(
80
80
  {:_id => key},
81
- {:_id => key, :value => value, :expires_in => expires_in },
81
+ {:_id => key, :value => value, :expires_at => get_expiry(opts) },
82
82
  {:upsert => true}
83
83
  )
84
84
  end
@@ -57,10 +57,9 @@ module Padrino
57
57
  # @api public
58
58
  def set(key, value, opts = nil)
59
59
  value = parser.encode(value)
60
- if opts && opts[:expires_in]
61
- expires_in = opts[:expires_in].to_i
62
- expires_in = expires_in if expires_in < EXPIRES_EDGE
63
- @backend.setex(key, expires_in, value)
60
+ if opts && opts[:expires_in] && opts[:expires_in] >= 0
61
+ @backend.set(key, value)
62
+ @backend.expireat(key, get_expiry(opts))
64
63
  else
65
64
  @backend.set(key, value)
66
65
  end
data/test/helper.rb CHANGED
@@ -22,3 +22,23 @@ class MiniTest::Spec
22
22
 
23
23
  end
24
24
  end
25
+
26
+ MiniTest::Spec.class_eval do
27
+ def self.shared_examples
28
+ @shared_examples ||= {}
29
+ end
30
+ end
31
+
32
+ module MiniTest::Spec::SharedExamples
33
+ def shared_examples_for(desc, &block)
34
+ MiniTest::Spec.shared_examples[desc] = block
35
+ end
36
+
37
+ def it_behaves_like(desc)
38
+ self.instance_eval do
39
+ MiniTest::Spec.shared_examples[desc].call
40
+ end
41
+ end
42
+ end
43
+
44
+ Object.class_eval { include(MiniTest::Spec::SharedExamples) }
@@ -0,0 +1,58 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/spec'
3
+
4
+ class BaseTest < MiniTest::Spec
5
+ def load_store; true; end
6
+
7
+ def should_skip?
8
+ @skip ||= load_store
9
+ end
10
+ end
11
+
12
+ shared_examples_for :cacheable do
13
+ describe 'Cache' do
14
+
15
+ class Foo
16
+ def bar; "bar"; end
17
+ end
18
+
19
+ it "return nil trying to get a value that doesn't exist" do
20
+ Padrino.cache.flush
21
+ assert_equal nil, Padrino.cache.get(@test_key)
22
+ end
23
+
24
+ it 'set and get an object w/marshal' do
25
+ Padrino.cache.parser = :marshal
26
+ Padrino.cache.set(@test_key, Foo.new)
27
+ assert_equal "bar", Padrino.cache.get(@test_key).bar
28
+ end
29
+
30
+ it 'set and get a nil value' do
31
+ Padrino.cache.set(@test_key, nil)
32
+ assert_equal '', Padrino.cache.get(@test_key).to_s
33
+ end
34
+
35
+ it 'set and get a raw value' do
36
+ Padrino.cache.set(@test_key, 'foo')
37
+ assert_equal 'foo', Padrino.cache.get(@test_key)
38
+ end
39
+
40
+ it "set a value that expires" do
41
+ init_time = ( Time.now - 20 )
42
+ Time.stub(:now, init_time) { Padrino.cache.set(@test_key, 'test', :expires_in => 1) }
43
+ Time.stub(:now, init_time + 20) { assert_equal nil, Padrino.cache.get(@test_key) }
44
+ end
45
+
46
+ it "be able to cache forever" do
47
+ Padrino.cache.set('forever', 'cached', :expires_in => -1)
48
+ 2.times { |i| assert_equal 'cached', Padrino.cache.get('forever') }
49
+ end
50
+
51
+ it 'delete a value' do
52
+ Padrino.cache.set(@test_key, 'test')
53
+ assert_equal 'test', Padrino.cache.get(@test_key)
54
+ Padrino.cache.delete(@test_key)
55
+ assert_equal nil, Padrino.cache.get(@test_key)
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,28 @@
1
+ require File.expand_path('../../helper', __FILE__)
2
+ require File.expand_path('../shared', __FILE__)
3
+
4
+ class DalliTest < BaseTest
5
+ def load_store
6
+ require 'dalli'
7
+ connection = ::Dalli::Client.new('127.0.0.1:11211', :exception_retry_limit => 1)
8
+ connection.set('ping','alive')
9
+ Padrino::Cache::Store::Memcache.new(connection)
10
+ false
11
+ rescue LoadError
12
+ true
13
+ end
14
+
15
+ before do
16
+ skip if should_skip?
17
+ connection = ::Dalli::Client.new('127.0.0.1:11211', :exception_retry_limit => 1)
18
+ Padrino.cache = Padrino::Cache::Store::Memcache.new(connection)
19
+ Padrino.cache.flush
20
+ @test_key = "val_#{Time.now.to_i}"
21
+ end
22
+
23
+ after do
24
+ Padrino.cache.flush
25
+ end
26
+
27
+ it_behaves_like :cacheable
28
+ end
@@ -0,0 +1,18 @@
1
+ require File.expand_path('../../helper', __FILE__)
2
+ require File.expand_path('../shared', __FILE__)
3
+
4
+ class FileStoreTest < BaseTest
5
+ before do
6
+ @apptmp = "#{Dir.tmpdir}/padrino-tests/#{UUID.new.generate}"
7
+ FileUtils.mkdir_p(@apptmp)
8
+ Padrino.cache = Padrino::Cache::Store::File.new(@apptmp)
9
+ @test_key = "val_#{Time.now.to_i}"
10
+ Padrino.cache.flush
11
+ end
12
+
13
+ after do
14
+ Padrino.cache.flush
15
+ end
16
+
17
+ it_behaves_like :cacheable
18
+ end
@@ -0,0 +1,22 @@
1
+ require File.expand_path('../../helper', __FILE__)
2
+ require File.expand_path('../shared', __FILE__)
3
+
4
+ class MemcachedTest < BaseTest
5
+ def load_store
6
+ require 'memcached'
7
+ connection = ::Memcached.new('127.0.0.1:11211', :exception_retry_limit => 1)
8
+ Padrino::Cache::Store::Memcache.new(connection).set('ping','alive')
9
+ @test_key = "val_#{Time.now.to_i}"
10
+ false
11
+ rescue LoadError
12
+ true
13
+ rescue Memcached::SystemError
14
+ true
15
+ end
16
+
17
+ before do
18
+ Padrino.cache.flush
19
+ end
20
+
21
+ it_behaves_like :cacheable
22
+ end
@@ -0,0 +1,19 @@
1
+ require File.expand_path('../../helper', __FILE__)
2
+ require File.expand_path('../shared', __FILE__)
3
+
4
+ class InMemoryStoreTest < BaseTest
5
+ before :each do
6
+ Padrino.cache = Padrino::Cache::Store::Memory.new(50)
7
+ @test_key = "val_#{Time.now.to_i}"
8
+ Padrino.cache.flush
9
+ end
10
+ after(:each) { Padrino.cache.flush }
11
+
12
+ it_behaves_like :cacheable
13
+
14
+ it "only store 50 entries" do
15
+ 51.times { |i| Padrino.cache.set(i.to_s, i.to_s) }
16
+ assert_equal nil, Padrino.cache.get('0')
17
+ assert_equal '1', Padrino.cache.get('1')
18
+ end
19
+ end
@@ -0,0 +1,32 @@
1
+ require File.expand_path('../../helper', __FILE__)
2
+ require File.expand_path('../shared', __FILE__)
3
+
4
+ class MongoTest < BaseTest
5
+ def load_store
6
+ require 'mongo'
7
+ if defined?(RUBY_ENGINE) && RUBY_ENGINE == 'rbx'
8
+ fail NotImplementedError, "Skipping mongo test for rbx"
9
+ end
10
+ connection = ::Mongo::Connection.new('127.0.0.1', 27017).db('padrino-cache_test')
11
+ Padrino::Cache::Store::Mongo.new(connection)
12
+ false
13
+ rescue LoadError, Mongo::ConnectionFailure, NotImplementedError => e
14
+ true
15
+ end
16
+
17
+ before do
18
+ skip if should_skip?
19
+ connection = ::Mongo::Connection.new('127.0.0.1', 27017).db('padrino-cache_test')
20
+ config = {:size => 10, :collection => 'cache'}
21
+ Padrino.cache = Padrino::Cache::Store::Mongo.new(connection, config)
22
+ Padrino.cache.flush
23
+ @test_key = "val_#{Time.now.to_i}"
24
+ end
25
+
26
+ after do
27
+ Padrino.cache.flush
28
+ end
29
+
30
+ it_behaves_like :cacheable
31
+
32
+ end
@@ -0,0 +1,30 @@
1
+ require File.expand_path('../../helper', __FILE__)
2
+ require File.expand_path('../shared', __FILE__)
3
+
4
+ class RedisTest < BaseTest
5
+
6
+ def load_store
7
+ require 'redis'
8
+ store_config = {:host => '127.0.0.1',
9
+ :port => 6379,
10
+ :db => 0}
11
+ Padrino::Cache::Store::Redis.new(::Redis.new(store_config).set('ping','alive'))
12
+ false
13
+ rescue LoadError, Redis::CannotConnectError
14
+ true
15
+ end
16
+
17
+ before do
18
+ skip if should_skip?
19
+ require 'redis' unless defined?(Redis)
20
+ Padrino.cache = Padrino::Cache::Store::Redis.new(::Redis.new(:host => '127.0.0.1', :port => 6379, :db => 0))
21
+ Padrino.cache.flush
22
+ @test_key = "val_#{Time.now.to_i}"
23
+ end
24
+
25
+ after do
26
+ Padrino.cache.flush
27
+ end
28
+
29
+ it_behaves_like :cacheable
30
+ end
@@ -270,4 +270,40 @@ describe "PadrinoCache" do
270
270
  assert_equal 'foo?yes', body
271
271
  assert_equal 2, call_count
272
272
  end
273
+
274
+ should 'resolve block cache keys' do
275
+ call_count = 0
276
+ mock_app do
277
+ register Padrino::Cache
278
+ enable :caching
279
+
280
+ get '/foo', :cache => true do
281
+ cache_key { "key #{params[:id]}" }
282
+ call_count += 1
283
+ params[:id]
284
+ end
285
+ end
286
+
287
+ get '/foo?id=1'
288
+ get '/foo?id=2'
289
+ get '/foo?id=2'
290
+ get '/foo?id=1&something_else=42'
291
+ get '/foo?id=3&something_else=42'
292
+
293
+ assert_equal 3, call_count
294
+ end
295
+
296
+ should 'raise an error if providing both a cache_key and block' do
297
+ mock_app do
298
+ register Padrino::Cache
299
+ enable :caching
300
+
301
+ get '/foo', :cache => true do
302
+ cache_key(:some_key) { "key #{params[:id]}" }
303
+ end
304
+ end
305
+
306
+ assert_raises(RuntimeError) { get '/foo' }
307
+ end
308
+
273
309
  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.2
4
+ version: 0.11.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -12,7 +12,7 @@ authors:
12
12
  autorequire:
13
13
  bindir: bin
14
14
  cert_chain: []
15
- date: 2013-05-21 00:00:00.000000000 Z
15
+ date: 2013-07-29 00:00:00.000000000 Z
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
18
18
  name: padrino-core
@@ -21,7 +21,7 @@ dependencies:
21
21
  requirements:
22
22
  - - '='
23
23
  - !ruby/object:Gem::Version
24
- version: 0.11.2
24
+ version: 0.11.3
25
25
  type: :runtime
26
26
  prerelease: false
27
27
  version_requirements: !ruby/object:Gem::Requirement
@@ -29,7 +29,7 @@ dependencies:
29
29
  requirements:
30
30
  - - '='
31
31
  - !ruby/object:Gem::Version
32
- version: 0.11.2
32
+ version: 0.11.3
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: padrino-helpers
35
35
  requirement: !ruby/object:Gem::Requirement
@@ -37,7 +37,7 @@ dependencies:
37
37
  requirements:
38
38
  - - '='
39
39
  - !ruby/object:Gem::Version
40
- version: 0.11.2
40
+ version: 0.11.3
41
41
  type: :runtime
42
42
  prerelease: false
43
43
  version_requirements: !ruby/object:Gem::Requirement
@@ -45,7 +45,7 @@ dependencies:
45
45
  requirements:
46
46
  - - '='
47
47
  - !ruby/object:Gem::Version
48
- version: 0.11.2
48
+ version: 0.11.3
49
49
  description: Caching support for memcached, page and fragment
50
50
  email: padrinorb@gmail.com
51
51
  executables: []
@@ -73,9 +73,14 @@ files:
73
73
  - lib/padrino-cache/store/redis.rb
74
74
  - padrino-cache.gemspec
75
75
  - test/helper.rb
76
- - test/shared.rb
76
+ - test/stores/shared.rb
77
+ - test/stores/test_dalli.rb
78
+ - test/stores/test_file.rb
79
+ - test/stores/test_memcache.rb
80
+ - test/stores/test_memory.rb
81
+ - test/stores/test_mongo.rb
82
+ - test/stores/test_redis.rb
77
83
  - test/test_padrino_cache.rb
78
- - test/test_stores.rb
79
84
  homepage: http://www.padrinorb.com
80
85
  licenses: []
81
86
  post_install_message:
@@ -89,9 +94,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
89
94
  - - ! '>='
90
95
  - !ruby/object:Gem::Version
91
96
  version: '0'
92
- segments:
93
- - 0
94
- hash: 503192052381123808
95
97
  required_rubygems_version: !ruby/object:Gem::Requirement
96
98
  none: false
97
99
  requirements:
@@ -106,7 +108,12 @@ specification_version: 3
106
108
  summary: Page and fragment caching for Padrino
107
109
  test_files:
108
110
  - test/helper.rb
109
- - test/shared.rb
111
+ - test/stores/shared.rb
112
+ - test/stores/test_dalli.rb
113
+ - test/stores/test_file.rb
114
+ - test/stores/test_memcache.rb
115
+ - test/stores/test_memory.rb
116
+ - test/stores/test_mongo.rb
117
+ - test/stores/test_redis.rb
110
118
  - test/test_padrino_cache.rb
111
- - test/test_stores.rb
112
119
  has_rdoc:
data/test/shared.rb DELETED
@@ -1,37 +0,0 @@
1
- class Foo
2
- def bar; "bar"; end
3
- end
4
-
5
- should "return nil trying to get a value that doesn't exist" do
6
- assert_equal nil, Padrino.cache.get(@test_key)
7
- end
8
-
9
- should 'set and get an object w/marshal' do
10
- Padrino.cache.parser = :marshal
11
- Padrino.cache.set(@test_key, Foo.new)
12
- assert_equal "bar", Padrino.cache.get(@test_key).bar
13
- end
14
-
15
- should 'set and get a nil value' do
16
- Padrino.cache.set(@test_key, nil)
17
- assert_equal '', Padrino.cache.get(@test_key).to_s
18
- end
19
-
20
- should 'set and get a raw value' do
21
- Padrino.cache.set(@test_key, 'foo')
22
- assert_equal 'foo', Padrino.cache.get(@test_key)
23
- end
24
-
25
- should "set a value that expires" do
26
- Padrino.cache.set(@test_key, 'test', :expires_in => 1)
27
- # assert_equal 'test', Padrino.cache.get(@test_key) # Fails on race condition
28
- sleep 2
29
- assert_equal nil, Padrino.cache.get(@test_key)
30
- end
31
-
32
- should 'delete a value' do
33
- Padrino.cache.set(@test_key, 'test')
34
- assert_equal 'test', Padrino.cache.get(@test_key)
35
- Padrino.cache.delete(@test_key)
36
- assert_equal nil, Padrino.cache.get(@test_key)
37
- end
data/test/test_stores.rb DELETED
@@ -1,136 +0,0 @@
1
- require File.expand_path('../helper', __FILE__)
2
-
3
- Shared = File.read File.expand_path('../shared.rb', __FILE__)
4
-
5
- begin
6
- require 'memcached'
7
- # we're just going to assume memcached is running on the default port
8
- Padrino::Cache::Store::Memcache.new(::Memcached.new('127.0.0.1:11211', :exception_retry_limit => 1)).set('ping','alive')
9
- rescue LoadError
10
- warn "Skipping memcache with memcached library tests"
11
- rescue Memcached::SystemError
12
- warn "Skipping memcache with memcached server tests"
13
- else
14
- describe "MemcacheStore" do
15
- def setup
16
- Padrino.cache = Padrino::Cache::Store::Memcache.new(::Memcached.new('127.0.0.1:11211', :exception_retry_limit => 1))
17
- Padrino.cache.flush
18
- @test_key = "val_#{Time.now.to_i}"
19
- end
20
-
21
- def teardown
22
- Padrino.cache.flush
23
- end
24
-
25
- eval Shared
26
- end
27
- end
28
-
29
- begin
30
- require 'dalli'
31
- # we're just going to assume memcached is running on the default port
32
- Padrino::Cache::Store::Memcache.new(::Dalli::Client.new('127.0.0.1:11211', :exception_retry_limit => 1).set('ping','alive'))
33
-
34
- describe "MemcacheWithDalliStore" do
35
- def setup
36
- Padrino.cache = Padrino::Cache::Store::Memcache.new(::Dalli::Client.new('127.0.0.1:11211', :exception_retry_limit => 1))
37
- Padrino.cache.flush
38
- @test_key = "val_#{Time.now.to_i}"
39
- end
40
-
41
- def teardown
42
- Padrino.cache.flush
43
- end
44
-
45
- eval Shared
46
- end
47
- rescue LoadError
48
- warn "Skipping memcache with dalli library tests"
49
- end
50
-
51
- begin
52
- require 'redis'
53
- Padrino::Cache::Store::Redis.new(::Redis.new(:host => '127.0.0.1', :port => 6379, :db => 0).set('ping','alive'))
54
- rescue LoadError
55
- warn "Skipping redis with redis library tests"
56
- rescue Redis::CannotConnectError
57
- warn "Skipping redis with redis server tests"
58
- else
59
- describe "RedisStore" do
60
- def setup
61
- Padrino.cache = Padrino::Cache::Store::Redis.new(::Redis.new(:host => '127.0.0.1', :port => 6379, :db => 0))
62
- Padrino.cache.flush
63
- @test_key = "val_#{Time.now.to_i}"
64
- end
65
-
66
- def teardown
67
- Padrino.cache.flush
68
- end
69
-
70
- eval <<-REDIS_TEST
71
- should 'add a value to a list' do
72
- Padrino.cache.lpush(@test_key, "test")
73
- assert_equal "test", Padrino.cache.lpop(@test_key)
74
- end
75
- REDIS_TEST
76
-
77
- eval Shared
78
- end
79
- end
80
-
81
- begin
82
- require 'mongo'
83
- Padrino::Cache::Store::Mongo.new(::Mongo::Connection.new('127.0.0.1', 27017).db('padrino-cache_test'))
84
- rescue LoadError
85
- warn "Skipping Mongo tests with Mongo library tests"
86
- rescue Mongo::ConnectionFailure
87
- warn "Skipping Mongo with server tests"
88
- else
89
- describe "MongoStore" do
90
- def setup
91
- Padrino.cache = Padrino::Cache::Store::Mongo.new(::Mongo::Connection.new('127.0.0.1', 27017).db('padrino-cache_test'), {:size => 10, :collection => 'cache'})
92
- Padrino.cache.flush
93
- @test_key = "val_#{Time.now.to_i}"
94
- end
95
-
96
- def teardown
97
- Padrino.cache.flush
98
- end
99
-
100
- eval Shared
101
- end
102
- end
103
-
104
- describe "FileStore" do
105
- def setup
106
- @apptmp = "#{Dir.tmpdir}/padrino-tests/#{UUID.new.generate}"
107
- FileUtils.mkdir_p(@apptmp)
108
- Padrino.cache = Padrino::Cache::Store::File.new(@apptmp)
109
- @test_key = "val_#{Time.now.to_i}"
110
- end
111
-
112
- def teardown
113
- Padrino.cache.flush
114
- end
115
-
116
- eval Shared
117
- end
118
-
119
- describe "InMemoryStore" do
120
- def setup
121
- Padrino.cache = Padrino::Cache::Store::Memory.new(50)
122
- @test_key = "val_#{Time.now.to_i}"
123
- end
124
-
125
- def teardown
126
- Padrino.cache.flush
127
- end
128
-
129
- eval Shared
130
-
131
- should "only store 50 entries" do
132
- 51.times { |i| Padrino.cache.set(i.to_s, i.to_s) }
133
- assert_equal nil, Padrino.cache.get('0')
134
- assert_equal '1', Padrino.cache.get('1')
135
- end
136
- end