elephas 0.1.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/doc/Elephas.html CHANGED
@@ -121,7 +121,7 @@ Licensed under the MIT license, which can be found at <a href="http://www.openso
121
121
  </div>
122
122
 
123
123
  <div id="footer">
124
- Generated on Sat Jul 28 14:29:51 2012 by
124
+ Generated on Sun Jul 29 10:32:07 2012 by
125
125
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
126
126
  0.8.2.1 (ruby-1.9.2).
127
127
  </div>
data/doc/_index.html CHANGED
@@ -198,7 +198,7 @@
198
198
  </div>
199
199
 
200
200
  <div id="footer">
201
- Generated on Sat Jul 28 14:29:51 2012 by
201
+ Generated on Sun Jul 29 10:32:07 2012 by
202
202
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
203
203
  0.8.2.1 (ruby-1.9.2).
204
204
  </div>
data/doc/file.README.html CHANGED
@@ -66,23 +66,49 @@
66
66
  <p><a href="http://travis-ci.org/ShogunPanda/elephas"><img src="https://secure.travis-ci.org/ShogunPanda/elephas.png?branch=master" alt="Build Status"></a>
67
67
  <a href="https://gemnasium.com/ShogunPanda/elephas"><img src="https://gemnasium.com/ShogunPanda/elephas.png?travis" alt="Dependency Status"></a></p>
68
68
 
69
- <p>A storage agnostic caching framework.
70
- <a href="http://github.com/ShogunPanda/elephas">http://github.com/ShogunPanda/elephas</a></p>
69
+ <p>A storage agnostic caching framework.</p>
70
+
71
+ <p><a href="http://github.com/ShogunPanda/elephas">http://github.com/ShogunPanda/elephas</a></p>
71
72
 
72
73
  <h2>Usage</h2>
73
74
 
74
- <p><em>TODO</em></p>
75
+ <p>The usage of the framework is really simple.</p>
76
+
77
+ <p>At first you have to setup a provider (that is, a storage) for the Elephas. By default it uses an internal hash, and also Rails is supported.</p>
78
+
79
+ <pre class="code ruby"><code><span class='const'>Elephas</span><span class='op'>::</span><span class='const'>Cache</span><span class='period'>.</span><span class='id identifier rubyid_provider'>provider</span> <span class='op'>=</span> <span class='const'>Elephas</span><span class='op'>::</span><span class='const'>Providers</span><span class='op'>::</span><span class='const'>RubyOnRails</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span>
80
+ </code></pre>
81
+
82
+ <p>After that, you can query the framework for a value use the <code>use</code> method.</p>
83
+
84
+ <p>You should also pass a block to the method, so that the framework use that for computing the value of the cache entry.</p>
85
+
86
+ <pre class="code ruby"><code><span class='id identifier rubyid_value'>value</span> <span class='op'>=</span> <span class='const'>Elephas</span><span class='op'>::</span><span class='const'>Cache</span><span class='period'>.</span><span class='id identifier rubyid_use'>use</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>KEY</span><span class='tstring_end'>&quot;</span></span><span class='rparen'>)</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_options'>options</span><span class='op'>|</span>
87
+ <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>VALUE</span><span class='tstring_end'>&quot;</span></span>
88
+ <span class='kw'>end</span>
89
+ <span class='comment'># =&gt; &quot;VALUE&quot;
90
+ </span></code></pre>
91
+
92
+ <p>The next time you issue this call, the block won&#39;t be called.</p>
93
+
94
+ <p>The block takes an argument, which contains all the options for the entry.</p>
95
+
96
+ <p>You can see <code>Elephas::Cache.setup_options</code> documentation to see what options are supported.</p>
97
+
98
+ <p>For now, you just have to know that you can set the <code>:ttl</code> option to specify how long the value will stay in the cache (in milliseconds). Setting it to a non-positive value means to never cache the value.</p>
75
99
 
76
100
  <p>See documentation for more informations.</p>
77
101
 
102
+ <p><strong>You&#39;re done!</strong></p>
103
+
78
104
  <h2>Contributing to elephas</h2>
