elephas 2.2.0 → 3.0.0

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/doc/method_list.html CHANGED
@@ -55,39 +55,39 @@
55
55
 
56
56
 
57
57
  <li class="r2 ">
58
- <span class='object_link'><a href="Elephas/Providers/Hash.html#data-instance_method" title="Elephas::Providers::Hash#data (method)">#data</a></span>
58
+ <span class='object_link'><a href="Elephas/Cache.html#backend-instance_method" title="Elephas::Cache#backend (method)">#backend</a></span>
59
59
 
60
- <small>Elephas::Providers::Hash</small>
60
+ <small>Elephas::Cache</small>
61
61
 
62
62
  </li>
63
63
 
64
64
 
65
65
  <li class="r1 ">
66
- <span class='object_link'><a href="Elephas/Cache.html#default_prefix-class_method" title="Elephas::Cache.default_prefix (method)">default_prefix</a></span>
66
+ <span class='object_link'><a href="Elephas/Backends/Hash.html#data-instance_method" title="Elephas::Backends::Hash#data (method)">#data</a></span>
67
67
 
68
- <small>Elephas::Cache</small>
68
+ <small>Elephas::Backends::Hash</small>
69
69
 
70
70
  </li>
71
71
 
72
72
 
73
73
  <li class="r2 ">
74
- <span class='object_link'><a href="Elephas/Providers/Hash.html#delete-instance_method" title="Elephas::Providers::Hash#delete (method)">#delete</a></span>
74
+ <span class='object_link'><a href="Elephas/Backends/Base.html#delete-instance_method" title="Elephas::Backends::Base#delete (method)">#delete</a></span>
75
75
 
76
- <small>Elephas::Providers::Hash</small>
76
+ <small>Elephas::Backends::Base</small>
77
77
 
78
78
  </li>
79
79
 
80
80
 
81
81
  <li class="r1 ">
82
- <span class='object_link'><a href="Elephas/Providers/RubyOnRails.html#delete-instance_method" title="Elephas::Providers::RubyOnRails#delete (method)">#delete</a></span>
82
+ <span class='object_link'><a href="Elephas/Backends/RubyOnRails.html#delete-instance_method" title="Elephas::Backends::RubyOnRails#delete (method)">#delete</a></span>
83
83
 
84
- <small>Elephas::Providers::RubyOnRails</small>
84
+ <small>Elephas::Backends::RubyOnRails</small>
85
85
 
86
86
  </li>
87
87
 
88
88
 
89
89
  <li class="r2 ">
90
- <span class='object_link'><a href="Elephas/Cache.html#delete-class_method" title="Elephas::Cache.delete (method)">delete</a></span>
90
+ <span class='object_link'><a href="Elephas/Cache.html#delete-instance_method" title="Elephas::Cache#delete (method)">#delete</a></span>
91
91
 
92
92
  <small>Elephas::Cache</small>
93
93
 
@@ -95,9 +95,9 @@
95
95
 
96
96
 
97
97
  <li class="r1 ">
98
- <span class='object_link'><a href="Elephas/Providers/Base.html#delete-instance_method" title="Elephas::Providers::Base#delete (method)">#delete</a></span>
98
+ <span class='object_link'><a href="Elephas/Backends/Hash.html#delete-instance_method" title="Elephas::Backends::Hash#delete (method)">#delete</a></span>
99
99
 
100
- <small>Elephas::Providers::Base</small>
100
+ <small>Elephas::Backends::Hash</small>
101
101
 
102
102
  </li>
103
103
 
@@ -111,33 +111,33 @@
111
111
 
112
112
 
113
113
  <li class="r1 ">
114
- <span class='object_link'><a href="Elephas/Providers/Hash.html#exists%3F-instance_method" title="Elephas::Providers::Hash#exists? (method)">#exists?</a></span>
114
+ <span class='object_link'><a href="Elephas/Backends/Base.html#exists%3F-instance_method" title="Elephas::Backends::Base#exists? (method)">#exists?</a></span>
115
115
 
