padrino-cache 0.10.0 → 0.10.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/padrino-cache.rb +3 -1
- data/lib/padrino-cache/store.rb +2 -1
- data/lib/padrino-cache/store/mongo.rb +109 -0
- data/test/test_stores.rb +21 -23
- metadata +4 -3
data/lib/padrino-cache.rb
CHANGED
@@ -26,6 +26,7 @@ module Padrino
|
|
26
26
|
# Padrino.cache = Padrino::Cache::Store::Memcache.new(::Memcached.new('127.0.0.1:11211', :exception_retry_limit => 1))
|
27
27
|
# Padrino.cache = Padrino::Cache::Store::Memcache.new(::Dalli::Client.new('127.0.0.1:11211', :exception_retry_limit => 1))
|
28
28
|
# Padrino.cache = Padrino::Cache::Store::Redis.new(::Redis.new(:host => '127.0.0.1', :port => 6379, :db => 0))
|
29
|
+
# Padrino.cache = Padrino::Cache::Store::Mongo.new(::Mongo::Connection.new('127.0.0.1', 27017).db('padrino'), :username => 'username', :password => 'password', :size => 64, :max => 100, :collection => 'cache')
|
29
30
|
# Padrino.cache = Padrino::Cache::Store::Memory.new(50)
|
30
31
|
# Padrino.cache = Padrino::Cache::Store::File.new(/my/cache/path)
|
31
32
|
#
|
@@ -69,6 +70,7 @@ module Padrino
|
|
69
70
|
# set :cache, Padrino::Cache::Store::Memcache.new(::Memcached.new('127.0.0.1:11211', :exception_retry_limit => 1))
|
70
71
|
# set :cache, Padrino::Cache::Store::Memcache.new(::Dalli::Client.new('127.0.0.1:11211', :exception_retry_limit => 1))
|
71
72
|
# set :cache, Padrino::Cache::Store::Redis.new(::Redis.new(:host => '127.0.0.1', :port => 6379, :db => 0))
|
73
|
+
# set :cache, Padrino::Cache::Store::Mongo.new(::Mongo::Connection.new('127.0.0.1', 27017).db('padrino'), :username => 'username', :password => 'password', :size => 64, :max => 100, :collection => 'cache')
|
72
74
|
# set :cache, Padrino::Cache::Store::Memory.new(50)
|
73
75
|
# set :cache, Padrino::Cache::Store::File.new(Padrino.root('tmp', app_name.to_s, 'cache')) # default choice
|
74
76
|
#
|
@@ -93,4 +95,4 @@ module Padrino
|
|
93
95
|
end
|
94
96
|
end
|
95
97
|
end # Cache
|
96
|
-
end # Padrino
|
98
|
+
end # Padrino
|
data/lib/padrino-cache/store.rb
CHANGED
@@ -7,6 +7,7 @@ module Padrino
|
|
7
7
|
autoload :Memcache, 'padrino-cache/store/memcache'
|
8
8
|
autoload :Memory, 'padrino-cache/store/memory'
|
9
9
|
autoload :Redis, 'padrino-cache/store/redis'
|
10
|
+
autoload :Mongo, 'padrino-cache/store/mongo'
|
10
11
|
end # Store
|
11
12
|
end # Cache
|
12
|
-
end # Padrino
|
13
|
+
end # Padrino
|
@@ -0,0 +1,109 @@
|
|
1
|
+
module Padrino
|
2
|
+
module Cache
|
3
|
+
module Store
|
4
|
+
##
|
5
|
+
# MongoDB Cache Store
|
6
|
+
#
|
7
|
+
class Mongo
|
8
|
+
##
|
9
|
+
# Initialize Mongo store with client connection and optional username and password.
|
10
|
+
#
|
11
|
+
# ==== Examples
|
12
|
+
# Padrino.cache = Padrino::Cache::Store::Mongo.new(::Mongo::Connection.new('127.0.0.1', 27017).db('padrino'), :username => 'username', :password => 'password', :size => 64, :max => 100, :collection => 'cache')
|
13
|
+
# # or from your app
|
14
|
+
# set :cache, Padrino::Cache::Store::Mongo.new(::Mongo::Connection.new('127.0.0.1', 27017).db('padrino'), :username => 'username', :password => 'password', :size => 64, :max => 100, :collection => 'cache')
|
15
|
+
#
|
16
|
+
def initialize(client, opts={})
|
17
|
+
@client = client
|
18
|
+
@options = {
|
19
|
+
:capped => true,
|
20
|
+
:collection => 'cache',
|
21
|
+
:size => 64,
|
22
|
+
:max => 100
|
23
|
+
}.merge(opts)
|
24
|
+
|
25
|
+
if @options[:username] && @options[:password]
|
26
|
+
@client.authenticate(@options[:username], @options[:password], true)
|
27
|
+
end
|
28
|
+
@backend = get_collection
|
29
|
+
end
|
30
|
+
|
31
|
+
##
|
32
|
+
# Return the a value for the given key
|
33
|
+
#
|
34
|
+
# ==== Examples
|
35
|
+
# # with MyApp.cache.set('records', records)
|
36
|
+
# MyApp.cache.get('records')
|
37
|
+
#
|
38
|
+
def get(key)
|
39
|
+
doc = @backend.find_one(:_id => key, :expires_in => {'$gt' => Time.now.utc})
|
40
|
+
return nil if doc.nil?
|
41
|
+
Marshal.load(doc['value'].to_s) if doc['value'].present?
|
42
|
+
end
|
43
|
+
|
44
|
+
##
|
45
|
+
# Set or update the value for a given key and optionally with an expire time
|
46
|
+
# Default expiry is Time.now + 86400s.
|
47
|
+
#
|
48
|
+
# ==== Examples
|
49
|
+
# MyApp.cache.set('records', records)
|
50
|
+
# MyApp.cache.set('records', records, :expires_in => 30) # => 30 seconds
|
51
|
+
#
|
52
|
+
def set(key, value, opts = nil)
|
53
|
+
key = key.to_s
|
54
|
+
value = BSON::Binary.new(Marshal.dump(value)) if value
|
55
|
+
if opts && opts[:expires_in]
|
56
|
+
expires_in = opts[:expires_in].to_i
|
57
|
+
expires_in = Time.now.utc + expires_in if expires_in < EXPIRES_EDGE
|
58
|
+
else
|
59
|
+
expires_in = Time.now.utc + EXPIRES_EDGE
|
60
|
+
end
|
61
|
+
@backend.update(
|
62
|
+
{:_id => key},
|
63
|
+
{:_id => key, :value => value, :expires_in => expires_in },
|
64
|
+
{:upsert => true})
|
65
|
+
end
|
66
|
+
|
67
|
+
##
|
68
|
+
# Delete the value for a given key
|
69
|
+
#
|
70
|
+
# ==== Examples
|
71
|
+
# # with: MyApp.cache.set('records', records)
|
72
|
+
# MyApp.cache.delete('records')
|
73
|
+
#
|
74
|
+
def delete(key)
|
75
|
+
if not @options[:capped]
|
76
|
+
@backend.remove({:_id => key})
|
77
|
+
else
|
78
|
+
# Mongo will overwrite it with a simple object {_id: new ObjectId()}
|
79
|
+
@backend.update({:_id => key},{},{:upsert => true})
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
##
|
84
|
+
# Reinitialize your cache
|
85
|
+
#
|
86
|
+
# ==== Examples
|
87
|
+
# # with: MyApp.cache.set('records', records)
|
88
|
+
# MyApp.cache.flush
|
89
|
+
# MyApp.cache.get('records') # => nil
|
90
|
+
#
|
91
|
+
def flush
|
92
|
+
@backend.drop
|
93
|
+
@backend = get_collection
|
94
|
+
end
|
95
|
+
|
96
|
+
private
|
97
|
+
def get_collection
|
98
|
+
if @client.collection_names.include?(@options[:collection]) or !@options[:capped]
|
99
|
+
@client.collection @options[:collection]
|
100
|
+
else
|
101
|
+
@client.create_collection(@options[:collection], { :capped => @options[:capped],
|
102
|
+
:size => @options[:size]*1024**2,
|
103
|
+
:max => @options[:max] })
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end # Mongo
|
107
|
+
end # Store
|
108
|
+
end # Cache
|
109
|
+
end # Padrino
|
data/test/test_stores.rb
CHANGED
@@ -10,7 +10,7 @@ should 'set and get an object' do
|
|
10
10
|
assert_equal "bar", Padrino.cache.get('val').bar
|
11
11
|
end
|
12
12
|
|
13
|
-
should 'set
|
13
|
+
should 'set and get a nil value' do
|
14
14
|
Padrino.cache.set('val', nil)
|
15
15
|
assert_equal nil, Padrino.cache.get('val')
|
16
16
|
end
|
@@ -39,27 +39,6 @@ should 'delete a value' do
|
|
39
39
|
end
|
40
40
|
HERE_DOC
|
41
41
|
|
42
|
-
begin
|
43
|
-
require 'memcached'
|
44
|
-
# we're just going to assume memcached is running on the default port
|
45
|
-
Padrino::Cache::Store::Memcache.new(::Memcached.new('127.0.0.1:11211', :exception_retry_limit => 1)).set('ping','alive')
|
46
|
-
|
47
|
-
class TestMemcacheStore < Test::Unit::TestCase
|
48
|
-
def setup
|
49
|
-
Padrino.cache = Padrino::Cache::Store::Memcache.new(::Memcached.new('127.0.0.1:11211', :exception_retry_limit => 1))
|
50
|
-
Padrino.cache.flush
|
51
|
-
end
|
52
|
-
|
53
|
-
def teardown
|
54
|
-
Padrino.cache.flush
|
55
|
-
end
|
56
|
-
|
57
|
-
eval COMMON_TESTS
|
58
|
-
end
|
59
|
-
rescue LoadError
|
60
|
-
warn "Skipping memcached with memcached library tests"
|
61
|
-
end
|
62
|
-
|
63
42
|
begin
|
64
43
|
require 'memcache'
|
65
44
|
# we're just going to assume memcached is running on the default port
|
@@ -121,6 +100,25 @@ rescue LoadError
|
|
121
100
|
warn "Skipping redis tests"
|
122
101
|
end
|
123
102
|
|
103
|
+
begin
|
104
|
+
require 'mongo'
|
105
|
+
Padrino::Cache::Store::Mongo.new(::Mongo::Connection.new('127.0.0.1', 27017).db('padrino-cache_test'))
|
106
|
+
class TestMongoStore < Test::Unit::TestCase
|
107
|
+
def setup
|
108
|
+
Padrino.cache = Padrino::Cache::Store::Mongo.new(::Mongo::Connection.new('127.0.0.1', 27017).db('padrino-cache_test'), {:size => 10, :collection => 'cache'})
|
109
|
+
Padrino.cache.flush
|
110
|
+
end
|
111
|
+
|
112
|
+
def teardown
|
113
|
+
Padrino.cache.flush
|
114
|
+
end
|
115
|
+
|
116
|
+
eval COMMON_TESTS
|
117
|
+
end
|
118
|
+
rescue LoadError
|
119
|
+
warn "Skipping Mongo tests"
|
120
|
+
end
|
121
|
+
|
124
122
|
class TestFileStore < Test::Unit::TestCase
|
125
123
|
def setup
|
126
124
|
@apptmp = "#{Dir.tmpdir}/padrino-tests/#{UUID.new.generate}"
|
@@ -151,4 +149,4 @@ class TestInMemoryStore < Test::Unit::TestCase
|
|
151
149
|
assert_equal nil, Padrino.cache.get('0')
|
152
150
|
assert_equal '1', Padrino.cache.get('1')
|
153
151
|
end
|
154
|
-
end
|
152
|
+
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: padrino-cache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.10.
|
5
|
+
version: 0.10.1
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Padrino Team
|
@@ -13,7 +13,7 @@ autorequire:
|
|
13
13
|
bindir: bin
|
14
14
|
cert_chain: []
|
15
15
|
|
16
|
-
date: 2011-
|
16
|
+
date: 2011-08-01 00:00:00 -07:00
|
17
17
|
default_executable:
|
18
18
|
dependencies:
|
19
19
|
- !ruby/object:Gem::Dependency
|
@@ -24,7 +24,7 @@ dependencies:
|
|
24
24
|
requirements:
|
25
25
|
- - "="
|
26
26
|
- !ruby/object:Gem::Version
|
27
|
-
version: 0.10.
|
27
|
+
version: 0.10.1
|
28
28
|
type: :runtime
|
29
29
|
version_requirements: *id001
|
30
30
|
description: Caching support for memcached, page and fragment
|
@@ -49,6 +49,7 @@ files:
|
|
49
49
|
- lib/padrino-cache/store/file.rb
|
50
50
|
- lib/padrino-cache/store/memcache.rb
|
51
51
|
- lib/padrino-cache/store/memory.rb
|
52
|
+
- lib/padrino-cache/store/mongo.rb
|
52
53
|
- lib/padrino-cache/store/redis.rb
|
53
54
|
- padrino-cache.gemspec
|
54
55
|
- test/helper.rb
|