79
105
 
80
106
  <ul>
81
- <li>Check out the latest master to make sure the feature hasn&#39;t been implemented or the bug hasn&#39;t been fixed yet</li>
82
- <li>Check out the issue tracker to make sure someone already hasn&#39;t requested it and/or contributed it</li>
83
- <li>Fork the project</li>
84
- <li>Start a feature/bugfix branch</li>
85
- <li>Commit and push until you are happy with your contribution</li>
107
+ <li>Check out the latest master to make sure the feature hasn&#39;t been implemented or the bug hasn&#39;t been fixed yet.</li>
108
+ <li>Check out the issue tracker to make sure someone already hasn&#39;t requested it and/or contributed it.</li>
109
+ <li>Fork the project.</li>
110
+ <li>Start a feature/bugfix branch.</li>
111
+ <li>Commit and push until you are happy with your contribution.</li>
86
112
  <li>Make sure to add tests for it. This is important so I don&#39;t break it in a future version unintentionally.</li>
87
113
  <li>Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.</li>
88
114
  </ul>
@@ -94,7 +120,7 @@ Licensed under the MIT license, which can be found at <a href="http://www.openso
94
120
  </div></div>
95
121
 
96
122
  <div id="footer">
97
- Generated on Sat Jul 28 14:29:51 2012 by
123
+ Generated on Sun Jul 29 10:32:07 2012 by
98
124
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
99
125
  0.8.2.1 (ruby-1.9.2).
100
126
  </div>
data/doc/index.html CHANGED
@@ -66,23 +66,49 @@
66
66
  <p><a href="http://travis-ci.org/ShogunPanda/elephas"><img src="https://secure.travis-ci.org/ShogunPanda/elephas.png?branch=master" alt="Build Status"></a>
67
67
  <a href="https://gemnasium.com/ShogunPanda/elephas"><img src="https://gemnasium.com/ShogunPanda/elephas.png?travis" alt="Dependency Status"></a></p>
68
68
 
69
- <p>A storage agnostic caching framework.
70
- <a href="http://github.com/ShogunPanda/elephas">http://github.com/ShogunPanda/elephas</a></p>
69
+ <p>A storage agnostic caching framework.</p>
70
+
71
+ <p><a href="http://github.com/ShogunPanda/elephas">http://github.com/ShogunPanda/elephas</a></p>
71
72
 
72
73
  <h2>Usage</h2>
73
74
 
74
- <p><em>TODO</em></p>
75
+ <p>The usage of the framework is really simple.</p>
76
+
77
+ <p>At first you have to setup a provider (that is, a storage) for the Elephas. By default it uses an internal hash, and also Rails is supported.</p>
78
+
79
+ <pre class="code ruby"><code><span class='const'>Elephas</span><span class='op'>::</span><span class='const'>Cache</span><span class='period'>.</span><span class='id identifier rubyid_provider'>provider</span> <span class='op'>=</span> <span class='const'>Elephas</span><span class='op'>::</span><span class='const'>Providers</span><span class='op'>::</span><span class='const'>RubyOnRails</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span>
80
+ </code></pre>
81
+
82
+ <p>After that, you can query the framework for a value use the <code>use</code> method.</p>
83
+
84
+ <p>You should also pass a block to the method, so that the framework use that for computing the value of the cache entry.</p>
85
+
86
+ <pre class="code ruby"><code><span class='id identifier rubyid_value'>value</span> <span class='op'>=</span> <span class='const'>Elephas</span><span class='op'>::</span><span class='const'>Cache</span><span class='period'>.</span><span class='id identifier rubyid_use'>use</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>KEY</span><span class='tstring_end'>&quot;</span></span><span class='rparen'>)</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_options'>options</span><span class='op'>|</span>
87
+ <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>VALUE</span><span class='tstring_end'>&quot;</span></span>
88
+ <span class='kw'>end</span>
89
+ <span class='comment'># =&gt; &quot;VALUE&quot;
90
+ </span></code></pre>
91
+
92
+ <p>The next time you issue this call, the block won&#39;t be called.</p>
93
+
94
+ <p>The block takes an argument, which contains all the options for the entry.</p>
95
+
96
+ <p>You can see <code>Elephas::Cache.setup_options</code> documentation to see what options are supported.</p>
97
+
98
+ <p>For now, you just have to know that you can set the <code>:ttl</code> option to specify how long the value will stay in the cache (in milliseconds). Setting it to a non-positive value means to never cache the value.</p>
75
99
 