116
- <small>Elephas::Providers::Hash</small>
116
+ <small>Elephas::Backends::Base</small>
117
117
 
118
118
  </li>
119
119
 
120
120
 
121
121
  <li class="r2 ">
122
- <span class='object_link'><a href="Elephas/Cache.html#exists%3F-class_method" title="Elephas::Cache.exists? (method)">exists?</a></span>
122
+ <span class='object_link'><a href="Elephas/Backends/Hash.html#exists%3F-instance_method" title="Elephas::Backends::Hash#exists? (method)">#exists?</a></span>
123
123
 
124
- <small>Elephas::Cache</small>
124
+ <small>Elephas::Backends::Hash</small>
125
125
 
126
126
  </li>
127
127
 
128
128
 
129
129
  <li class="r1 ">
130
- <span class='object_link'><a href="Elephas/Providers/Base.html#exists%3F-instance_method" title="Elephas::Providers::Base#exists? (method)">#exists?</a></span>
130
+ <span class='object_link'><a href="Elephas/Cache.html#exists%3F-instance_method" title="Elephas::Cache#exists? (method)">#exists?</a></span>
131
131
 
132
- <small>Elephas::Providers::Base</small>
132
+ <small>Elephas::Cache</small>
133
133
 
134
134
  </li>
135
135
 
136
136
 
137
137
  <li class="r2 ">
138
- <span class='object_link'><a href="Elephas/Providers/RubyOnRails.html#exists%3F-instance_method" title="Elephas::Providers::RubyOnRails#exists? (method)">#exists?</a></span>
138
+ <span class='object_link'><a href="Elephas/Backends/RubyOnRails.html#exists%3F-instance_method" title="Elephas::Backends::RubyOnRails#exists? (method)">#exists?</a></span>
139
139
 
140
- <small>Elephas::Providers::RubyOnRails</small>
140
+ <small>Elephas::Backends::RubyOnRails</small>
141
141
 
142
142
  </li>
143
143
 
@@ -159,23 +159,23 @@
159
159
 
160
160
 
161
161
  <li class="r1 ">
162
- <span class='object_link'><a href="Elephas/Entry.html#initialize-instance_method" title="Elephas::Entry#initialize (method)">#initialize</a></span>
162
+ <span class='object_link'><a href="Elephas/Backends/Hash.html#initialize-instance_method" title="Elephas::Backends::Hash#initialize (method)">#initialize</a></span>
163
163
 
164
- <small>Elephas::Entry</small>
164
+ <small>Elephas::Backends::Hash</small>
165
165
 
166
166
  </li>
167
167
 
168
168
 
169
169
  <li class="r2 ">
170
- <span class='object_link'><a href="Elephas/Providers/Hash.html#initialize-instance_method" title="Elephas::Providers::Hash#initialize (method)">#initialize</a></span>
170
+ <span class='object_link'><a href="Elephas/Cache.html#initialize-instance_method" title="Elephas::Cache#initialize (method)">#initialize</a></span>
171
171
 
172
- <small>Elephas::Providers::Hash</small>
172
+ <small>Elephas::Cache</small>
173
173
 
174
174
  </li>
175
175
 
176
176
 
177
177
  <li class="r1 ">
178
- <span class='object_link'><a href="Elephas/Entry.html#key-instance_method" title="Elephas::Entry#key (method)">#key</a></span>
178
+ <span class='object_link'><a href="Elephas/Entry.html#initialize-instance_method" title="Elephas::Entry#initialize (method)">#initialize</a></span>
179
179
 
180
180
  <small>Elephas::Entry</small>
181
181
 
@@ -183,31 +183,31 @@
183
183
 
184
184
 
185
185
  <li class="r2 ">
186
- <span class='object_link'><a href="Elephas/Providers/Base.html#now-instance_method" title="Elephas::Providers::Base#now (method)">#now</a></span>
186
+ <span class='object_link'><a href="Elephas/Entry.html#key-instance_method" title="Elephas::Entry#key (method)">#key</a></span>
187
187
 
