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,36 +0,0 @@
1
- module Padrino
2
- module Cache
3
- ##
4
- # Defines a padrino parser for our cache store.
5
- #
6
- module Parser
7
- ##
8
- # With Parser::Plain we will store
9
- # text and object in a text format.
10
- #
11
- module Plain
12
- def self.decode(code)
13
- code.to_s
14
- end
15
-
16
- def self.encode(code)
17
- code.to_s
18
- end
19
- end
20
-
21
- ##
22
- # With Parser::Marshal we will store
23
- # text and object in a marshaled format.
24
- #
25
- module Marshal
26
- def self.decode(code)
27
- ::Marshal.load(code.to_s)
28
- end
29
-
30
- def self.encode(code)
31
- ::Marshal.dump(code)
32
- end
33
- end
34
- end
35
- end
36
- end
@@ -1,18 +0,0 @@
1
- module Padrino
2
- module Cache
3
- ##
4
- # Defines the available storage adapters for persisting the cache.
5
- #
6
- module Store
7
- # The defined duration for the expiration edge.
8
- EXPIRES_EDGE = 86400
9
-
10
- autoload :Base, 'padrino-cache/store/base'
11
- autoload :File, 'padrino-cache/store/file'
12
- autoload :Memcache, 'padrino-cache/store/memcache'
13
- autoload :Memory, 'padrino-cache/store/memory'
14
- autoload :Redis, 'padrino-cache/store/redis'
15
- autoload :Mongo, 'padrino-cache/store/mongo'
16
- end
17
- end
18
- end
@@ -1,76 +0,0 @@
1
- module Padrino
2
- module Cache
3
- module Store
4
- ##
5
- # Abstract Cache Store
6
- #
7
- class Base
8
- ##
9
- # Get the cache parser strategy.
10
- #
11
- # By default is plain, otherwise you can set **Marshal** or write your own.
12
- #
13
- def parser
14
- @_parser
15
- end
16
-
17
- ##
18
- # Set the caching parser strategy.
19
- #
20
- # @param value
21
- # Module of Padrino::Cache::Parser or any that respond to encode/decode.
22
- #
23
- # @example
24
- # # shorter version:
25
- # Padrino.cache.parser = :plain
26
- # Padrino.cache.parser = :marshal
27
- # # longer version:
28
- # Padrino.cache.parser = Padrino::Cache::Parser::Plain
29
- # Padrino.cache.parser = Padrino::Cache::Parser::Marshal
30
- #
31
- # You can easily write your own:
32
- #
33
- # @example
34
- # require 'oj'
35
- # module FastJSONParser
36
- # def self.encode(value)
37
- # Oj.dump(value)
38
- # end
39
- #
40
- # def self.decode(value)
41
- # Oj.load(value)
42
- # end
43
- # end
44
- #
45
- # Padrino.cache_parser = FastJSONParser
46
- #
47
- def parser=(mod)
48
- mod = Padrino::Cache::Parser.const_get(mod.to_s.camelize) unless mod.is_a?(Module)
49
- raise "#{mod} should respond to encode" unless mod.respond_to?(:encode)
50
- raise "#{mod} should respond to decode" unless mod.respond_to?(:decode)
51
- @_parser=mod
52
- end
53
-
54
- def initialize(options={})
55
- @never = -1
56
- self.parser = options[:parser] || :plain
57
- end
58
-
59
- private
60
- def get_expiry( opts )
61
- if opts && opts[:expires_in] && opts[:expires_in] != -1
62
- expires_in = opts[:expires_in].to_i
63
- expires_in = EXPIRES_EDGE if expires_in > EXPIRES_EDGE
64
- Time.now.to_i + expires_in
65
- else
66
- @never
67
- end
68
- end
69
-
70
- def now_before?( expiry )
71
- expiry.to_i == @never || expiry.to_i > Time.now.to_i
72
- end
73
- end
74
- end
75
- end
76
- end
@@ -1,112 +0,0 @@
1
- module Padrino
2
- module Cache
3
- module Store
4
- ##
5
- # File based Cache Store.
6
- #
7
- class File < Base
8
- ##
9
- # Initialize File store with File root.
10
- #
11
- # @param [String] root
12
- # path to cache file.
13
- #
14
- # @example
15
- # Padrino.cache = Padrino::Cache::Store::File.new("path/to")
16
- # # Or from your app
17
- # set :cache, Padrino::Cache::Store::File.new("path/to")
18
- # # You can provide a marshal parser (to store ruby objects)
19
- # set :cache, Padrino::Cache::Store::File.new("path/to", :parser => :marshal)
20
- #
21
- def initialize(root, options={})
22
- @root = root
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
- init
38
- if ::File.exist?(path_for_key(key))
39
- read_method = ::File.respond_to?(:binread) ? :binread : :read
40
- contents = ::File.send(read_method, path_for_key(key))
41
- expiry, body = contents.split("\n", 2)
42
- if now_before? expiry
43
- parser.decode(body) if body
44
- else # expire the key
45
- delete(key)
46
- nil
47
- end
48
- else # key can't be found
49
- nil
50
- end
51
- end
52
-
53
- ##
54
- # Set the value for a given key and optionally with an expire time.
55
- # Default expiry time is 86400.
56
- #
57
- # @param [String] key
58
- # cache key
59
- # @param value
60
- # value of cache key
61
- #
62
- # @example
63
- # MyApp.cache.set('records', records)
64
- # MyApp.cache.set('records', records, :expires_in => 30) # => 30 seconds
65
- #
66
- def set(key, value, opts = nil)
67
- init
68
- value = parser.encode(value) if value
69
- ::File.open(path_for_key(key), 'wb') { |f| f << get_expiry(opts).to_s << "\n" << value } if value
70
- end
71
-
72
- ##
73
- # Delete the value for a given key.
74
- #
75
- # @param [String] key
76
- # cache key
77
- # @param value
78
- # value of cache key
79
- #
80
- # @example
81
- # # with: MyApp.cache.set('records', records)
82
- # MyApp.cache.delete('records')
83
- #
84
- def delete(key)
85
- init
86
- Array(key).each { |k| FileUtils.rm_rf(path_for_key(k)) }
87
- end
88
-
89
- ##
90
- # Reinitialize your cache.
91
- #
92
- # @example
93
- # # with: MyApp.cache.set('records', records)
94
- # MyApp.cache.flush
95
- # MyApp.cache.get('records') # => nil
96
- #
97
- def flush
98
- FileUtils.rm_rf(@root)
99
- end
100
-
101
- private
102
- def path_for_key(key)
103
- ::File.join(@root, Rack::Utils.escape(key.to_s))
104
- end
105
-
106
- def init
107
- FileUtils.mkdir_p(@root) unless ::File.exist?(@root)
108
- end
109
- end
110
- end
111
- end
112
- end
@@ -1,85 +0,0 @@
1
- module Padrino
2
- module Cache
3
- module Store
4
- ##
5
- # Memcache Cache Store
6
- #
7
- class Memcache < Base
8
- ##
9
- # Initialize Memcache store with client connection.
10
- #
11
- # @param client
12
- # instance of Memcache library
13
- #
14
- # @example
15
- # Padrino.cache = Padrino::Cache::Store::Memcache.new(::Memcached.new('127.0.0.1:11211'))
16
- # Padrino.cache = Padrino::Cache::Store::Memcache.new(::Memcached.new('127.0.0.1:11211', :exception_retry_limit => 1))
17
- # # or from your app
18
- # set :cache, Padrino::Cache::Store::Memcache.new(::Memcached.new('127.0.0.1:11211'))
19
- # set :cache, Padrino::Cache::Store::Memcache.new(::Memcached.new('127.0.0.1:11211', :exception_retry_limit => 1))
20
- #
21
- def initialize(client, options={})
22
- @backend = client
23
- super(options)
24
- @never = 0 # never TTL in Memcache is 0
25
- end
26
-
27
- ##
28
- # Return the value for the given key.
29
- #
30
- # @param [String] key
31
- # cache key to retrieve value
32
- #
33
- # @example
34
- # MyApp.cache.get('records')
35
- #
36
- def get(key)
37
- @backend.get(key)
38
- rescue Memcached::NotFound
39
- nil
40
- end
41
-
42
- ##
43
- # Set the value for a given key and optionally with an expire time.
44
- # Default expiry time 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
- @backend.set(key, value, get_expiry(opts))
57
- end
58
-
59
- ##
60
- # Delete the value for a given key.
61
- #
62
- # @param [String] key
63
- # cache key
64
- #
65
- # @example
66
- # MyApp.cache.delete('records')
67
- #
68
- def delete(key)
69
- @backend.delete(key)
70
- end
71
-
72
- ##
73
- # Reinitialize your cache.
74
- #
75
- # @example
76
- # MyApp.cache.flush
77
- # MyApp.cache.get('records') # => nil
78
- #
79
- def flush
80
- @backend.respond_to?(:flush_all) ? @backend.flush_all : @backend.flush
81
- end
82
- end
83
- end
84
- end
85
- end
@@ -1,95 +0,0 @@
1
- module Padrino
2
- module Cache
3
- module Store
4
- ##
5
- # Memory Cache Store
6
- #
7
- class Memory < Base
8
- ##
9
- # Initialize Memory Store with memory size.
10
- #
11
- # @param [Integer] size
12
- # Size of memory cache.
13
- #
14
- # @example
15
- # Padrino.cache = Padrino::Cache::Store::Memory.new(10000)
16
- # # or from your app
17
- # set :cache, Padrino::Cache::Store::Memory.new(10000)
18
- #
19
- def initialize(size = 5000, options={})
20
- @size, @entries, @index = size, [], {}
21
- super(options)
22
- end
23
-
24
- ##
25
- # Return the value for the given key.
26
- #
27
- # @param [String] key
28
- # cache key to retrieve value
29
- #
30
- # @example
31
- # MyApp.cache.get('records')
32
- #
33
- def get(key)
34
- if @index.key?(key) and value = @index[key]
35
- expiry, body = value
36
- if now_before? expiry
37
- body
38
- else
39
- delete(key)
40
- nil
41
- end
42
- else
43
- nil
44
- end
45
- end
46
-
47
- ##
48
- # Set the value for a given key and optionally with an expire time.
49
- # Default expiry is 86400.
50
- #
51
- # @param [String] key
52
- # cache key
53
- # @param value
54
- # value of cache key
55
- #
56
- # @example
57
- # MyApp.cache.set('records', records)
58
- # MyApp.cache.set('records', records, :expires_in => 30) # => 30 seconds
59
- #
60
- def set(key, value, opts = nil)
61
- delete(key)
62
- @entries.push(key)
63
- @index[key] = [get_expiry(opts), value]
64
- while @entries.size > @size
65
- delete(@entries.shift)
66
- end
67
- end
68
-
69
- ##
70
- # Delete the value for a given key.
71
- #
72
- # @param [String] key
73
- # cache key
74
- #
75
- # @example
76
- # MyApp.cache.delete('records')
77
- #
78
- def delete(key)
79
- @index.delete(key)
80
- end
81
-
82
- ##
83
- # Reinitialize your cache.
84
- #
85
- # @example
86
- # MyApp.cache.flush
87
- # MyApp.cache.get('records') # => nil
88
- #
89
- def flush
90
- @index = Hash.new
91
- end
92
- end
93
- end
94
- end
95
- end
@@ -1,127 +0,0 @@
1
- module Padrino
2
- module Cache
3
- module Store
4
- ##
5
- # MongoDB Cache Store
6
- #
7
- class Mongo < Base
8
- ##
9
- # Initialize Mongo store with client connection and optional username and password.
10
- #
11
- # @param client
12
- # Instance of Mongo connection
13
- # @param [Hash] opts
14
- # options to pass into Mongo connection
15
- #
16
- # @example
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')
18
- # # or from your app
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')
20
- # # you can provide a marshal parser (to store ruby objects)
21
- # set :cache, Padrino::Cache::Store::Mongo.new(::Mongo::Connection.new('127.0.0.1', 27017).db('padrino'), :parser => :marshal)
22
- #
23
- def initialize(client, options={})
24
- @client = client
25
- @options = {
26
- :capped => true,
27
- :collection => 'cache',
28
- :size => 64,
29
- :max => 100
30
- }.merge(options)
31
-
32
- if @options[:username] && @options[:password]
33
- @client.authenticate(@options[:username], @options[:password], true)
34
- end
35
- @backend = get_collection
36
- super(options)
37
- end
38
-
39
- ##
40
- # Return the value for the given key.
41
- #
42
- # @param [String] key
43
- # cache key
44
- #
45
- # @example
46
- # MyApp.cache.get('records')
47
- #
48
- def get(key)
49
- doc = @backend.find_one( :_id => key, '$or' => [ { :expires_at => { '$gt' => Time.now.to_i } }, { :expires_at => -1 } ] )
50
- return nil if doc.nil?
51
- expiry = doc['expires_at']
52
- if now_before? expiry
53
- parser.decode(doc['value'].to_s)
54
- else
55
- delete(key)
56
- nil
57
- end
58
- end
59
-
60
- ##
61
- # Set or update the value for a given key and optionally with an expire time.
62
- # Default expiry is Time.now + 86400s.
63
- #
64
- # @param [String] key
65
- # cache key
66
- # @param value
67
- # value of cache key
68
- #
69
- # @example
70
- # MyApp.cache.set('records', records)
71
- # MyApp.cache.set('records', records, :expires_in => 30) # => 30 seconds
72
- #
73
- def set(key, value, opts = nil)
74
- key = key.to_s
75
- value = BSON::Binary.new(parser.encode(value)) if value
76
- @backend.update(
77
- {:_id => key},
78
- {:_id => key, :value => value, :expires_at => get_expiry(opts) },
79
- {:upsert => true}
80
- )
81
- end
82
-
83
- ##
84
- # Delete the value for a given key.
85
- #
86
- # @param [String] key
87
- # cache key
88
- #
89
- # @example
90
- # MyApp.cache.delete('records')
91
- #
92
- def delete(key)
93
- if not @options[:capped]
94
- @backend.remove({:_id => key})
95
- else
96
- # Mongo will overwrite it with a simple object {_id: new ObjectId()}
97
- @backend.update({:_id => key},{},{:upsert => true})
98
- end
99
- end
100
-
101
- ##
102
- # Reinitialize your cache.
103
- #
104
- # @example
105
- # # with: MyApp.cache.set('records', records)
106
- # MyApp.cache.flush
107
- # MyApp.cache.get('records') # => nil
108
- #
109
- def flush
110
- @backend.drop
111
- @backend = get_collection
112
- end
113
-
114
- private
115
- def get_collection
116
- if @client.collection_names.include?(@options[:collection]) or !@options[:capped]
117
- @client.collection @options[:collection]
118
- else
119
- @client.create_collection(@options[:collection], { :capped => @options[:capped],
120
- :size => @options[:size]*1024**2,
121
- :max => @options[:max] })
122
- end
123
- end
124
- end
125
- end
126
- end
127
- end