76
100
  <p>See documentation for more informations.</p>
77
101
 
102
+ <p><strong>You&#39;re done!</strong></p>
103
+
78
104
  <h2>Contributing to elephas</h2>
79
105
 
80
106
  <ul>
81
- <li>Check out the latest master to make sure the feature hasn&#39;t been implemented or the bug hasn&#39;t been fixed yet</li>
82
- <li>Check out the issue tracker to make sure someone already hasn&#39;t requested it and/or contributed it</li>
83
- <li>Fork the project</li>
84
- <li>Start a feature/bugfix branch</li>
85
- <li>Commit and push until you are happy with your contribution</li>
107
+ <li>Check out the latest master to make sure the feature hasn&#39;t been implemented or the bug hasn&#39;t been fixed yet.</li>
108
+ <li>Check out the issue tracker to make sure someone already hasn&#39;t requested it and/or contributed it.</li>
109
+ <li>Fork the project.</li>
110
+ <li>Start a feature/bugfix branch.</li>
111
+ <li>Commit and push until you are happy with your contribution.</li>
86
112
  <li>Make sure to add tests for it. This is important so I don&#39;t break it in a future version unintentionally.</li>
87
113
  <li>Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.</li>
88
114
  </ul>
@@ -94,7 +120,7 @@ Licensed under the MIT license, which can be found at <a href="http://www.openso
94
120
  </div></div>
95
121
 
96
122
  <div id="footer">
97
- Generated on Sat Jul 28 14:29:51 2012 by
123
+ Generated on Sun Jul 29 10:32:07 2012 by
98
124
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
99
125
  0.8.2.1 (ruby-1.9.2).
100
126
  </div>
data/doc/method_list.html CHANGED
@@ -47,25 +47,25 @@
47
47
 
48
48
 
49
49
  <li class="r1 ">
50
- <span class='object_link'><a href="Elephas/Providers/Hash.html#data-instance_method" title="Elephas::Providers::Hash#data (method)">#data</a></span>
50
+ <span class='object_link'><a href="Elephas/Entry.html#%3D%3D-instance_method" title="Elephas::Entry#== (method)">#==</a></span>
51
51
 
52
- <small>Elephas::Providers::Hash</small>
52
+ <small>Elephas::Entry</small>
53
53
 
54
54
  </li>
55
55
 
56
56
 
57
57
  <li class="r2 ">
58
- <span class='object_link'><a href="Elephas/Providers/RubyOnRails.html#delete-instance_method" title="Elephas::Providers::RubyOnRails#delete (method)">#delete</a></span>
58
+ <span class='object_link'><a href="Elephas/Providers/Hash.html#data-instance_method" title="Elephas::Providers::Hash#data (method)">#data</a></span>
59
59
 
60
- <small>Elephas::Providers::RubyOnRails</small>
60
+ <small>Elephas::Providers::Hash</small>
61
61
 
62
62
  </li>
63
63
 
64
64
 
65
65
  <li class="r1 ">
66
- <span class='object_link'><a href="Elephas/Providers/Base.html#delete-instance_method" title="Elephas::Providers::Base#delete (method)">#delete</a></span>
66
+ <span class='object_link'><a href="Elephas/Cache.html#default_prefix-class_method" title="Elephas::Cache.default_prefix (method)">default_prefix</a></span>
67
67
 
68
- <small>Elephas::Providers::Base</small>
68
+ <small>Elephas::Cache</small>
69
69
 
70
70
  </li>
71
71
 
@@ -79,6 +79,14 @@
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>
83
+
84
+ <small>Elephas::Providers::RubyOnRails</small>
85
+
86
+ </li>
87
+
88
+
89
+ <li class="r2 ">
82
90
  <span class='object_link'><a href="Elephas/Cache.html#delete-class_method" title="Elephas::Cache.delete (method)">delete</a></span>