188
- <small>Elephas::Providers::Base</small>
188
+ <small>Elephas::Entry</small>
189
189
 
190
190
  </li>
191
191
 
192
192
 
193
193
  <li class="r1 ">
194
- <span class='object_link'><a href="Elephas/Cache.html#provider-class_method" title="Elephas::Cache.provider (method)">provider</a></span>
194
+ <span class='object_link'><a href="Elephas/Backends/Base.html#now-instance_method" title="Elephas::Backends::Base#now (method)">#now</a></span>
195
195
 
196
- <small>Elephas::Cache</small>
196
+ <small>Elephas::Backends::Base</small>
197
197
 
198
198
  </li>
199
199
 
200
200
 
201
201
  <li class="r2 ">
202
- <span class='object_link'><a href="Elephas/Providers/Base.html#read-instance_method" title="Elephas::Providers::Base#read (method)">#read</a></span>
202
+ <span class='object_link'><a href="Elephas/Cache.html#prefix-instance_method" title="Elephas::Cache#prefix (method)">#prefix</a></span>
203
203
 
204
- <small>Elephas::Providers::Base</small>
204
+ <small>Elephas::Cache</small>
205
205
 
206
206
  </li>
207
207
 
208
208
 
209
209
  <li class="r1 ">
210
- <span class='object_link'><a href="Elephas/Cache.html#read-class_method" title="Elephas::Cache.read (method)">read</a></span>
210
+ <span class='object_link'><a href="Elephas/Cache.html#read-instance_method" title="Elephas::Cache#read (method)">#read</a></span>
211
211
 
212
212
  <small>Elephas::Cache</small>
213
213
 
@@ -215,47 +215,47 @@
215
215
 
216
216
 
217
217
  <li class="r2 ">
218
- <span class='object_link'><a href="Elephas/Providers/RubyOnRails.html#read-instance_method" title="Elephas::Providers::RubyOnRails#read (method)">#read</a></span>
218
+ <span class='object_link'><a href="Elephas/Backends/RubyOnRails.html#read-instance_method" title="Elephas::Backends::RubyOnRails#read (method)">#read</a></span>
219
219
 
220
- <small>Elephas::Providers::RubyOnRails</small>
220
+ <small>Elephas::Backends::RubyOnRails</small>
221
221
 
222
222
  </li>
223
223
 
224
224
 
225
225
  <li class="r1 ">
226
- <span class='object_link'><a href="Elephas/Providers/Hash.html#read-instance_method" title="Elephas::Providers::Hash#read (method)">#read</a></span>
226
+ <span class='object_link'><a href="Elephas/Backends/Hash.html#read-instance_method" title="Elephas::Backends::Hash#read (method)">#read</a></span>
227
227
 
228
- <small>Elephas::Providers::Hash</small>
228
+ <small>Elephas::Backends::Hash</small>
229
229
 
230
230
  </li>
231
231
 
232
232
 
233
233
  <li class="r2 ">
234
- <span class='object_link'><a href="Elephas/Entry.html#refresh-instance_method" title="Elephas::Entry#refresh (method)">#refresh</a></span>
234
+ <span class='object_link'><a href="Elephas/Backends/Base.html#read-instance_method" title="Elephas::Backends::Base#read (method)">#read</a></span>
235
235
 
236
- <small>Elephas::Entry</small>
236
+ <small>Elephas::Backends::Base</small>
237
237
 
238
238
  </li>
239
239
 
240
240
 
241
241
  <li class="r1 ">
242
- <span class='object_link'><a href="Elephas/Cache.html#setup_options-class_method" title="Elephas::Cache.setup_options (method)">setup_options</a></span>
242
+ <span class='object_link'><a href="Elephas/Entry.html#refresh-instance_method" title="Elephas::Entry#refresh (method)">#refresh</a></span>
243
243
 
244
- <small>Elephas::Cache</small>
244
+ <small>Elephas::Entry</small>
245
245
 
246
246
  </li>
