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.
@@ -1,105 +0,0 @@
1
- module Padrino
2
- module Cache
3
- module Store
4
- ##
5
- # Redis Cache Store
6
- #
7
- class Redis < Base
8
- ##
9
- # Initialize Redis store with client connection.
10
- #
11
- # @param client
12
- # Instance of Redis client.
13
- #
14
- # @example
15
- # Padrino.cache = Padrino::Cache::Store::Redis.new(::Redis.new(:host => '127.0.0.1', :port => 6379, :db => 0))
16
- # # or from your app
17
- # set :cache, Padrino::Cache::Store::Redis.new(::Redis.new(:host => '127.0.0.1', :port => 6379, :db => 0))
18
- # # you can provide a marshal parser (to store ruby objects)
19
- # set :cache, Padrino::Cache::Store::Redis.new(::Redis.new(:host => '127.0.0.1', :port => 6379, :db => 0), :parser => :marshal)
20
- #
21
- def initialize(client, options={})
22
- @backend = client
23
- super(options)
24
- end
25
-
26
- ##
27
- # Return the value for the given key.
28
- #
29
- # @param [String] key
30
- # cache key
31
- #
32
- # @example
33
- # # with MyApp.cache.set('records', records)
34
- # MyApp.cache.get('records')
35
- #
36
- def get(key)
37
- code = @backend.get(key)
38
- return nil unless code
39
- parser.decode(code)
40
- end
41
-
42
- ##
43
- # Set the value for a given key and optionally with an expire time.
44
- # Default expiry is 86400.
45
- #
46
- # @param [String] key
47
- # cache key
48
- # @param value
49
- # value of cache key
50
- #
51
- # @example
52
- # MyApp.cache.set('records', records)
53
- # MyApp.cache.set('records', records, :expires_in => 30) # => 30 seconds
54
- #
55
- def set(key, value, opts = nil)
56
- value = parser.encode(value)
57
- if opts && opts[:expires_in] && opts[:expires_in] >= 0
58
- @backend.set(key, value)
59
- @backend.expireat(key, get_expiry(opts))
60
- else
61
- @backend.set(key, value)
62
- end
63
- end
64
-
65
- ##
66
- # Delete the value for a given key.
67
- #
68
- # @param [String] key
69
- # cache key
70
- #
71
- # @example
72
- # MyApp.cache.delete('records')
73
- #
74
- def delete(key)
75
- @backend.del(key)
76
- end
77
-
78
- ##
79
- # Reinitialize your cache.
80
- #
81
- # @example
82
- # MyApp.cache.flush
83
- # MyApp.cache.get('records') # => nil
84
- #
85
- def flush
86
- @backend.flushdb
87
- end
88
-
89
- ##
90
- # Redis has a ton of powerful features (see:
91
- # https://github.com/redis/redis-rb), which we can't use due
92
- # to how strict the cache library is. This method catches all method calls and
93
- # tries to pass them on the redis gem.
94
- #
95
- def method_missing(name, *args, &block)
96
- if @backend.respond_to?(name)
97
- @backend.send(name, *args, &block)
98
- else
99
- super
100
- end
101
- end
102
- end
103
- end
104
- end
105
- end
@@ -1,58 +0,0 @@
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
@@ -1,28 +0,0 @@
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
@@ -1,18 +0,0 @@
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
@@ -1,22 +0,0 @@
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
@@ -1,19 +0,0 @@
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
@@ -1,32 +0,0 @@
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
@@ -1,30 +0,0 @@
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