83
91
 
84
92
  <small>Elephas::Cache</small>
@@ -86,6 +94,14 @@
86
94
  </li>
87
95
 
88
96
 
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>
99
+
100
+ <small>Elephas::Providers::Base</small>
101
+
102
+ </li>
103
+
104
+
89
105
  <li class="r2 ">
90
106
  <span class='object_link'><a href="Elephas/Entry.html#ensure-class_method" title="Elephas::Entry.ensure (method)">ensure</a></span>
91
107
 
@@ -95,33 +111,33 @@
95
111
 
96
112
 
97
113
  <li class="r1 ">
98
- <span class='object_link'><a href="Elephas/Providers/Base.html#exists%3F-instance_method" title="Elephas::Providers::Base#exists? (method)">#exists?</a></span>
114
+ <span class='object_link'><a href="Elephas/Providers/Hash.html#exists%3F-instance_method" title="Elephas::Providers::Hash#exists? (method)">#exists?</a></span>
99
115
 
100
- <small>Elephas::Providers::Base</small>
116
+ <small>Elephas::Providers::Hash</small>
101
117
 
102
118
  </li>
103
119
 
104
120
 
105
121
  <li class="r2 ">
106
- <span class='object_link'><a href="Elephas/Providers/Hash.html#exists%3F-instance_method" title="Elephas::Providers::Hash#exists? (method)">#exists?</a></span>
122
+ <span class='object_link'><a href="Elephas/Cache.html#exists%3F-class_method" title="Elephas::Cache.exists? (method)">exists?</a></span>
107
123
 
108
- <small>Elephas::Providers::Hash</small>
124
+ <small>Elephas::Cache</small>
109
125
 
110
126
  </li>
111
127
 
112
128
 
113
129
  <li class="r1 ">
114
- <span class='object_link'><a href="Elephas/Providers/RubyOnRails.html#exists%3F-instance_method" title="Elephas::Providers::RubyOnRails#exists? (method)">#exists?</a></span>
130
+ <span class='object_link'><a href="Elephas/Providers/Base.html#exists%3F-instance_method" title="Elephas::Providers::Base#exists? (method)">#exists?</a></span>
115
131
 
116
- <small>Elephas::Providers::RubyOnRails</small>
132
+ <small>Elephas::Providers::Base</small>
117
133
 
118
134
  </li>
119
135
 
120
136
 
121
137
  <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>
138
+ <span class='object_link'><a href="Elephas/Providers/RubyOnRails.html#exists%3F-instance_method" title="Elephas::Providers::RubyOnRails#exists? (method)">#exists?</a></span>
123
139
 
124
- <small>Elephas::Cache</small>
140
+ <small>Elephas::Providers::RubyOnRails</small>
125
141
 
126
142
  </li>
127
143
 
@@ -183,17 +199,17 @@
183
199
 
184
200
 
185
201
  <li class="r2 ">
186
- <span class='object_link'><a href="Elephas/Providers/Hash.html#read-instance_method" title="Elephas::Providers::Hash#read (method)">#read</a></span>
202
+ <span class='object_link'><a href="Elephas/Providers/Base.html#read-instance_method" title="Elephas::Providers::Base#read (method)">#read</a></span>
187
203
 
188
- <small>Elephas::Providers::Hash</small>
204
+ <small>Elephas::Providers::Base</small>
189
205
 
190
206
  </li>
191
207
 
192
208
 
193
209
  <li class="r1 ">
194
- <span class='object_link'><a href="Elephas/Providers/Base.html#read-instance_method" title="Elephas::Providers::Base#read (method)">#read</a></span>
210
+ <span class='object_link'><a href="Elephas/Cache.html#read-class_method" title="Elephas::Cache.read (method)">read</a></span>
195
211
 
196
- <small>Elephas::Providers::Base</small>
212
+ <small>Elephas::Cache</small>
197
213
 
198
214
  </li>
199
215
 