247
247
 
248
248
 
249
249
  <li class="r2 ">
250
- <span class='object_link'><a href="Elephas/Entry.html#ttl-instance_method" title="Elephas::Entry#ttl (method)">#ttl</a></span>
250
+ <span class='object_link'><a href="Elephas/Cache.html#setup_options-instance_method" title="Elephas::Cache#setup_options (method)">#setup_options</a></span>
251
251
 
252
- <small>Elephas::Entry</small>
252
+ <small>Elephas::Cache</small>
253
253
 
254
254
  </li>
255
255
 
256
256
 
257
257
  <li class="r1 ">
258
- <span class='object_link'><a href="Elephas/Entry.html#updated_at-instance_method" title="Elephas::Entry#updated_at (method)">#updated_at</a></span>
258
+ <span class='object_link'><a href="Elephas/Entry.html#ttl-instance_method" title="Elephas::Entry#ttl (method)">#ttl</a></span>
259
259
 
260
260
  <small>Elephas::Entry</small>
261
261
 
@@ -263,23 +263,23 @@
263
263
 
264
264
 
265
265
  <li class="r2 ">
266
- <span class='object_link'><a href="Elephas/Cache.html#use-class_method" title="Elephas::Cache.use (method)">use</a></span>
266
+ <span class='object_link'><a href="Elephas/Entry.html#updated_at-instance_method" title="Elephas::Entry#updated_at (method)">#updated_at</a></span>
267
267
 
268
- <small>Elephas::Cache</small>
268
+ <small>Elephas::Entry</small>
269
269
 
270
270
  </li>
271
271
 
272
272
 
273
273
  <li class="r1 ">
274
- <span class='object_link'><a href="Elephas/Entry.html#valid%3F-instance_method" title="Elephas::Entry#valid? (method)">#valid?</a></span>
274
+ <span class='object_link'><a href="Elephas/Cache.html#use-instance_method" title="Elephas::Cache#use (method)">#use</a></span>
275
275
 
276
- <small>Elephas::Entry</small>
276
+ <small>Elephas::Cache</small>
277
277
 
278
278
  </li>
279
279
 
280
280
 
281
281
  <li class="r2 ">
282
- <span class='object_link'><a href="Elephas/Entry.html#value-instance_method" title="Elephas::Entry#value (method)">#value</a></span>
282
+ <span class='object_link'><a href="Elephas/Entry.html#valid%3F-instance_method" title="Elephas::Entry#valid? (method)">#valid?</a></span>
283
283
 
284
284
  <small>Elephas::Entry</small>
285
285
 
@@ -287,37 +287,45 @@
287
287
 
288
288
 
289
289
  <li class="r1 ">
290
- <span class='object_link'><a href="Elephas/Providers/Hash.html#write-instance_method" title="Elephas::Providers::Hash#write (method)">#write</a></span>
290
+ <span class='object_link'><a href="Elephas/Entry.html#value-instance_method" title="Elephas::Entry#value (method)">#value</a></span>
291
291
 
292
- <small>Elephas::Providers::Hash</small>
292
+ <small>Elephas::Entry</small>
293
293
 
294
294
  </li>
295
295
 
296
296
 
297
297
  <li class="r2 ">
298
- <span class='object_link'><a href="Elephas/Providers/RubyOnRails.html#write-instance_method" title="Elephas::Providers::RubyOnRails#write (method)">#write</a></span>
298
+ <span class='object_link'><a href="Elephas/Backends/Base.html#write-instance_method" title="Elephas::Backends::Base#write (method)">#write</a></span>
299
299
 
300
- <small>Elephas::Providers::RubyOnRails</small>
300
+ <small>Elephas::Backends::Base</small>
301
301
 
302
302
  </li>
303
303
 
304
304
 
305
305
  <li class="r1 ">
306
- <span class='object_link'><a href="Elephas/Providers/Base.html#write-instance_method" title="Elephas::Providers::Base#write (method)">#write</a></span>
306
+ <span class='object_link'><a href="Elephas/Backends/RubyOnRails.html#write-instance_method" title="Elephas::Backends::RubyOnRails#write (method)">#write</a></span>
307
307
 
