padrino-cache 0.10.2 → 0.10.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.document +3 -3
- data/.yardopts +1 -0
- data/{LICENSE → LICENSE.txt} +0 -0
- data/lib/padrino-cache.rb +10 -3
- data/lib/padrino-cache/helpers/cache_store.rb +2 -0
- data/lib/padrino-cache/helpers/fragment.rb +11 -3
- data/lib/padrino-cache/helpers/page.rb +42 -14
- data/lib/padrino-cache/store/file.rb +35 -11
- data/lib/padrino-cache/store/memcache.rb +24 -5
- data/lib/padrino-cache/store/memory.rb +24 -5
- data/lib/padrino-cache/store/mongo.rb +27 -5
- data/lib/padrino-cache/store/redis.rb +24 -5
- data/test/helper.rb +1 -1
- data/test/test_padrino_cache.rb +28 -3
- data/test/test_stores.rb +6 -6
- metadata +9 -8
data/.document
CHANGED
data/.yardopts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
--title 'Padrino Cache Documentation' --protected
|
data/{LICENSE → LICENSE.txt}
RENAMED
|
File without changes
|
data/lib/padrino-cache.rb
CHANGED
|
@@ -8,13 +8,14 @@ module Padrino
|
|
|
8
8
|
##
|
|
9
9
|
# Returns the caching engine
|
|
10
10
|
#
|
|
11
|
-
#
|
|
11
|
+
# @example
|
|
12
12
|
# # with: Padrino.cache = Padrino::Cache::Store::File.new(/my/cache/path)
|
|
13
13
|
# Padrino.cache.set('val', 'test')
|
|
14
14
|
# Padrino.cache.get('val') # => 'test'
|
|
15
15
|
# Padrino.cache.delete('val')
|
|
16
16
|
# Padrino.cache.flush
|
|
17
17
|
#
|
|
18
|
+
# @api public
|
|
18
19
|
def cache
|
|
19
20
|
@_cache
|
|
20
21
|
end
|
|
@@ -22,7 +23,10 @@ module Padrino
|
|
|
22
23
|
##
|
|
23
24
|
# Set the caching engine
|
|
24
25
|
#
|
|
25
|
-
#
|
|
26
|
+
# @param value
|
|
27
|
+
# Instance of Padrino::Cache::Store
|
|
28
|
+
#
|
|
29
|
+
# @example
|
|
26
30
|
# Padrino.cache = Padrino::Cache::Store::Memcache.new(::Memcached.new('127.0.0.1:11211', :exception_retry_limit => 1))
|
|
27
31
|
# Padrino.cache = Padrino::Cache::Store::Memcache.new(::Dalli::Client.new('127.0.0.1:11211', :exception_retry_limit => 1))
|
|
28
32
|
# Padrino.cache = Padrino::Cache::Store::Redis.new(::Redis.new(:host => '127.0.0.1', :port => 6379, :db => 0))
|
|
@@ -30,13 +34,14 @@ module Padrino
|
|
|
30
34
|
# Padrino.cache = Padrino::Cache::Store::Memory.new(50)
|
|
31
35
|
# Padrino.cache = Padrino::Cache::Store::File.new(/my/cache/path)
|
|
32
36
|
#
|
|
33
|
-
# You can manage your cache from anywhere in your app:
|
|
37
|
+
# # You can manage your cache from anywhere in your app:
|
|
34
38
|
#
|
|
35
39
|
# Padrino.cache.set('val', 'test')
|
|
36
40
|
# Padrino.cache.get('val') # => 'test'
|
|
37
41
|
# Padrino.cache.delete('val')
|
|
38
42
|
# Padrino.cache.flush
|
|
39
43
|
#
|
|
44
|
+
# @api public
|
|
40
45
|
def cache=(value)
|
|
41
46
|
@_cache = value
|
|
42
47
|
end
|
|
@@ -81,6 +86,7 @@ module Padrino
|
|
|
81
86
|
# MyApp.cache.delete('val')
|
|
82
87
|
# MyApp.cache.flush
|
|
83
88
|
#
|
|
89
|
+
# @api public
|
|
84
90
|
def registered(app)
|
|
85
91
|
app.helpers Padrino::Cache::Helpers::CacheStore
|
|
86
92
|
app.helpers Padrino::Cache::Helpers::Fragment
|
|
@@ -90,6 +96,7 @@ module Padrino
|
|
|
90
96
|
end
|
|
91
97
|
alias :included :registered
|
|
92
98
|
|
|
99
|
+
# @api private
|
|
93
100
|
def padrino_route_added(route, verb, path, args, options, block) # @private
|
|
94
101
|
Padrino::Cache::Helpers::Page.padrino_route_added(route, verb, path, args, options, block)
|
|
95
102
|
end
|
|
@@ -20,7 +20,14 @@ module Padrino
|
|
|
20
20
|
# This helper is used anywhere in your application you would like to associate a fragment
|
|
21
21
|
# to be cached. It can be used in within a route:
|
|
22
22
|
#
|
|
23
|
-
#
|
|
23
|
+
# @param [String] key
|
|
24
|
+
# cache key
|
|
25
|
+
# @param [Hash] opts
|
|
26
|
+
# cache options, e.g :expires_in
|
|
27
|
+
# @param [Proc]
|
|
28
|
+
# Execution result to store in the cache
|
|
29
|
+
#
|
|
30
|
+
# @example
|
|
24
31
|
# # Caching a fragment
|
|
25
32
|
# class MyTweets < Padrino::Application
|
|
26
33
|
# enable :caching # turns on caching mechanism
|
|
@@ -40,16 +47,17 @@ module Padrino
|
|
|
40
47
|
# end
|
|
41
48
|
# end
|
|
42
49
|
#
|
|
50
|
+
# @api public
|
|
43
51
|
def cache(key, opts = nil, &block)
|
|
44
52
|
if settings.caching?
|
|
45
53
|
began_at = Time.now
|
|
46
54
|
if value = settings.cache.get(key.to_s)
|
|
47
|
-
logger.debug "GET Fragment
|
|
55
|
+
logger.debug "GET Fragment", began_at, key.to_s if defined?(logger)
|
|
48
56
|
concat_content(value)
|
|
49
57
|
else
|
|
50
58
|
value = capture_html(&block)
|
|
51
59
|
settings.cache.set(key.to_s, value, opts)
|
|
52
|
-
logger.debug "SET Fragment
|
|
60
|
+
logger.debug "SET Fragment", began_at, key.to_s if defined?(logger)
|
|
53
61
|
concat_content(value)
|
|
54
62
|
end
|
|
55
63
|
end
|
|
@@ -7,7 +7,7 @@ module Padrino
|
|
|
7
7
|
# By default, cached content is persisted with a "file store"--that is, in a
|
|
8
8
|
# subdirectory of your application root.
|
|
9
9
|
#
|
|
10
|
-
#
|
|
10
|
+
# @example
|
|
11
11
|
# # Setting content expiry time
|
|
12
12
|
# class CachedApp < Padrino::Application
|
|
13
13
|
# enable :caching # turns on caching mechanism
|
|
@@ -43,41 +43,69 @@ module Padrino
|
|
|
43
43
|
# be executed; rather, its previous output will be sent to the client with a
|
|
44
44
|
# 200 OK status code.
|
|
45
45
|
#
|
|
46
|
+
# @param [Integer] time
|
|
47
|
+
# Time til expiration (seconds)
|
|
48
|
+
#
|
|
49
|
+
# @example
|
|
50
|
+
# controller '/blog', :cache => true do
|
|
51
|
+
# expires_in 15
|
|
52
|
+
#
|
|
53
|
+
# get '/entries' do
|
|
54
|
+
# # expires_in 15 => can also be defined inside a single route
|
|
55
|
+
# 'just broke up eating twinkies lol'
|
|
56
|
+
# end
|
|
57
|
+
# end
|
|
58
|
+
#
|
|
59
|
+
# @api public
|
|
46
60
|
def expires_in(time)
|
|
47
|
-
@
|
|
61
|
+
@route.cache_expires_in = time if @route
|
|
62
|
+
@_last_expires_in = time
|
|
48
63
|
end
|
|
49
64
|
|
|
50
65
|
##
|
|
51
66
|
# This helper is used within a route or route to indicate the name in the cache.
|
|
52
67
|
#
|
|
68
|
+
# @param [Symbol] name
|
|
69
|
+
# cache key
|
|
70
|
+
#
|
|
71
|
+
# @example
|
|
72
|
+
# controller '/blog', :cache => true do
|
|
73
|
+
#
|
|
74
|
+
# get '/post/:id' do
|
|
75
|
+
# cache_key :my_name
|
|
76
|
+
# @post = Post.find(params[:id])
|
|
77
|
+
# end
|
|
78
|
+
# end
|
|
79
|
+
#
|
|
80
|
+
# @api public
|
|
53
81
|
def cache_key(name)
|
|
54
|
-
@
|
|
82
|
+
@route.cache_key = name
|
|
55
83
|
end
|
|
56
84
|
|
|
85
|
+
# @api private
|
|
57
86
|
def self.padrino_route_added(route, verb, path, args, options, block) # @private
|
|
58
87
|
if route.cache and %w(GET HEAD).include?(verb)
|
|
59
|
-
route.
|
|
88
|
+
route.before_filters do
|
|
60
89
|
if settings.caching?
|
|
61
90
|
began_at = Time.now
|
|
62
|
-
value = settings.cache.get(@
|
|
63
|
-
|
|
64
|
-
logger.debug "GET Cache (%0.4fms) %s" % [Time.now-began_at, env['PATH_INFO']] if defined?(logger) && value
|
|
91
|
+
value = settings.cache.get(@route.cache_key || env['PATH_INFO'])
|
|
92
|
+
logger.debug "GET Cache", began_at, env['PATH_INFO'] if defined?(logger) && value
|
|
65
93
|
halt 200, value if value
|
|
66
94
|
end
|
|
67
|
-
|
|
68
|
-
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
route.after_filters do
|
|
69
98
|
if settings.caching?
|
|
70
99
|
began_at = Time.now
|
|
71
100
|
if @_last_expires_in
|
|
72
|
-
settings.cache.set(@
|
|
101
|
+
settings.cache.set(@route.cache_key || env['PATH_INFO'], @_response_buffer, :expires_in => @_last_expires_in)
|
|
73
102
|
@_last_expires_in = nil
|
|
74
103
|
else
|
|
75
|
-
settings.cache.set(@
|
|
104
|
+
settings.cache.set(@route.cache_key || env['PATH_INFO'], @_response_buffer)
|
|
76
105
|
end
|
|
77
|
-
|
|
78
|
-
logger.debug "SET Cache (%0.4fms) %s" % [Time.now-began_at, env['PATH_INFO']] if defined?(logger)
|
|
106
|
+
logger.debug "SET Cache", began_at, env['PATH_INFO'] if defined?(logger)
|
|
79
107
|
end
|
|
80
|
-
|
|
108
|
+
end
|
|
81
109
|
end
|
|
82
110
|
end
|
|
83
111
|
end # Page
|
|
@@ -8,11 +8,15 @@ module Padrino
|
|
|
8
8
|
##
|
|
9
9
|
# Initialize File store with File root
|
|
10
10
|
#
|
|
11
|
-
#
|
|
11
|
+
# @param [String] root
|
|
12
|
+
# path to cache file
|
|
13
|
+
#
|
|
14
|
+
# @example
|
|
12
15
|
# Padrino.cache = Padrino::Cache::Store::File.new("path/to")
|
|
13
16
|
# # or from your app
|
|
14
17
|
# set :cache, Padrino::Cache::Store::File.new("path/to")
|
|
15
18
|
#
|
|
19
|
+
# @api public
|
|
16
20
|
def initialize(root)
|
|
17
21
|
@root = root
|
|
18
22
|
end
|
|
@@ -20,10 +24,14 @@ module Padrino
|
|
|
20
24
|
##
|
|
21
25
|
# Return the a value for the given key
|
|
22
26
|
#
|
|
23
|
-
#
|
|
27
|
+
# @param [String] key
|
|
28
|
+
# cache key
|
|
29
|
+
#
|
|
30
|
+
# @example
|
|
24
31
|
# # with MyApp.cache.set('records', records)
|
|
25
32
|
# MyApp.cache.get('records')
|
|
26
33
|
#
|
|
34
|
+
# @api public
|
|
27
35
|
def get(key)
|
|
28
36
|
init
|
|
29
37
|
if ::File.exist?(path_for_key(key))
|
|
@@ -45,10 +53,16 @@ module Padrino
|
|
|
45
53
|
# Set the value for a given key and optionally with an expire time
|
|
46
54
|
# Default expiry time is 86400.
|
|
47
55
|
#
|
|
48
|
-
#
|
|
56
|
+
# @param [String] key
|
|
57
|
+
# cache key
|
|
58
|
+
# @param value
|
|
59
|
+
# value of cache key
|
|
60
|
+
#
|
|
61
|
+
# @example
|
|
49
62
|
# MyApp.cache.set('records', records)
|
|
50
63
|
# MyApp.cache.set('records', records, :expires_in => 30) # => 30 seconds
|
|
51
64
|
#
|
|
65
|
+
# @api public
|
|
52
66
|
def set(key, value, opts = nil)
|
|
53
67
|
init
|
|
54
68
|
if opts && opts[:expires_in]
|
|
@@ -64,10 +78,16 @@ module Padrino
|
|
|
64
78
|
##
|
|
65
79
|
# Delete the value for a given key
|
|
66
80
|
#
|
|
67
|
-
#
|
|
81
|
+
# @param [String] key
|
|
82
|
+
# cache key
|
|
83
|
+
# @param value
|
|
84
|
+
# value of cache key
|
|
85
|
+
#
|
|
86
|
+
# @example
|
|
68
87
|
# # with: MyApp.cache.set('records', records)
|
|
69
88
|
# MyApp.cache.delete('records')
|
|
70
89
|
#
|
|
90
|
+
# @api public
|
|
71
91
|
def delete(key)
|
|
72
92
|
init
|
|
73
93
|
Array(key).each { |k| FileUtils.rm_rf(path_for_key(k)) }
|
|
@@ -76,23 +96,27 @@ module Padrino
|
|
|
76
96
|
##
|
|
77
97
|
# Reinitialize your cache
|
|
78
98
|
#
|
|
79
|
-
#
|
|
99
|
+
# @example
|
|
80
100
|
# # with: MyApp.cache.set('records', records)
|
|
81
101
|
# MyApp.cache.flush
|
|
82
102
|
# MyApp.cache.get('records') # => nil
|
|
83
103
|
#
|
|
104
|
+
# @api public
|
|
84
105
|
def flush
|
|
85
106
|
FileUtils.rm_rf(@root)
|
|
86
107
|
end
|
|
87
108
|
|
|
88
109
|
private
|
|
89
|
-
def path_for_key(key)
|
|
90
|
-
::File.join(@root, Rack::Utils.escape(key.to_s))
|
|
91
|
-
end
|
|
92
110
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
111
|
+
# @api private
|
|
112
|
+
def path_for_key(key)
|
|
113
|
+
::File.join(@root, Rack::Utils.escape(key.to_s))
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
# @api private
|
|
117
|
+
def init
|
|
118
|
+
FileUtils.mkdir_p(@root) unless ::File.exist?(@root)
|
|
119
|
+
end
|
|
96
120
|
end # File
|
|
97
121
|
end # Store
|
|
98
122
|
end # Cache
|
|
@@ -8,13 +8,17 @@ module Padrino
|
|
|
8
8
|
##
|
|
9
9
|
# Initialize Memcache store with client connection.
|
|
10
10
|
#
|
|
11
|
-
#
|
|
11
|
+
# @param client
|
|
12
|
+
# instance of Memcache library
|
|
13
|
+
#
|
|
14
|
+
# @example
|
|
12
15
|
# Padrino.cache = Padrino::Cache::Store::Memcache.new(::Memcached.new('127.0.0.1:11211'))
|
|
13
16
|
# Padrino.cache = Padrino::Cache::Store::Memcache.new(::Memcached.new('127.0.0.1:11211', :exception_retry_limit => 1))
|
|
14
17
|
# # or from your app
|
|
15
18
|
# set :cache, Padrino::Cache::Store::Memcache.new(::Memcached.new('127.0.0.1:11211'))
|
|
16
19
|
# set :cache, Padrino::Cache::Store::Memcache.new(::Memcached.new('127.0.0.1:11211', :exception_retry_limit => 1))
|
|
17
20
|
#
|
|
21
|
+
# @api public
|
|
18
22
|
def initialize(client)
|
|
19
23
|
@backend = client
|
|
20
24
|
rescue
|
|
@@ -24,10 +28,14 @@ module Padrino
|
|
|
24
28
|
##
|
|
25
29
|
# Return the a value for the given key
|
|
26
30
|
#
|
|
27
|
-
#
|
|
31
|
+
# @param [String] key
|
|
32
|
+
# cache key to retrieve value
|
|
33
|
+
#
|
|
34
|
+
# @example
|
|
28
35
|
# # with MyApp.cache.set('records', records)
|
|
29
36
|
# MyApp.cache.get('records')
|
|
30
37
|
#
|
|
38
|
+
# @api public
|
|
31
39
|
def get(key)
|
|
32
40
|
@backend.get(key)
|
|
33
41
|
rescue Memcached::NotFound
|
|
@@ -38,10 +46,16 @@ module Padrino
|
|
|
38
46
|
# Set the value for a given key and optionally with an expire time
|
|
39
47
|
# Default expiry time is 86400.
|
|
40
48
|
#
|
|
41
|
-
#
|
|
49
|
+
# @param [String] key
|
|
50
|
+
# cache key
|
|
51
|
+
# @param value
|
|
52
|
+
# value of cache key
|
|
53
|
+
#
|
|
54
|
+
# @example
|
|
42
55
|
# MyApp.cache.set('records', records)
|
|
43
56
|
# MyApp.cache.set('records', records, :expires_in => 30) # => 30 seconds
|
|
44
57
|
#
|
|
58
|
+
# @api public
|
|
45
59
|
def set(key, value, opts = nil)
|
|
46
60
|
if opts && opts[:expires_in]
|
|
47
61
|
expires_in = opts[:expires_in].to_i
|
|
@@ -55,10 +69,14 @@ module Padrino
|
|
|
55
69
|
##
|
|
56
70
|
# Delete the value for a given key
|
|
57
71
|
#
|
|
58
|
-
#
|
|
72
|
+
# @param [String] key
|
|
73
|
+
# cache key
|
|
74
|
+
#
|
|
75
|
+
# @example
|
|
59
76
|
# # with: MyApp.cache.set('records', records)
|
|
60
77
|
# MyApp.cache.delete('records')
|
|
61
78
|
#
|
|
79
|
+
# @api public
|
|
62
80
|
def delete(key)
|
|
63
81
|
@backend.delete(key)
|
|
64
82
|
end
|
|
@@ -66,11 +84,12 @@ module Padrino
|
|
|
66
84
|
##
|
|
67
85
|
# Reinitialize your cache
|
|
68
86
|
#
|
|
69
|
-
#
|
|
87
|
+
# @example
|
|
70
88
|
# # with: MyApp.cache.set('records', records)
|
|
71
89
|
# MyApp.cache.flush
|
|
72
90
|
# MyApp.cache.get('records') # => nil
|
|
73
91
|
#
|
|
92
|
+
# @api public
|
|
74
93
|
def flush
|
|
75
94
|
@backend.respond_to?(:flush_all) ? @backend.flush_all : @backend.flush
|
|
76
95
|
end
|
|
@@ -8,11 +8,15 @@ module Padrino
|
|
|
8
8
|
##
|
|
9
9
|
# Initialize Memory Store with memory size
|
|
10
10
|
#
|
|
11
|
-
#
|
|
11
|
+
# @param [Integer] size
|
|
12
|
+
# Size of memory cache
|
|
13
|
+
#
|
|
14
|
+
# @example
|
|
12
15
|
# Padrino.cache = Padrino::Cache::Store::Memory.new(10000)
|
|
13
16
|
# # or from your app
|
|
14
17
|
# set :cache, Padrino::Cache::Store::Memory.new(10000)
|
|
15
18
|
#
|
|
19
|
+
# @api public
|
|
16
20
|
def initialize(size = 5000)
|
|
17
21
|
@size, @entries, @index = size, [], {}
|
|
18
22
|
end
|
|
@@ -20,10 +24,14 @@ module Padrino
|
|
|
20
24
|
##
|
|
21
25
|
# Return the a value for the given key
|
|
22
26
|
#
|
|
23
|
-
#
|
|
27
|
+
# @param [String] key
|
|
28
|
+
# cache key to retrieve value
|
|
29
|
+
#
|
|
30
|
+
# @example
|
|
24
31
|
# # with MyApp.cache.set('records', records)
|
|
25
32
|
# MyApp.cache.get('records')
|
|
26
33
|
#
|
|
34
|
+
# @api public
|
|
27
35
|
def get(key)
|
|
28
36
|
if @index.key?(key) and value = @index[key]
|
|
29
37
|
expires_in, body = value
|
|
@@ -43,10 +51,16 @@ module Padrino
|
|
|
43
51
|
# Set the value for a given key and optionally with an expire time.
|
|
44
52
|
# Default expiry is 86400.
|
|
45
53
|
#
|
|
46
|
-
#
|
|
54
|
+
# @param [String] key
|
|
55
|
+
# cache key
|
|
56
|
+
# @param value
|
|
57
|
+
# value of cache key
|
|
58
|
+
#
|
|
59
|
+
# @example
|
|
47
60
|
# MyApp.cache.set('records', records)
|
|
48
61
|
# MyApp.cache.set('records', records, :expires_in => 30) # => 30 seconds
|
|
49
62
|
#
|
|
63
|
+
# @api public
|
|
50
64
|
def set(key, value, opts = nil)
|
|
51
65
|
delete(key) if @index.key?(key)
|
|
52
66
|
if opts && opts[:expires_in]
|
|
@@ -66,10 +80,14 @@ module Padrino
|
|
|
66
80
|
##
|
|
67
81
|
# Delete the value for a given key
|
|
68
82
|
#
|
|
69
|
-
#
|
|
83
|
+
# @param [String] key
|
|
84
|
+
# cache key
|
|
85
|
+
#
|
|
86
|
+
# @example
|
|
70
87
|
# # with: MyApp.cache.set('records', records)
|
|
71
88
|
# MyApp.cache.delete('records')
|
|
72
89
|
#
|
|
90
|
+
# @api public
|
|
73
91
|
def delete(key)
|
|
74
92
|
@index.delete(key)
|
|
75
93
|
end
|
|
@@ -77,11 +95,12 @@ module Padrino
|
|
|
77
95
|
##
|
|
78
96
|
# Reinitialize your cache
|
|
79
97
|
#
|
|
80
|
-
#
|
|
98
|
+
# @example
|
|
81
99
|
# # with: MyApp.cache.set('records', records)
|
|
82
100
|
# MyApp.cache.flush
|
|
83
101
|
# MyApp.cache.get('records') # => nil
|
|
84
102
|
#
|
|
103
|
+
# @api public
|
|
85
104
|
def flush
|
|
86
105
|
@index = Hash.new
|
|
87
106
|
end
|
|
@@ -8,11 +8,17 @@ module Padrino
|
|
|
8
8
|
##
|
|
9
9
|
# Initialize Mongo store with client connection and optional username and password.
|
|
10
10
|
#
|
|
11
|
-
#
|
|
11
|
+
# @param client
|
|
12
|
+
# Instance of Mongo connection
|
|
13
|
+
# @param [Hash] opts
|
|
14
|
+
# optiosn to pass into Mongo connection
|
|
15
|
+
#
|
|
16
|
+
# @example
|
|
12
17
|
# 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
18
|
# # or from your app
|
|
14
19
|
# 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
20
|
#
|
|
21
|
+
# @api public
|
|
16
22
|
def initialize(client, opts={})
|
|
17
23
|
@client = client
|
|
18
24
|
@options = {
|
|
@@ -31,7 +37,10 @@ module Padrino
|
|
|
31
37
|
##
|
|
32
38
|
# Return the a value for the given key
|
|
33
39
|
#
|
|
34
|
-
#
|
|
40
|
+
# @param [String] key
|
|
41
|
+
# cache key
|
|
42
|
+
#
|
|
43
|
+
# @example
|
|
35
44
|
# # with MyApp.cache.set('records', records)
|
|
36
45
|
# MyApp.cache.get('records')
|
|
37
46
|
#
|
|
@@ -45,10 +54,16 @@ module Padrino
|
|
|
45
54
|
# Set or update the value for a given key and optionally with an expire time
|
|
46
55
|
# Default expiry is Time.now + 86400s.
|
|
47
56
|
#
|
|
48
|
-
#
|
|
57
|
+
# @param [String] key
|
|
58
|
+
# cache key
|
|
59
|
+
# @param value
|
|
60
|
+
# value of cache key
|
|
61
|
+
#
|
|
62
|
+
# @example
|
|
49
63
|
# MyApp.cache.set('records', records)
|
|
50
64
|
# MyApp.cache.set('records', records, :expires_in => 30) # => 30 seconds
|
|
51
65
|
#
|
|
66
|
+
# @api public
|
|
52
67
|
def set(key, value, opts = nil)
|
|
53
68
|
key = key.to_s
|
|
54
69
|
value = BSON::Binary.new(Marshal.dump(value)) if value
|
|
@@ -67,10 +82,14 @@ module Padrino
|
|
|
67
82
|
##
|
|
68
83
|
# Delete the value for a given key
|
|
69
84
|
#
|
|
70
|
-
#
|
|
85
|
+
# @param [String] key
|
|
86
|
+
# cache key
|
|
87
|
+
#
|
|
88
|
+
# @example
|
|
71
89
|
# # with: MyApp.cache.set('records', records)
|
|
72
90
|
# MyApp.cache.delete('records')
|
|
73
91
|
#
|
|
92
|
+
# @api public
|
|
74
93
|
def delete(key)
|
|
75
94
|
if not @options[:capped]
|
|
76
95
|
@backend.remove({:_id => key})
|
|
@@ -83,17 +102,20 @@ module Padrino
|
|
|
83
102
|
##
|
|
84
103
|
# Reinitialize your cache
|
|
85
104
|
#
|
|
86
|
-
#
|
|
105
|
+
# @example
|
|
87
106
|
# # with: MyApp.cache.set('records', records)
|
|
88
107
|
# MyApp.cache.flush
|
|
89
108
|
# MyApp.cache.get('records') # => nil
|
|
90
109
|
#
|
|
110
|
+
# @api public
|
|
91
111
|
def flush
|
|
92
112
|
@backend.drop
|
|
93
113
|
@backend = get_collection
|
|
94
114
|
end
|
|
95
115
|
|
|
96
116
|
private
|
|
117
|
+
|
|
118
|
+
# @api private
|
|
97
119
|
def get_collection
|
|
98
120
|
if @client.collection_names.include?(@options[:collection]) or !@options[:capped]
|
|
99
121
|
@client.collection @options[:collection]
|
|
@@ -8,11 +8,15 @@ module Padrino
|
|
|
8
8
|
##
|
|
9
9
|
# Initialize Redis store with client connection.
|
|
10
10
|
#
|
|
11
|
-
#
|
|
11
|
+
# @param client
|
|
12
|
+
# Instance of Redis client
|
|
13
|
+
#
|
|
14
|
+
# @example
|
|
12
15
|
# Padrino.cache = Padrino::Cache::Store::Redis.new(::Redis.new(:host => '127.0.0.1', :port => 6379, :db => 0))
|
|
13
16
|
# # or from your app
|
|
14
17
|
# set :cache, Padrino::Cache::Store::Redis.new(::Redis.new(:host => '127.0.0.1', :port => 6379, :db => 0))
|
|
15
18
|
#
|
|
19
|
+
# @api public
|
|
16
20
|
def initialize(client)
|
|
17
21
|
@backend = client
|
|
18
22
|
end
|
|
@@ -20,10 +24,14 @@ module Padrino
|
|
|
20
24
|
##
|
|
21
25
|
# Return the a value for the given key
|
|
22
26
|
#
|
|
23
|
-
#
|
|
27
|
+
# @param [String] key
|
|
28
|
+
# cache key
|
|
29
|
+
#
|
|
30
|
+
# @example
|
|
24
31
|
# # with MyApp.cache.set('records', records)
|
|
25
32
|
# MyApp.cache.get('records')
|
|
26
33
|
#
|
|
34
|
+
# @api public
|
|
27
35
|
def get(key)
|
|
28
36
|
code = @backend.get(key)
|
|
29
37
|
Marshal.load(code) if code.present?
|
|
@@ -33,10 +41,16 @@ module Padrino
|
|
|
33
41
|
# Set the value for a given key and optionally with an expire time
|
|
34
42
|
# Default expiry is 86400.
|
|
35
43
|
#
|
|
36
|
-
#
|
|
44
|
+
# @param [String] key
|
|
45
|
+
# cache key
|
|
46
|
+
# @param value
|
|
47
|
+
# value of cache key
|
|
48
|
+
#
|
|
49
|
+
# @example
|
|
37
50
|
# MyApp.cache.set('records', records)
|
|
38
51
|
# MyApp.cache.set('records', records, :expires_in => 30) # => 30 seconds
|
|
39
52
|
#
|
|
53
|
+
# @api public
|
|
40
54
|
def set(key, value, opts = nil)
|
|
41
55
|
value = Marshal.dump(value) if value
|
|
42
56
|
if opts && opts[:expires_in]
|
|
@@ -51,10 +65,14 @@ module Padrino
|
|
|
51
65
|
##
|
|
52
66
|
# Delete the value for a given key
|
|
53
67
|
#
|
|
54
|
-
#
|
|
68
|
+
# @param [String] key
|
|
69
|
+
# cache key
|
|
70
|
+
#
|
|
71
|
+
# @example
|
|
55
72
|
# # with: MyApp.cache.set('records', records)
|
|
56
73
|
# MyApp.cache.delete('records')
|
|
57
74
|
#
|
|
75
|
+
# @api public
|
|
58
76
|
def delete(key)
|
|
59
77
|
@backend.del(key)
|
|
60
78
|
end
|
|
@@ -62,11 +80,12 @@ module Padrino
|
|
|
62
80
|
##
|
|
63
81
|
# Reinitialize your cache
|
|
64
82
|
#
|
|
65
|
-
#
|
|
83
|
+
# @example
|
|
66
84
|
# # with: MyApp.cache.set('records', records)
|
|
67
85
|
# MyApp.cache.flush
|
|
68
86
|
# MyApp.cache.get('records') # => nil
|
|
69
87
|
#
|
|
88
|
+
# @api public
|
|
70
89
|
def flush
|
|
71
90
|
@backend.flushdb
|
|
72
91
|
end
|
data/test/helper.rb
CHANGED
data/test/test_padrino_cache.rb
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/helper')
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
describe "PadrinoCache" do
|
|
4
4
|
|
|
5
5
|
def teardown
|
|
6
6
|
tmp = File.expand_path(File.dirname(__FILE__) + "/tmp")
|
|
@@ -58,20 +58,45 @@ class TestPadrinoCache < Test::Unit::TestCase
|
|
|
58
58
|
end
|
|
59
59
|
|
|
60
60
|
should 'accept custom cache keys' do
|
|
61
|
+
called = false
|
|
61
62
|
mock_app do
|
|
62
63
|
register Padrino::Cache
|
|
63
64
|
enable :caching
|
|
64
|
-
get
|
|
65
|
-
|
|
65
|
+
get '/foo', :cache => true do
|
|
66
|
+
if called
|
|
67
|
+
"you'll never see me"
|
|
68
|
+
else
|
|
69
|
+
cache_key :foo
|
|
70
|
+
called = 'foo'
|
|
71
|
+
|
|
72
|
+
called
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
get '/bar', :cache => true do
|
|
77
|
+
if called
|
|
78
|
+
cache_key :bar
|
|
79
|
+
called = 'bar'
|
|
80
|
+
|
|
81
|
+
called
|
|
82
|
+
else
|
|
83
|
+
"you'll never see me"
|
|
84
|
+
end
|
|
85
|
+
end
|
|
66
86
|
end
|
|
67
87
|
get "/foo"
|
|
68
88
|
assert_equal 200, status
|
|
69
89
|
assert_equal 'foo', body
|
|
70
90
|
assert_equal 'foo', @app.cache.get(:foo)
|
|
91
|
+
get "/foo"
|
|
92
|
+
assert_equal 'foo', body
|
|
93
|
+
|
|
71
94
|
get "/bar"
|
|
72
95
|
assert_equal 200, status
|
|
73
96
|
assert_equal 'bar', body
|
|
74
97
|
assert_equal 'bar', @app.cache.get(:bar)
|
|
98
|
+
get "/bar"
|
|
99
|
+
assert_equal 'bar', body
|
|
75
100
|
end
|
|
76
101
|
|
|
77
102
|
should 'delete based on urls' do
|
data/test/test_stores.rb
CHANGED
|
@@ -44,7 +44,7 @@ begin
|
|
|
44
44
|
# we're just going to assume memcached is running on the default port
|
|
45
45
|
Padrino::Cache::Store::Memcache.new(::MemCache.new('127.0.0.1:11211', :exception_retry_limit => 1)).set('ping','alive')
|
|
46
46
|
|
|
47
|
-
|
|
47
|
+
describe "MemcacheStore" do
|
|
48
48
|
def setup
|
|
49
49
|
Padrino.cache = Padrino::Cache::Store::Memcache.new(::MemCache.new('127.0.0.1:11211', :exception_retry_limit => 1))
|
|
50
50
|
Padrino.cache.flush
|
|
@@ -65,7 +65,7 @@ begin
|
|
|
65
65
|
# we're just going to assume memcached is running on the default port
|
|
66
66
|
Padrino::Cache::Store::Memcache.new(::Dalli::Client.new('127.0.0.1:11211', :exception_retry_limit => 1).set('ping','alive'))
|
|
67
67
|
|
|
68
|
-
|
|
68
|
+
describe "MemcacheWithDalliStore" do
|
|
69
69
|
def setup
|
|
70
70
|
Padrino.cache = Padrino::Cache::Store::Memcache.new(::Dalli::Client.new('127.0.0.1:11211', :exception_retry_limit => 1))
|
|
71
71
|
Padrino.cache.flush
|
|
@@ -85,7 +85,7 @@ end
|
|
|
85
85
|
begin
|
|
86
86
|
require 'redis'
|
|
87
87
|
Padrino::Cache::Store::Redis.new(::Redis.new(:host => '127.0.0.1', :port => 6379, :db => 0).set('ping','alive'))
|
|
88
|
-
|
|
88
|
+
describe "RedisStore" do
|
|
89
89
|
def setup
|
|
90
90
|
Padrino.cache = Padrino::Cache::Store::Redis.new(::Redis.new(:host => '127.0.0.1', :port => 6379, :db => 0))
|
|
91
91
|
Padrino.cache.flush
|
|
@@ -105,7 +105,7 @@ end
|
|
|
105
105
|
begin
|
|
106
106
|
require 'mongo'
|
|
107
107
|
Padrino::Cache::Store::Mongo.new(::Mongo::Connection.new('127.0.0.1', 27017).db('padrino-cache_test'))
|
|
108
|
-
|
|
108
|
+
describe "MongoStore" do
|
|
109
109
|
def setup
|
|
110
110
|
Padrino.cache = Padrino::Cache::Store::Mongo.new(::Mongo::Connection.new('127.0.0.1', 27017).db('padrino-cache_test'), {:size => 10, :collection => 'cache'})
|
|
111
111
|
Padrino.cache.flush
|
|
@@ -122,7 +122,7 @@ rescue LoadError
|
|
|
122
122
|
warn "Skipping Mongo tests"
|
|
123
123
|
end
|
|
124
124
|
|
|
125
|
-
|
|
125
|
+
describe "FileStore" do
|
|
126
126
|
def setup
|
|
127
127
|
@apptmp = "#{Dir.tmpdir}/padrino-tests/#{UUID.new.generate}"
|
|
128
128
|
FileUtils.mkdir_p(@apptmp)
|
|
@@ -137,7 +137,7 @@ class TestFileStore < Test::Unit::TestCase
|
|
|
137
137
|
eval COMMON_TESTS
|
|
138
138
|
end
|
|
139
139
|
|
|
140
|
-
|
|
140
|
+
describe "InMemoryStore" do
|
|
141
141
|
def setup
|
|
142
142
|
Padrino.cache = Padrino::Cache::Store::Memory.new(50)
|
|
143
143
|
@test_key = "val_#{Time.now.to_i}"
|
metadata
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: padrino-cache
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
hash:
|
|
4
|
+
hash: 49
|
|
5
5
|
prerelease:
|
|
6
6
|
segments:
|
|
7
7
|
- 0
|
|
8
8
|
- 10
|
|
9
|
-
-
|
|
10
|
-
version: 0.10.
|
|
9
|
+
- 3
|
|
10
|
+
version: 0.10.3
|
|
11
11
|
platform: ruby
|
|
12
12
|
authors:
|
|
13
13
|
- Padrino Team
|
|
@@ -18,7 +18,7 @@ autorequire:
|
|
|
18
18
|
bindir: bin
|
|
19
19
|
cert_chain: []
|
|
20
20
|
|
|
21
|
-
date: 2011-
|
|
21
|
+
date: 2011-10-03 00:00:00 -07:00
|
|
22
22
|
default_executable:
|
|
23
23
|
dependencies:
|
|
24
24
|
- !ruby/object:Gem::Dependency
|
|
@@ -29,12 +29,12 @@ dependencies:
|
|
|
29
29
|
requirements:
|
|
30
30
|
- - "="
|
|
31
31
|
- !ruby/object:Gem::Version
|
|
32
|
-
hash:
|
|
32
|
+
hash: 49
|
|
33
33
|
segments:
|
|
34
34
|
- 0
|
|
35
35
|
- 10
|
|
36
|
-
-
|
|
37
|
-
version: 0.10.
|
|
36
|
+
- 3
|
|
37
|
+
version: 0.10.3
|
|
38
38
|
type: :runtime
|
|
39
39
|
version_requirements: *id001
|
|
40
40
|
description: Caching support for memcached, page and fragment
|
|
@@ -48,7 +48,8 @@ extra_rdoc_files:
|
|
|
48
48
|
files:
|
|
49
49
|
- .document
|
|
50
50
|
- .gitignore
|
|
51
|
-
-
|
|
51
|
+
- .yardopts
|
|
52
|
+
- LICENSE.txt
|
|
52
53
|
- README.rdoc
|
|
53
54
|
- Rakefile
|
|
54
55
|
- lib/padrino-cache.rb
|