@@ -207,9 +223,9 @@
207
223
 
208
224
 
209
225
  <li class="r1 ">
210
- <span class='object_link'><a href="Elephas/Cache.html#read-class_method" title="Elephas::Cache.read (method)">read</a></span>
226
+ <span class='object_link'><a href="Elephas/Providers/Hash.html#read-instance_method" title="Elephas::Providers::Hash#read (method)">#read</a></span>
211
227
 
212
- <small>Elephas::Cache</small>
228
+ <small>Elephas::Providers::Hash</small>
213
229
 
214
230
  </li>
215
231
 
@@ -223,6 +239,14 @@
223
239
 
224
240
 
225
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>
243
+
244
+ <small>Elephas::Cache</small>
245
+
246
+ </li>
247
+
248
+
249
+ <li class="r2 ">
226
250
  <span class='object_link'><a href="Elephas/Entry.html#ttl-instance_method" title="Elephas::Entry#ttl (method)">#ttl</a></span>
227
251
 
228
252
  <small>Elephas::Entry</small>
@@ -230,7 +254,7 @@
230
254
  </li>
231
255
 
232
256
 
233
- <li class="r2 ">
257
+ <li class="r1 ">
234
258
  <span class='object_link'><a href="Elephas/Entry.html#updated_at-instance_method" title="Elephas::Entry#updated_at (method)">#updated_at</a></span>
235
259
 
236
260
  <small>Elephas::Entry</small>
@@ -238,7 +262,7 @@
238
262
  </li>
239
263
 
240
264
 
241
- <li class="r1 ">
265
+ <li class="r2 ">
242
266
  <span class='object_link'><a href="Elephas/Cache.html#use-class_method" title="Elephas::Cache.use (method)">use</a></span>
243
267
 
244
268
  <small>Elephas::Cache</small>
@@ -246,7 +270,7 @@
246
270
  </li>
247
271
 
248
272
 
249
- <li class="r2 ">
273
+ <li class="r1 ">
250
274
  <span class='object_link'><a href="Elephas/Entry.html#valid%3F-instance_method" title="Elephas::Entry#valid? (method)">#valid?</a></span>
251
275
 
252
276
  <small>Elephas::Entry</small>
@@ -254,7 +278,7 @@
254
278
  </li>
255
279
 
256
280
 
257
- <li class="r1 ">
281
+ <li class="r2 ">
258
282
  <span class='object_link'><a href="Elephas/Entry.html#value-instance_method" title="Elephas::Entry#value (method)">#value</a></span>
259
283
 
260
284
  <small>Elephas::Entry</small>
@@ -262,15 +286,15 @@
262
286
  </li>
263
287
 
264
288
 
265
- <li class="r2 ">
266
- <span class='object_link'><a href="Elephas/Cache.html#write-class_method" title="Elephas::Cache.write (method)">write</a></span>
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>
267
291
 
268
- <small>Elephas::Cache</small>
292
+ <small>Elephas::Providers::Hash</small>
269
293
 
270
294
  </li>
271
295
 
272
296
 
273
- <li class="r1 ">
297
+ <li class="r2 ">
274
298
  <span class='object_link'><a href="Elephas/Providers/RubyOnRails.html#write-instance_method" title="Elephas::Providers::RubyOnRails#write (method)">#write</a></span>
275
299
 
276
300
  <small>Elephas::Providers::RubyOnRails</small>
@@ -278,7 +302,7 @@
278
302
  </li>
279
303
 
280
304
 
281
- <li class="r2 ">
305
+ <li class="r1 ">
282
306
  <span class='object_link'><a href="Elephas/Providers/Base.html#write-instance_method" title="Elephas::Providers::Base#write (method)">#write</a></span>
283
307
 
284
308
  <small>Elephas::Providers::Base</small>
@@ -286,10 +310,10 @@
286
310
  </li>
287
311
 
288
312
 
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>
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>
291
315
 
292
- <small>Elephas::Providers::Hash</small>
316
+ <small>Elephas::Cache</small>
293
317
 
294
318
  </li>
295
319
 
@@ -103,7 +103,7 @@
103
103
  </div>