308
- <small>Elephas::Providers::Base</small>
308
+ <small>Elephas::Backends::RubyOnRails</small>
309
309
 
310
310
  </li>
311
311
 
312
312
 
313
313
  <li class="r2 ">
314
- <span class='object_link'><a href="Elephas/Cache.html#write-class_method" title="Elephas::Cache.write (method)">write</a></span>
314
+ <span class='object_link'><a href="Elephas/Cache.html#write-instance_method" title="Elephas::Cache#write (method)">#write</a></span>
315
315
 
316
316
  <small>Elephas::Cache</small>
317
317
 
318
318
  </li>
319
319
 
320
320
 
321
+ <li class="r1 ">
322
+ <span class='object_link'><a href="Elephas/Backends/Hash.html#write-instance_method" title="Elephas::Backends::Hash#write (method)">#write</a></span>
323
+
324
+ <small>Elephas::Backends::Hash</small>
325
+
326
+ </li>
327
+
328
+
321
329
  </ul>
322
330
  </div>
323
331
  </body>
@@ -103,7 +103,7 @@
103
103
  </div>
104
104
 
105
105
  <div id="footer">
106
- Generated on Thu Feb 28 10:48:17 2013 by
106
+ Generated on Sun Mar 24 12:12:01 2013 by
107
107
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
108
108
  0.8.5.2 (ruby-1.9.3).
109
109
  </div>
data/elephas.gemspec CHANGED
@@ -24,5 +24,5 @@ Gem::Specification.new do |gem|
24
24
 
25
25
  gem.required_ruby_version = ">= 1.9.3"
26
26
 
27
- gem.add_dependency("lazier", "~> 2.7.0")
27
+ gem.add_dependency("lazier", "~> 2.8.0")
28
28
  end
@@ -6,11 +6,9 @@
6
6
 
7
7
  module Elephas
8
8
  # This module hosts all the storage system for the caches.
9
- module Providers
10
-
11
- # The base provider, with all methods a valid provider should override.
12
- module Base
13
- extend ActiveSupport::Concern
9
+ module Backends
10
+ # The a base backend. All data-related methods must be overriden.
11
+ class Base
14
12
  include Lazier::I18n
15
13
 
16
14
  # Reads a value from the cache.
@@ -58,7 +56,7 @@ module Elephas
58
56
  private
59
57
  # Marks a method as unimplemented.
60
58
  def unimplemented
61
- self.i18n_setup(:elephas, ::File.absolute_path(::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/")) if !@i18n
59
+ self.i18n_setup(:elephas, ::File.absolute_path(::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../../locales/")) if !@i18n
62
60
  raise ArgumentError.new(self.i18n.unimplemented)
63
61
  end
64
62
  end
@@ -5,16 +5,16 @@
5
5
  #
6
6
 
7
7
  module Elephas
8
- module Providers
9
- # This is a simple providers, which uses an hash for storing the values.
8
+ module Backends
9
+ # This is a simple backend, which uses an hash for storing the values.
10
10
  #
11
11
  # @attribute data
12
- # @return [Hash] The internal hash used by the provider.
13
- class Hash
14
- include Elephas::Providers::Base
12
+ # @return [Hash] The internal hash used by the backend.
13
+ class Hash < Base
15
14
  attr_accessor :data
16
15
 
17
- # Initialize the provider
16
+ # Initialize the backend.
17
+ #
18
18
  # @param data [Hash] The initial data stored.
19
19
  def initialize(data = nil)
20
20
  @data = data && data.is_a?(::Hash) ? data : {}
@@ -59,7 +59,7 @@ module Elephas
59
59
  # @return [Boolean] `true` if the key is in the cache, `false` otherwise.
60
60
  def exists?(key)
61
61
  key = key.ensure_string
62
- @data.has_key?(key) && @data[key].valid?
62
+ @data.has_key?(key) && @data[key].valid?(self)
63
63
  end
64
64
  end
65
65
  end
@@ -5,11 +5,9 @@
5
5
  #
6
6
 
7
7
  module Elephas
8
- module Providers
9
- # This is a Ruby on Rails providers, which uses Rails.cache.
10
- class RubyOnRails
11
- include Elephas::Providers::Base
12
-
8
+ module Backends
9
+ # This is a Ruby on Rails backend, which uses Rails.cache.
10
+ class RubyOnRails < Base
13
11
  # Reads a value from the cache.
14
12
  #
15
13
  # @param key [String] The key to lookup.
@@ -49,7 +47,7 @@ module Elephas
49
47
  # @return [Boolean] `true` if the key is in the cache, `false` otherwise.
50
48
  def exists?(key)
51
49
  key = key.ensure_string
52
- Rails.cache.exist?(key) && Rails.cache.read(key).valid?
50
+ Rails.cache.exist?(key) && Rails.cache.read(key).valid?(self)
53
51
  end
54
52
  end
55
53
  end
data/lib/elephas/cache.rb CHANGED
@@ -6,11 +6,21 @@
6
6
 
7
7
  module Elephas
8
8
  # This is the main class of the framework. Use only this class to access the cache.
9
+ #
10
+ # @attribute backend
11
+ # @return [Backend] The backend used for the caching.
12
+ # @attribute prefix
13
+ # @return [String] The default prefix for cache entries.
9
14
  class Cache
10
- # @attribute provider
11
- # @return [Provider] The provider used for the caching.
12
- class << self
13
- attr_accessor :provider
15
+ attr_accessor :backend
16
+ attr_accessor :prefix
17
+
18
+ # Initialize the cache.
19
+ #
20
+ # @param backend [Backends::Base] The backend to use. By default uses an Hash backend.
21
+ def initialize(backend = nil)
22
+ @backend = backend || Elephas::Backends::Hash.new
23
+ @prefix = "elephas-#{::Elephas::Version::STRING}-cache"
14
24
  end
15
25
 
16
26
  # This is the main method of the framework.
@@ -20,14 +30,16 @@ module Elephas
20
30
  # If it doesn't find it, it uses the provided block (which receives options as argument) to compute its value and then store it into the cache for later usages.
21
31
  #
22
32
  # ```ruby
23
- # value = Elephas::Cache.use("KEY") do |options|
33
+ # cache = Elephas::Cache.new(Elephas::Backends::Hash.new)
34
+ #
35
+ # value = cache.use("KEY") do |options|
24
36
  # "VALUE"
25
37
  # end
26
38
  #
27
39
  # value
28
40
  # # => "VALUE"
29
41
  #
30
- # value = Elephas::Cache.use("KEY") do |options|
42
+ # value = cache.use("KEY") do |options|
31
43
  # "ANOTHER VALUE"
32
44
  # end
33
45
  #
@@ -40,14 +52,14 @@ module Elephas
40
52
  # @param block [Proc] An optional block to run to compute the value for the key if nothing is found.
41
53
  # @return [Object|Entry] The found or newly-set value associated to the key.
42
54
  # @see .setup_options
43
- def self.use(key, options = {}, &block)
55
+ def use(key, options = {}, &block)
44
56
  rv = nil
45
57
 
46
58
  # Get options
47
- options = ::Elephas::Cache.setup_options(options, key)
59
+ options = self.setup_options(options, key)
48
60
 
49
61
  # Check if the storage has the value (if we don't have to skip the cache)
50
- rv = @provider.read(options[:hash]) if options[:force] == false && options[:ttl] > 0
62
+ rv = choose_backend(options).read(options[:hash]) if options[:force] == false && options[:ttl] > 0
51
63
  rv = compute_value(options, &block) if rv.nil? && block # Try to compute the value from the block
52
64
 
53
65
  # Return value
@@ -57,9 +69,10 @@ module Elephas
57
69
  # Reads a value from the cache.
58
70
  #
59
71
  # @param key [String] The key to lookup.
72
+ # @param backend [Backends::Base|NilClass] The backend to use. Defaults to the current backend.
60
73
  # @return [Object|NilClass] The read value or `nil`.
61
- def self.read(key)
62
- @provider.read(key)
74
+ def read(key, backend = nil)
75
+ choose_backend({backend: backend}).read(key)
63
76
  end
64
77
 
65
78
  # Writes a value to the cache.
@@ -69,31 +82,26 @@ module Elephas
69
82
  # @param options [Hash] A list of options for writing.
70
83
  # @see .setup_options
71
84
  # @return [Object] The value itself.
72
- def self.write(key, value, options = {})
73
- @provider.write(key, value, ::Elephas::Cache.setup_options(options, key))
85
+ def write(key, value, options = {})
86
+ choose_backend(options).write(key, value, self.setup_options(options, key))
74
87
  end
75
88
 
76
89
  # Deletes a value from the cache.
77
90
  #
78
91
  # @param key [String] The key to delete.
92
+ # @param backend [Backends::Base|NilClass] The backend to use. Defaults to the current backend.
79
93
  # @return [Boolean] `true` if the key was in the cache, `false` otherwise.
80
- def self.delete(key)
81
- @provider.delete(key)
94
+ def delete(key, backend = nil)
95
+ choose_backend({backend: backend}).delete(key)
82
96
  end
83
97
 
84
98
  # Checks if a key exists in the cache.
85
99
  #
86
100
  # @param key [String] The key to lookup.
101
+ # @param backend [Backends::Base|NilClass] The backend to use. Defaults to the current backend.
87
102
  # @return [Boolean] `true` if the key is in the cache, `false` otherwise.
88
- def self.exists?(key)
89
- @provider.exists?(key)
90
- end
91
-
92
- # Returns the default prefix for cache entries.
93
- #
94
- # @return [String] The default prefix for cache entries.
95
- def self.default_prefix
96
- "elephas-#{::Elephas::Version::STRING}-cache"
103
+ def exists?(key, backend = nil)
104
+ choose_backend({backend: backend}).exists?(key)
97
105
  end
98
106
 
99
107
  # Setups options for use into the framework.
@@ -110,7 +118,7 @@ module Elephas
110
118
  # @param options [Object] An initial setup.
111
119
  # @param key [String] The key to associate to this options.
112
120
  # @return [Hash] An options hash.
113
- def self.setup_options(options, key)
121
+ def setup_options(options, key)
114
122
  options = {} if !options.is_a?(::Hash)
115
123
  options = {ttl: 1.hour * 1000, force: false, as_entry: false}.merge(options)
116
124
 
@@ -128,26 +136,36 @@ module Elephas
128
136
 
129
137
  private
130
138
  # Computes a new value and saves it to the cache.
139
+ #
131
140
  # @param options [Hash] A list of options for managing the value.
132
141
  # @param block [Proc] The block to run to compute the value.
133
142
  # @return [Object|Entry] The new value.
134
- def self.compute_value(options, &block)
143
+ def compute_value(options, &block)
135
144
  rv = block.call(options)
136
145
  rv = ::Elephas::Entry.ensure(rv, options[:complete_key], options) # Make sure is an entry
137
- Elephas::Cache.write(rv.hash, rv, options) if !rv.value.nil? && options[:ttl] > 0 # We have a value and we have to store it
146
+ self.write(rv.hash, rv, options) if !rv.value.nil? && options[:ttl] > 0 # We have a value and we have to store it
138
147
  rv
139
148
  end
140
149
 
141
150
  # Sanitizes options for safe usage.
151
+ #
142
152
  # @param options [Object] An initial setup.
143
153
  # @param key [String] The key to associate to this options.
144
154
  # @return [Hash] An options hash.
145
- def self.sanitize_options(options, key)
155
+ def sanitize_options(options, key)
146
156
  options[:key] ||= key.ensure_string
147
157
  options[:ttl] == options[:ttl].blank? ? 1.hour * 1000 : [options[:ttl].to_integer, 0].max
148
158
  options[:force] = options[:force].to_boolean
149
- options[:prefix] = options[:prefix].present? ? options[:prefix] : "elephas-#{::Elephas::Version::STRING}-cache"
159
+ options[:prefix] = options[:prefix].present? ? options[:prefix] : self.prefix
150
160
  options
151
161
  end
162
+
163
+ # Choose a backend to use.
164
+ #
165
+ # @param options [Backends::Base|Hash] The backend to use. Defaults to the current backend.
166
+ def choose_backend(options)
167
+ backend = (options.is_a?(Hash) ? options.symbolize_keys[:backend] : options)
168
+ backend.is_a?(Elephas::Backends::Base) ? backend : self.backend
169
+ end
152
170
  end
153
171
  end
data/lib/elephas/entry.rb CHANGED
@@ -41,20 +41,20 @@ module Elephas
41
41
  # Refreshes the entry.
42
42
  #
43
43
  # @param save [Boolean] If to save the refresh value in the cache.
44
+ # @param cache [Cache] The cache where to save the entry.
44
45
  # @return [Float] The new updated_at value.
45
- def refresh(save = false)
46
+ def refresh(save = false, cache = nil)
46
47
  @updated_at = get_new_updated_at(@updated_at)
47
- Elephas::Cache.provider.write(@hash, self) if save
48
+ cache.write(@hash, self) if save && cache
48
49
  @updated_at
49
50
  end
50
51
 
51
52
  # Checks if the entry is still valid.
52
53
  #
53
- # @param provider [Provider::Base] The provider to use for the check.
54
+ # @param backend [Backends::Base] The backend to use for the check.
54
55
  # @return [Boolean] `true` if the entry is still valid, `false` otherwise.
55
- def valid?(provider = nil)
56
- provider ||= ::Elephas::Cache.provider
57
- provider.now - self.updated_at < self.ttl / 1000
56
+ def valid?(backend)
57
+ backend.now - self.updated_at < self.ttl / 1000
58
58
  end
59
59
 
60
60
  # Compares to another Entry.
@@ -11,10 +11,10 @@ module Elephas
11
11
  # @see http://semver.org
12
12
  module Version
13
13
  # The major version.
14
- MAJOR = 2
14
+ MAJOR = 3
15
15
 
16
16
  # The minor version.
17
- MINOR = 2
17
+ MINOR = 0
18
18
 
19
19
  # The patch version.
20
20
  PATCH = 0
data/lib/elephas.rb CHANGED
@@ -9,10 +9,8 @@ require "digest/sha2"
9
9
  Lazier.load!("boolean", "math", "object")
10
10
 
11
11
  require "elephas/version" if !defined?(Elephas::Version)
12
+ require "elephas/backends/base"
13
+ require "elephas/backends/hash"
14
+ require "elephas/backends/ruby_on_rails"
12
15
  require "elephas/entry"
13
- require "elephas/provider"
14
- require "elephas/providers/hash"
15
- require "elephas/providers/ruby_on_rails"
16
- require "elephas/cache"
17
-
18
- Elephas::Cache.provider = defined?(Rails) ? Elephas::Providers::RubyOnRails.new : Elephas::Providers::Hash.new
16
+ require "elephas/cache"
data/locales/en.yml CHANGED
@@ -6,4 +6,4 @@
6
6
 
7
7
  ---
8
8
  elephas:
9
- unimplemented: "A Elephas::Providers subclass must override this method."
9
+ unimplemented: "A Elephas::Backends::Base subclass must override this method."
data/locales/it.yml CHANGED
@@ -6,4 +6,4 @@
6
6
 
7
7
  ---
8
8
  elephas:
9
- unimplemented: "Una sottoclasse di Elephas::Providers deve effettuare l'override di questo metodo."
9
+ unimplemented: "Una sottoclasse di Elephas::Backends::Base deve effettuare l'override di questo metodo."