104
104
 
105
105
  <div id="footer">
106
- Generated on Sat Jul 28 14:29:51 2012 by
106
+ Generated on Sun Jul 29 10:32:07 2012 by
107
107
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
108
108
  0.8.2.1 (ruby-1.9.2).
109
109
  </div>
data/lib/elephas/cache.rb CHANGED
@@ -12,36 +12,43 @@ module Elephas
12
12
  attr_accessor :provider
13
13
 
14
14
  # This is the main method of the framework.
15
- # It tries reading a key from the cache. If it doesn't find it, it uses the provided block to compute its value and then store it into the cache for later usages.
15
+ #
16
+ # It tries reading a key from the cache.
17
+ #
18
+ # 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.
19
+ #
20
+ # ```ruby
21
+ # value = Elephas::Cache.use("KEY") do |options|
22
+ # "VALUE"
23
+ # end
24
+ #
25
+ # value
26
+ # # => "VALUE"
27
+ #
28
+ # value = Elephas::Cache.use("KEY") do |options|
29
+ # "ANOTHER VALUE"
30
+ # end
31
+ #
32
+ # value
33
+ # # => "VALUE"
34
+ # ```
16
35
  #
17
36
  # @param key [String] The key to lookup.
18
37
  # @return [Object|Entry] The found or newly-set value associated to the key.
38
+ # @see
19
39
  def use(key, options = {})
20
40
  rv = nil
21
41
 
22
42
  # Get options
23
- options = {} if !options.is_a?(::Hash)
24
- options = {:ttl => 1.hour, :force => false, :as_entry => false}.merge(options)
25
- options[:ttl] == [options[:ttl].to_integer, 0].max
26
- options[:force] = options[:force].to_boolean
27
- options[:prefix] = options[:prefix].present? ? options[:prefix] : "elephas-#{::Elephas::Version::STRING}-cache"
28
-
29
- # Wrap the final key to ensure we don't have colliding namespaces.
30
- fkey = "#{options[:prefix]}[#{key}]"
31
-
32
- # Compute the hash key used for referencing this value
33
- options[:hash] = options[:hash] || ::Elephas::Entry.hashify_key(fkey.ensure_string)
43
+ options = self.setup_options(options, key)
34
44
 
35
45
  # Check if the storage has the value (if we don't have to skip the cache)
36
46
  rv = self.provider.read(options[:hash]) if options[:force] == false && options[:ttl] > 0
37
47
 
38
48
  if rv.nil? && block_given? then # Try to compute the value from the block
39
- rv = yield(key, options)
40
-
41
- if rv && options[:ttl] > 0 then # We have a value and we have to store it
42
- rv = ::Elephas::Entry.ensure(rv, key, options) # Make sure is an entry
43
- Elephas::Cache.write(fkey, value, options)
44
- end
49
+ rv = yield(options)
50
+ rv = ::Elephas::Entry.ensure(rv, options[:complete_key], options) # Make sure is an entry
51
+ Elephas::Cache.write(rv.hash, rv, options) if !rv.value.nil? && options[:ttl] > 0 # We have a value and we have to store it
45
52
  end
46
53
 
47
54
  # Return value
@@ -61,18 +68,16 @@ module Elephas
61
68
  # @param key [String] The key to associate the value with.
62
69
  # @param value [Object] The value to write. Setting a value to `nil` **doesn't** mean *deleting* the value.
63
70
  # @param options [Hash] A list of options for writing.
64
- # TODO: Insert options documentation.
71
+ # @see .setup_options
65
72
  # @return [Object] The value itself.
66
73
  def write(key, value, options = {})
67
- # TODO: Handle options
68
-
69
- self.provider.write(key, value, options)
74
+ self.provider.write(key, value, self.setup_options(options, key))
70
75
  end
71
76
 
72
77
  # Deletes a value from the cache.
73
78
  #
74
79
  # @param key [String] The key to delete.
75
- # @return [TrueClass|FalseClass] `true` if the key was in the cache, `false` otherwise.
80
+ # @return [Boolean] `true` if the key was in the cache, `false` otherwise.
76
81
  def delete(key)
77
82
  self.provider.delete(key)
78
83
  end
@@ -80,10 +85,48 @@ module Elephas
80
85
  # Checks if a key exists in the cache.
81
86
  #
82
87
  # @param key [String] The key to lookup.
83
- # @return [TrueClass|FalseClass] `true` if the key is in the cache, `false` otherwise.
88
+ # @return [Boolean] `true` if the key is in the cache, `false` otherwise.
84
89
  def exists?(key)
85
90
  self.provider.exists?(key)
86
91
  end
92
+
93
+ # Returns the default prefix for cache entries.
94
+ #
95
+ # @return [String] The default prefix for cache entries.
96
+ def default_prefix
97
+ "elephas-#{::Elephas::Version::STRING}-cache"
98
+ end
99
+
100
+ # Setups options for use into the framework.
101
+ # Valid options are:
102
+ #
103
+ # * **:ttl**: The TTL (time to live, in milliseconds) of the entry. It means how long will the value stay in cache. Setting it to 0 or less means never cache the entry.
104
+ # * **:force**: Setting it to `true` will always skip the cache.
105
+ # * **:key**: The key associated to this value. **You should never set this option directly.**
106
+ # * **:prefix**: The prefix used in cache. This is used to avoid conflicts with other caching frameworks.
107
+ # * **:complete_key**: The complete key used for computing the hash. By default is concatenation of `:key` and `:prefix` options.
108
+ # * **:hash**: The hash used to store the key in the cache. Should be unique
109
+ # * **:as_entry**: In `Elephas::Cache.use`, setting this to `true` will return the entire `Entry` object rather than the value only.
110
+ #
111
+ # @param options [Object] An initial setup.
112
+ # @param key [String] The key to associate to this options.
113
+ # @return [Hash] An options hash.
114
+ def setup_options(options, key)
115
+ options = {} if !options.is_a?(::Hash)
116
+ options = {:ttl => 1.hour * 1000, :force => false, :as_entry => false}.merge(options)
117
+ options[:key] ||= key.ensure_string
118
+ options[:ttl] == options[:ttl].blank? ? 1.hour * 1000 : [options[:ttl].to_integer, 0].max
119
+ options[:force] = options[:force].to_boolean
120
+ options[:prefix] = options[:prefix].present? ? options[:prefix] : "elephas-#{::Elephas::Version::STRING}-cache"
121
+
122
+ # Wrap the final key to ensure we don't have colliding namespaces.
123
+ options[:complete_key] ||= "#{options[:prefix]}[#{options[:key]}]"
124
+
125
+ # Compute the hash key used for referencing this value
126
+ options[:hash] ||= ::Elephas::Entry.hashify_key(options[:complete_key])
127
+
128
+ options
129
+ end
87
130
  end
88
131
  end
89
132
  end
data/lib/elephas/entry.rb CHANGED
@@ -5,7 +5,7 @@
5
5
  #
6
6
 
7
7
  module Elephas
8
- # This represents a cache entry.
8
+ # Represents a cache entry.
9
9
  class Entry
10
10
  # The key for this entry.
11
11
  attr_accessor :key
@@ -28,7 +28,7 @@ module Elephas
28
28
  # @param value [Object] The value contained in this entry.
29
29
  # @param hash [String] The hash for this entry. Should be unique. It is automatically created if not provided.
30
30
  # @param ttl [Integer] The time to live (TTL) for this entry. If set to 0 then the entry is not cached.
31
- def initialize(key, value, hash = nil, ttl = 3600)
31
+ def initialize(key, value, hash = nil, ttl = 360000)
32
32
  hash = self.class.hashify_key(key) if hash.blank?
33
33
  self.key = key
34
34
  self.hash = hash
@@ -40,10 +40,12 @@ module Elephas
40
40
  # Refreshes the entry.
41
41
  #
42
42
  # @param save [Boolean] If to save the refresh value in the cache.
43
+ # @return [Float] The new updated_at value.
43
44
  def refresh(save = false)
44
45
  self.updated_at = Time.now.to_f
45
46
 
46
- # TODO: Set this value in the cache
47
+ Elephas::Cache.provider.write(self.hash, self) if save
48
+ self.updated_at
47
49
  end
48
50
 
49
51
  # Checks if the entry is still valid.
@@ -52,7 +54,15 @@ module Elephas
52
54
  # @return [Boolean] `true` if the entry is still valid, `false` otherwise.
53
55
  def valid?(provider = nil)
54
56
  provider ||= ::Elephas::Cache.provider
55
- provider.now - self.updated_at < self.ttl
57
+ provider.now - self.updated_at < self.ttl / 1000
58
+ end
59
+
60
+ # Compare to another Entry.
61
+ #
62
+ # @param other [Entry] The entry to compare with
63
+ # @return [Boolean] `true` if the entries are the same, `false` otherwise.
64
+ def ==(other)
65
+ other.is_a?(::Elephas::Entry) && [self.key, self.hash, self.value] == [other.key, other.hash, other.value]
56
66
  end
57
67
 
58
68
  # Returns a unique hash for the key.
@@ -17,17 +17,18 @@ module Elephas
17
17
  # @param key [String] The key to lookup.
18
18
  # @return [Entry|NilClass] The read value or `nil`.
19
19
  def read(key)
20
- raise ArgumentError.new("A Elephas::Providers subclass should override this module.")
20
+ raise ArgumentError.new("A Elephas::Providers subclass must override this method.")
21
21
  end
22
22
 
23
23
  # Writes a value to the cache.
24
24
  #
25
25
  # @param key [String] The key to associate the value with.
26
- # @param value [Object] The value to write. **Setting a value to `nil` doesn't mean *deleting* the value.
27
- # @param options [Hash] A list of options for writing. @see Elephas::Cache.write
26
+ # @param value [Object] The value to write. Setting a value to `nil` **doesn't** mean *deleting* the value.
27
+ # @param options [Hash] A list of options for writing.
28
+ # @see Elephas::Cache.setup_options
28
29
  # @return [Object] The value itself.
29
30
  def write(key, value, options = {})
30
- raise ArgumentError.new("A Elephas::Providers subclass should override this module.")
31
+ raise ArgumentError.new("A Elephas::Providers subclass must override this method.")
31
32
  end
32
33
 
33
34
  # Deletes a value from the cache.
@@ -35,7 +36,7 @@ module Elephas
35
36
  # @param key [String] The key to delete.
36
37
  # @return [TrueClass|FalseClass] `true` if the key was in the cache, `false` otherwise.
37
38
  def delete(key)
38
- raise ArgumentError.new("A Elephas::Providers subclass should override this module.")
39
+ raise ArgumentError.new("A Elephas::Providers subclass must override this method.")
39
40
  end
40
41
 
41
42
  # Checks if a key exists in the cache.
@@ -43,7 +44,7 @@ module Elephas
43
44
  # @param key [String] The key to lookup.
44
45
  # @return [TrueClass|FalseClass] `true` if the key is in the cache, `false` otherwise.
45
46
  def exists?(key)
46
- raise ArgumentError.new("A Elephas::Providers subclass should override this module.")
47
+ raise ArgumentError.new("A Elephas::Providers subclass must override this method.")
47
48
  end
48
49
 
49
50
  # Returns the current time for comparing with entries TTL.
@@ -31,8 +31,9 @@ module Elephas
31
31
  # Writes a value to the cache.
32
32
  #
33
33
  # @param key [String] The key to associate the value with.
34
- # @param value [Object] The value to write. **Setting a value to `nil` doesn't mean *deleting* the value.
35
- # @param options [Hash] A list of options for writing. @see Elephas::Cache.write
34
+ # @param value [Object] The value to write. Setting a value to `nil` **doesn't** mean *deleting* the value.
35
+ # @param options [Hash] A list of options for writing.
36
+ # @see Elephas::Cache.setup_options
36
37
  # @return [Object] The value itself.
37
38
  def write(key, value, options = {})
38
39
  fvalue = ::Elephas::Entry.ensure(value, key, options)