elephas 2.2.0 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +1 -1
- data/README.md +4 -4
- data/doc/Elephas/{Providers → Backends}/Base.html +48 -38
- data/doc/Elephas/{Providers → Backends}/Hash.html +35 -38
- data/doc/Elephas/{Providers → Backends}/RubyOnRails.html +32 -35
- data/doc/Elephas/{Providers.html → Backends.html} +8 -10
- data/doc/Elephas/Cache.html +894 -111
- data/doc/Elephas/Entry.html +63 -50
- data/doc/Elephas/Version.html +3 -3
- data/doc/Elephas.html +3 -3
- data/doc/_index.html +14 -22
- data/doc/class_list.html +1 -1
- data/doc/file.README.html +5 -5
- data/doc/index.html +5 -5
- data/doc/method_list.html +62 -54
- data/doc/top-level-namespace.html +1 -1
- data/elephas.gemspec +1 -1
- data/lib/elephas/{provider.rb → backends/base.rb} +4 -6
- data/lib/elephas/{providers → backends}/hash.rb +7 -7
- data/lib/elephas/{providers → backends}/ruby_on_rails.rb +4 -6
- data/lib/elephas/cache.rb +47 -29
- data/lib/elephas/entry.rb +6 -6
- data/lib/elephas/version.rb +2 -2
- data/lib/elephas.rb +4 -6
- data/locales/en.yml +1 -1
- data/locales/it.yml +1 -1
- data/spec/elephas/cache_spec.rb +32 -35
- data/spec/elephas/entry_spec.rb +6 -5
- data/spec/elephas/{provider_spec.rb → providers/base_spec.rb} +2 -6
- data/spec/elephas/providers/hash_spec.rb +6 -6
- data/spec/elephas/providers/ruby_on_rails_spec.rb +2 -2
- metadata +14 -14
data/doc/Elephas/Cache.html
CHANGED
@@ -113,13 +113,13 @@
|
|
113
113
|
|
114
114
|
|
115
115
|
|
116
|
-
<h2>
|
116
|
+
<h2>Instance Attribute Summary <small>(<a href="#" class="summary_toggle">collapse</a>)</small></h2>
|
117
117
|
<ul class="summary">
|
118
118
|
|
119
119
|
<li class="public ">
|
120
120
|
<span class="summary_signature">
|
121
121
|
|
122
|
-
<a href="#
|
122
|
+
<a href="#backend-instance_method" title="#backend (instance method)">- (Backend) <strong>backend</strong> </a>
|
123
123
|
|
124
124
|
|
125
125
|
|
@@ -136,7 +136,33 @@
|
|
136
136
|
|
137
137
|
|
138
138
|
|
139
|
-
<span class="summary_desc"><div class='inline'><p>
|
139
|
+
<span class="summary_desc"><div class='inline'><p>The backend used for the caching.</p>
|
140
|
+
</div></span>
|
141
|
+
|
142
|
+
</li>
|
143
|
+
|
144
|
+
|
145
|
+
<li class="public ">
|
146
|
+
<span class="summary_signature">
|
147
|
+
|
148
|
+
<a href="#prefix-instance_method" title="#prefix (instance method)">- (String) <strong>prefix</strong> </a>
|
149
|
+
|
150
|
+
|
151
|
+
|
152
|
+
</span>
|
153
|
+
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
<span class="summary_desc"><div class='inline'><p>The default prefix for cache entries.</p>
|
140
166
|
</div></span>
|
141
167
|
|
142
168
|
</li>
|
@@ -149,7 +175,7 @@
|
|
149
175
|
|
150
176
|
|
151
177
|
<h2>
|
152
|
-
|
178
|
+
Instance Method Summary
|
153
179
|
<small>(<a href="#" class="summary_toggle">collapse</a>)</small>
|
154
180
|
</h2>
|
155
181
|
|
@@ -158,7 +184,7 @@
|
|
158
184
|
<li class="public ">
|
159
185
|
<span class="summary_signature">
|
160
186
|
|
161
|
-
<a href="#
|
187
|
+
<a href="#delete-instance_method" title="#delete (instance method)">- (Boolean) <strong>delete</strong>(key, backend = nil) </a>
|
162
188
|
|
163
189
|
|
164
190
|
|
@@ -172,7 +198,7 @@
|
|
172
198
|
|
173
199
|
|
174
200
|
|
175
|
-
<span class="summary_desc"><div class='inline'><p>
|
201
|
+
<span class="summary_desc"><div class='inline'><p>Deletes a value from the cache.</p>
|
176
202
|
</div></span>
|
177
203
|
|
178
204
|
</li>
|
@@ -181,7 +207,7 @@
|
|
181
207
|
<li class="public ">
|
182
208
|
<span class="summary_signature">
|
183
209
|
|
184
|
-
<a href="#
|
210
|
+
<a href="#exists%3F-instance_method" title="#exists? (instance method)">- (Boolean) <strong>exists?</strong>(key, backend = nil) </a>
|
185
211
|
|
186
212
|
|
187
213
|
|
@@ -195,7 +221,7 @@
|
|
195
221
|
|
196
222
|
|
197
223
|
|
198
|
-
<span class="summary_desc"><div class='inline'><p>
|
224
|
+
<span class="summary_desc"><div class='inline'><p>Checks if a key exists in the cache.</p>
|
199
225
|
</div></span>
|
200
226
|
|
201
227
|
</li>
|
@@ -204,13 +230,15 @@
|
|
204
230
|
<li class="public ">
|
205
231
|
<span class="summary_signature">
|
206
232
|
|
207
|
-
<a href="#
|
233
|
+
<a href="#initialize-instance_method" title="#initialize (instance method)">- (Cache) <strong>initialize</strong>(backend = nil) </a>
|
208
234
|
|
209
235
|
|
210
236
|
|
211
237
|
</span>
|
212
238
|
|
213
239
|
|
240
|
+
<span class="note title constructor">constructor</span>
|
241
|
+
|
214
242
|
|
215
243
|
|
216
244
|
|
@@ -218,7 +246,7 @@
|
|
218
246
|
|
219
247
|
|
220
248
|
|
221
|
-
<span class="summary_desc"><div class='inline'><p>
|
249
|
+
<span class="summary_desc"><div class='inline'><p>Initialize the cache.</p>
|
222
250
|
</div></span>
|
223
251
|
|
224
252
|
</li>
|
@@ -227,7 +255,7 @@
|
|
227
255
|
<li class="public ">
|
228
256
|
<span class="summary_signature">
|
229
257
|
|
230
|
-
<a href="#read-
|
258
|
+
<a href="#read-instance_method" title="#read (instance method)">- (Object|NilClass) <strong>read</strong>(key, backend = nil) </a>
|
231
259
|
|
232
260
|
|
233
261
|
|
@@ -250,7 +278,7 @@
|
|
250
278
|
<li class="public ">
|
251
279
|
<span class="summary_signature">
|
252
280
|
|
253
|
-
<a href="#setup_options-
|
281
|
+
<a href="#setup_options-instance_method" title="#setup_options (instance method)">- (Hash) <strong>setup_options</strong>(options, key) </a>
|
254
282
|
|
255
283
|
|
256
284
|
|
@@ -273,7 +301,7 @@
|
|
273
301
|
<li class="public ">
|
274
302
|
<span class="summary_signature">
|
275
303
|
|
276
|
-
<a href="#use-
|
304
|
+
<a href="#use-instance_method" title="#use (instance method)">- (Object|Entry) <strong>use</strong>(key, options = {}, &block) </a>
|
277
305
|
|
278
306
|
|
279
307
|
|
@@ -296,7 +324,7 @@
|
|
296
324
|
<li class="public ">
|
297
325
|
<span class="summary_signature">
|
298
326
|
|
299
|
-
<a href="#write-
|
327
|
+
<a href="#write-instance_method" title="#write (instance method)">- (Object) <strong>write</strong>(key, value, options = {}) </a>
|
300
328
|
|
301
329
|
|
302
330
|
|
@@ -319,16 +347,13 @@
|
|
319
347
|
</ul>
|
320
348
|
|
321
349
|
|
322
|
-
|
323
|
-
<
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
<span id="provider=-class_method"></span>
|
328
|
-
<div class="method_details first">
|
329
|
-
<h3 class="signature first" id="provider-class_method">
|
350
|
+
<div id="constructor_details" class="method_details_list">
|
351
|
+
<h2>Constructor Details</h2>
|
352
|
+
|
353
|
+
<div class="method_details first">
|
354
|
+
<h3 class="signature first" id="initialize-instance_method">
|
330
355
|
|
331
|
-
|
356
|
+
- (<tt><span class='object_link'><a href="" title="Elephas::Cache (class)">Cache</a></span></tt>) <strong>initialize</strong>(backend = nil)
|
332
357
|
|
333
358
|
|
334
359
|
|
@@ -336,13 +361,34 @@
|
|
336
361
|
|
337
362
|
</h3><div class="docstring">
|
338
363
|
<div class="discussion">
|
339
|
-
<p>
|
364
|
+
<p>Initialize the cache.</p>
|
340
365
|
|
341
366
|
|
342
367
|
</div>
|
343
368
|
</div>
|
344
369
|
<div class="tags">
|
370
|
+
<p class="tag_title">Parameters:</p>
|
371
|
+
<ul class="param">
|
345
372
|
|
373
|
+
<li>
|
374
|
+
|
375
|
+
<span class='name'>backend</span>
|
376
|
+
|
377
|
+
|
378
|
+
<span class='type'>(<tt><span class='object_link'><a href="Backends/Base.html" title="Elephas::Backends::Base (class)">Backends::Base</a></span></tt>)</span>
|
379
|
+
|
380
|
+
|
381
|
+
<em class="default">(defaults to: <tt>nil</tt>)</em>
|
382
|
+
|
383
|
+
|
384
|
+
—
|
385
|
+
<div class='inline'><p>The backend to use. By default uses an Hash backend.</p>
|
386
|
+
</div>
|
387
|
+
|
388
|
+
</li>
|
389
|
+
|
390
|
+
</ul>
|
391
|
+
|
346
392
|
|
347
393
|
</div><table class="source_code">
|
348
394
|
<tr>
|
@@ -350,32 +396,401 @@
|
|
350
396
|
<pre class="lines">
|
351
397
|
|
352
398
|
|
353
|
-
|
354
|
-
|
355
|
-
|
399
|
+
21
|
400
|
+
22
|
401
|
+
23
|
402
|
+
24</pre>
|
356
403
|
</td>
|
357
404
|
<td>
|
358
|
-
<pre class="code"><span class="info file"># File 'lib/elephas/cache.rb', line
|
405
|
+
<pre class="code"><span class="info file"># File 'lib/elephas/cache.rb', line 21</span>
|
359
406
|
|
360
|
-
<span class='kw'>def</span> <span class='id identifier
|
361
|
-
<span class='ivar'>@
|
407
|
+
<span class='kw'>def</span> <span class='id identifier rubyid_initialize'>initialize</span><span class='lparen'>(</span><span class='id identifier rubyid_backend'>backend</span> <span class='op'>=</span> <span class='kw'>nil</span><span class='rparen'>)</span>
|
408
|
+
<span class='ivar'>@backend</span> <span class='op'>=</span> <span class='id identifier rubyid_backend'>backend</span> <span class='op'>||</span> <span class='const'>Elephas</span><span class='op'>::</span><span class='const'>Backends</span><span class='op'>::</span><span class='const'>Hash</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span>
|
409
|
+
<span class='ivar'>@prefix</span> <span class='op'>=</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>elephas-</span><span class='embexpr_beg'>#{</span><span class='op'>::</span><span class='const'>Elephas</span><span class='op'>::</span><span class='const'>Version</span><span class='op'>::</span><span class='const'>STRING</span><span class='rbrace'>}</span><span class='tstring_content'>-cache</span><span class='tstring_end'>"</span></span>
|
362
410
|
<span class='kw'>end</span></pre>
|
363
411
|
</td>
|
364
412
|
</tr>
|
365
413
|
</table>
|
366
414
|
</div>
|
415
|
+
|
416
|
+
</div>
|
417
|
+
|
418
|
+
<div id="instance_attr_details" class="attr_details">
|
419
|
+
<h2>Instance Attribute Details</h2>
|
367
420
|
|
421
|
+
|
422
|
+
<span id="backend=-instance_method"></span>
|
423
|
+
<div class="method_details first">
|
424
|
+
<h3 class="signature first" id="backend-instance_method">
|
425
|
+
|
426
|
+
- (<tt>Backend</tt>) <strong>backend</strong>
|
427
|
+
|
428
|
+
|
429
|
+
|
430
|
+
|
431
|
+
|
432
|
+
</h3><div class="docstring">
|
433
|
+
<div class="discussion">
|
434
|
+
<p>The backend used for the caching.</p>
|
435
|
+
|
436
|
+
|
368
437
|
</div>
|
438
|
+
</div>
|
439
|
+
<div class="tags">
|
440
|
+
|
441
|
+
<p class="tag_title">Returns:</p>
|
442
|
+
<ul class="return">
|
443
|
+
|
444
|
+
<li>
|
445
|
+
|
446
|
+
|
447
|
+
<span class='type'>(<tt>Backend</tt>)</span>
|
448
|
+
|
449
|
+
|
450
|
+
|
451
|
+
—
|
452
|
+
<div class='inline'><p>The backend used for the caching.</p>
|
453
|
+
</div>
|
454
|
+
|
455
|
+
</li>
|
456
|
+
|
457
|
+
</ul>
|
369
458
|
|
459
|
+
</div><table class="source_code">
|
460
|
+
<tr>
|
461
|
+
<td>
|
462
|
+
<pre class="lines">
|
370
463
|
|
371
|
-
<div id="class_method_details" class="method_details_list">
|
372
|
-
<h2>Class Method Details</h2>
|
373
464
|
|
465
|
+
14
|
466
|
+
15
|
467
|
+
16
|
468
|
+
17
|
469
|
+
18
|
470
|
+
19
|
471
|
+
20
|
472
|
+
21
|
473
|
+
22
|
474
|
+
23
|
475
|
+
24
|
476
|
+
25
|
477
|
+
26
|
478
|
+
27
|
479
|
+
28
|
480
|
+
29
|
481
|
+
30
|
482
|
+
31
|
483
|
+
32
|
484
|
+
33
|
485
|
+
34
|
486
|
+
35
|
487
|
+
36
|
488
|
+
37
|
489
|
+
38
|
490
|
+
39
|
491
|
+
40
|
492
|
+
41
|
493
|
+
42
|
494
|
+
43
|
495
|
+
44
|
496
|
+
45
|
497
|
+
46
|
498
|
+
47
|
499
|
+
48
|
500
|
+
49
|
501
|
+
50
|
502
|
+
51
|
503
|
+
52
|
504
|
+
53
|
505
|
+
54
|
506
|
+
55
|
507
|
+
56
|
508
|
+
57
|
509
|
+
58
|
510
|
+
59
|
511
|
+
60
|
512
|
+
61
|
513
|
+
62
|
514
|
+
63
|
515
|
+
64
|
516
|
+
65
|
517
|
+
66
|
518
|
+
67
|
519
|
+
68
|
520
|
+
69
|
521
|
+
70
|
522
|
+
71
|
523
|
+
72
|
524
|
+
73
|
525
|
+
74
|
526
|
+
75
|
527
|
+
76
|
528
|
+
77
|
529
|
+
78
|
530
|
+
79
|
531
|
+
80
|
532
|
+
81
|
533
|
+
82
|
534
|
+
83
|
535
|
+
84
|
536
|
+
85
|
537
|
+
86
|
538
|
+
87
|
539
|
+
88
|
540
|
+
89
|
541
|
+
90
|
542
|
+
91
|
543
|
+
92
|
544
|
+
93
|
545
|
+
94
|
546
|
+
95
|
547
|
+
96
|
548
|
+
97
|
549
|
+
98
|
550
|
+
99
|
551
|
+
100
|
552
|
+
101
|
553
|
+
102
|
554
|
+
103
|
555
|
+
104
|
556
|
+
105
|
557
|
+
106
|
558
|
+
107
|
559
|
+
108
|
560
|
+
109
|
561
|
+
110
|
562
|
+
111
|
563
|
+
112
|
564
|
+
113
|
565
|
+
114
|
566
|
+
115
|
567
|
+
116
|
568
|
+
117
|
569
|
+
118
|
570
|
+
119
|
571
|
+
120
|
572
|
+
121
|
573
|
+
122
|
574
|
+
123
|
575
|
+
124
|
576
|
+
125
|
577
|
+
126
|
578
|
+
127
|
579
|
+
128
|
580
|
+
129
|
581
|
+
130
|
582
|
+
131
|
583
|
+
132
|
584
|
+
133
|
585
|
+
134
|
586
|
+
135
|
587
|
+
136
|
588
|
+
137
|
589
|
+
138
|
590
|
+
139
|
591
|
+
140
|
592
|
+
141
|
593
|
+
142
|
594
|
+
143
|
595
|
+
144
|
596
|
+
145
|
597
|
+
146
|
598
|
+
147
|
599
|
+
148
|
600
|
+
149
|
601
|
+
150
|
602
|
+
151
|
603
|
+
152
|
604
|
+
153
|
605
|
+
154
|
606
|
+
155
|
607
|
+
156
|
608
|
+
157
|
609
|
+
158
|
610
|
+
159
|
611
|
+
160
|
612
|
+
161
|
613
|
+
162
|
614
|
+
163
|
615
|
+
164
|
616
|
+
165
|
617
|
+
166
|
618
|
+
167
|
619
|
+
168
|
620
|
+
169
|
621
|
+
170</pre>
|
622
|
+
</td>
|
623
|
+
<td>
|
624
|
+
<pre class="code"><span class="info file"># File 'lib/elephas/cache.rb', line 14</span>
|
625
|
+
|
626
|
+
<span class='kw'>class</span> <span class='const'>Cache</span>
|
627
|
+
<span class='id identifier rubyid_attr_accessor'>attr_accessor</span> <span class='symbol'>:backend</span>
|
628
|
+
<span class='id identifier rubyid_attr_accessor'>attr_accessor</span> <span class='symbol'>:prefix</span>
|
629
|
+
|
630
|
+
<span class='comment'># Initialize the cache.
|
631
|
+
</span> <span class='comment'>#
|
632
|
+
</span> <span class='comment'># @param backend [Backends::Base] The backend to use. By default uses an Hash backend.
|
633
|
+
</span> <span class='kw'>def</span> <span class='id identifier rubyid_initialize'>initialize</span><span class='lparen'>(</span><span class='id identifier rubyid_backend'>backend</span> <span class='op'>=</span> <span class='kw'>nil</span><span class='rparen'>)</span>
|
634
|
+
<span class='ivar'>@backend</span> <span class='op'>=</span> <span class='id identifier rubyid_backend'>backend</span> <span class='op'>||</span> <span class='const'>Elephas</span><span class='op'>::</span><span class='const'>Backends</span><span class='op'>::</span><span class='const'>Hash</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span>
|
635
|
+
<span class='ivar'>@prefix</span> <span class='op'>=</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>elephas-</span><span class='embexpr_beg'>#{</span><span class='op'>::</span><span class='const'>Elephas</span><span class='op'>::</span><span class='const'>Version</span><span class='op'>::</span><span class='const'>STRING</span><span class='rbrace'>}</span><span class='tstring_content'>-cache</span><span class='tstring_end'>"</span></span>
|
636
|
+
<span class='kw'>end</span>
|
637
|
+
|
638
|
+
<span class='comment'># This is the main method of the framework.
|
639
|
+
</span> <span class='comment'>#
|
640
|
+
</span> <span class='comment'># It tries reading a key from the cache.
|
641
|
+
</span> <span class='comment'>#
|
642
|
+
</span> <span class='comment'># 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.
|
643
|
+
</span> <span class='comment'>#
|
644
|
+
</span> <span class='comment'># ```ruby
|
645
|
+
</span> <span class='comment'># cache = Elephas::Cache.new(Elephas::Backends::Hash.new)
|
646
|
+
</span> <span class='comment'>#
|
647
|
+
</span> <span class='comment'># value = cache.use("KEY") do |options|
|
648
|
+
</span> <span class='comment'># "VALUE"
|
649
|
+
</span> <span class='comment'># end
|
650
|
+
</span> <span class='comment'>#
|
651
|
+
</span> <span class='comment'># value
|
652
|
+
</span> <span class='comment'># # => "VALUE"
|
653
|
+
</span> <span class='comment'>#
|
654
|
+
</span> <span class='comment'># value = cache.use("KEY") do |options|
|
655
|
+
</span> <span class='comment'># "ANOTHER VALUE"
|
656
|
+
</span> <span class='comment'># end
|
657
|
+
</span> <span class='comment'>#
|
658
|
+
</span> <span class='comment'># value
|
659
|
+
</span> <span class='comment'># # => "VALUE"
|
660
|
+
</span> <span class='comment'># ```
|
661
|
+
</span> <span class='comment'>#
|
662
|
+
</span> <span class='comment'># @param key [String] The key to lookup.
|
663
|
+
</span> <span class='comment'># @param options [Hash] A list of options for managing this key.
|
664
|
+
</span> <span class='comment'># @param block [Proc] An optional block to run to compute the value for the key if nothing is found.
|
665
|
+
</span> <span class='comment'># @return [Object|Entry] The found or newly-set value associated to the key.
|
666
|
+
</span> <span class='comment'># @see .setup_options
|
667
|
+
</span> <span class='kw'>def</span> <span class='id identifier rubyid_use'>use</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='id identifier rubyid_options'>options</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span><span class='comma'>,</span> <span class='op'>&</span><span class='id identifier rubyid_block'>block</span><span class='rparen'>)</span>
|
668
|
+
<span class='id identifier rubyid_rv'>rv</span> <span class='op'>=</span> <span class='kw'>nil</span>
|
669
|
+
|
670
|
+
<span class='comment'># Get options
|
671
|
+
</span> <span class='id identifier rubyid_options'>options</span> <span class='op'>=</span> <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_setup_options'>setup_options</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span><span class='comma'>,</span> <span class='id identifier rubyid_key'>key</span><span class='rparen'>)</span>
|
672
|
+
|
673
|
+
<span class='comment'># Check if the storage has the value (if we don't have to skip the cache)
|
674
|
+
</span> <span class='id identifier rubyid_rv'>rv</span> <span class='op'>=</span> <span class='id identifier rubyid_choose_backend'>choose_backend</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_read'>read</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:hash</span><span class='rbracket'>]</span><span class='rparen'>)</span> <span class='kw'>if</span> <span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:force</span><span class='rbracket'>]</span> <span class='op'>==</span> <span class='kw'>false</span> <span class='op'>&&</span> <span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:ttl</span><span class='rbracket'>]</span> <span class='op'>></span> <span class='int'>0</span>
|
675
|
+
<span class='id identifier rubyid_rv'>rv</span> <span class='op'>=</span> <span class='id identifier rubyid_compute_value'>compute_value</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span><span class='comma'>,</span> <span class='op'>&</span><span class='id identifier rubyid_block'>block</span><span class='rparen'>)</span> <span class='kw'>if</span> <span class='id identifier rubyid_rv'>rv</span><span class='period'>.</span><span class='id identifier rubyid_nil?'>nil?</span> <span class='op'>&&</span> <span class='id identifier rubyid_block'>block</span> <span class='comment'># Try to compute the value from the block
|
676
|
+
</span>
|
677
|
+
<span class='comment'># Return value
|
678
|
+
</span> <span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:as_entry</span><span class='rbracket'>]</span> <span class='op'>?</span> <span class='id identifier rubyid_rv'>rv</span> <span class='op'>:</span> <span class='id identifier rubyid_rv'>rv</span><span class='period'>.</span><span class='id identifier rubyid_value'>value</span><span class='period'>.</span><span class='id identifier rubyid_dup'>dup</span>
|
679
|
+
<span class='kw'>end</span>
|
680
|
+
|
681
|
+
<span class='comment'># Reads a value from the cache.
|
682
|
+
</span> <span class='comment'>#
|
683
|
+
</span> <span class='comment'># @param key [String] The key to lookup.
|
684
|
+
</span> <span class='comment'># @param backend [Backends::Base|NilClass] The backend to use. Defaults to the current backend.
|
685
|
+
</span> <span class='comment'># @return [Object|NilClass] The read value or `nil`.
|
686
|
+
</span> <span class='kw'>def</span> <span class='id identifier rubyid_read'>read</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='id identifier rubyid_backend'>backend</span> <span class='op'>=</span> <span class='kw'>nil</span><span class='rparen'>)</span>
|
687
|
+
<span class='id identifier rubyid_choose_backend'>choose_backend</span><span class='lparen'>(</span><span class='lbrace'>{</span><span class='label'>backend:</span> <span class='id identifier rubyid_backend'>backend</span><span class='rbrace'>}</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_read'>read</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='rparen'>)</span>
|
688
|
+
<span class='kw'>end</span>
|
689
|
+
|
690
|
+
<span class='comment'># Writes a value to the cache.
|
691
|
+
</span> <span class='comment'>#
|
692
|
+
</span> <span class='comment'># @param key [String] The key to associate the value with.
|
693
|
+
</span> <span class='comment'># @param value [Object] The value to write. Setting a value to `nil` **doesn't** mean *deleting* the value.
|
694
|
+
</span> <span class='comment'># @param options [Hash] A list of options for writing.
|
695
|
+
</span> <span class='comment'># @see .setup_options
|
696
|
+
</span> <span class='comment'># @return [Object] The value itself.
|
697
|
+
</span> <span class='kw'>def</span> <span class='id identifier rubyid_write'>write</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='id identifier rubyid_value'>value</span><span class='comma'>,</span> <span class='id identifier rubyid_options'>options</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span><span class='rparen'>)</span>
|
698
|
+
<span class='id identifier rubyid_choose_backend'>choose_backend</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_write'>write</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='id identifier rubyid_value'>value</span><span class='comma'>,</span> <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_setup_options'>setup_options</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span><span class='comma'>,</span> <span class='id identifier rubyid_key'>key</span><span class='rparen'>)</span><span class='rparen'>)</span>
|
699
|
+
<span class='kw'>end</span>
|
700
|
+
|
701
|
+
<span class='comment'># Deletes a value from the cache.
|
702
|
+
</span> <span class='comment'>#
|
703
|
+
</span> <span class='comment'># @param key [String] The key to delete.
|
704
|
+
</span> <span class='comment'># @param backend [Backends::Base|NilClass] The backend to use. Defaults to the current backend.
|
705
|
+
</span> <span class='comment'># @return [Boolean] `true` if the key was in the cache, `false` otherwise.
|
706
|
+
</span> <span class='kw'>def</span> <span class='id identifier rubyid_delete'>delete</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='id identifier rubyid_backend'>backend</span> <span class='op'>=</span> <span class='kw'>nil</span><span class='rparen'>)</span>
|
707
|
+
<span class='id identifier rubyid_choose_backend'>choose_backend</span><span class='lparen'>(</span><span class='lbrace'>{</span><span class='label'>backend:</span> <span class='id identifier rubyid_backend'>backend</span><span class='rbrace'>}</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_delete'>delete</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='rparen'>)</span>
|
708
|
+
<span class='kw'>end</span>
|
709
|
+
|
710
|
+
<span class='comment'># Checks if a key exists in the cache.
|
711
|
+
</span> <span class='comment'>#
|
712
|
+
</span> <span class='comment'># @param key [String] The key to lookup.
|
713
|
+
</span> <span class='comment'># @param backend [Backends::Base|NilClass] The backend to use. Defaults to the current backend.
|
714
|
+
</span> <span class='comment'># @return [Boolean] `true` if the key is in the cache, `false` otherwise.
|
715
|
+
</span> <span class='kw'>def</span> <span class='id identifier rubyid_exists?'>exists?</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='id identifier rubyid_backend'>backend</span> <span class='op'>=</span> <span class='kw'>nil</span><span class='rparen'>)</span>
|
716
|
+
<span class='id identifier rubyid_choose_backend'>choose_backend</span><span class='lparen'>(</span><span class='lbrace'>{</span><span class='label'>backend:</span> <span class='id identifier rubyid_backend'>backend</span><span class='rbrace'>}</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_exists?'>exists?</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='rparen'>)</span>
|
717
|
+
<span class='kw'>end</span>
|
718
|
+
|
719
|
+
<span class='comment'># Setups options for use into the framework.
|
720
|
+
</span> <span class='comment'># Valid options are:
|
721
|
+
</span> <span class='comment'>#
|
722
|
+
</span> <span class='comment'># * **: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.
|
723
|
+
</span> <span class='comment'># * **:force**: Setting it to `true` will always skip the cache.
|
724
|
+
</span> <span class='comment'># * **:key**: The key associated to this value. **You should never set this option directly.**
|
725
|
+
</span> <span class='comment'># * **:prefix**: The prefix used in cache. This is used to avoid conflicts with other caching frameworks.
|
726
|
+
</span> <span class='comment'># * **:complete_key**: The complete key used for computing the hash. By default is concatenation of `:key` and `:prefix` options.
|
727
|
+
</span> <span class='comment'># * **:hash**: The hash used to store the key in the cache. Should be unique
|
728
|
+
</span> <span class='comment'># * **:as_entry**: In `Elephas::Cache.use`, setting this to `true` will return the entire `Entry` object rather than the value only.
|
729
|
+
</span> <span class='comment'>#
|
730
|
+
</span> <span class='comment'># @param options [Object] An initial setup.
|
731
|
+
</span> <span class='comment'># @param key [String] The key to associate to this options.
|
732
|
+
</span> <span class='comment'># @return [Hash] An options hash.
|
733
|
+
</span> <span class='kw'>def</span> <span class='id identifier rubyid_setup_options'>setup_options</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span><span class='comma'>,</span> <span class='id identifier rubyid_key'>key</span><span class='rparen'>)</span>
|
734
|
+
<span class='id identifier rubyid_options'>options</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span> <span class='kw'>if</span> <span class='op'>!</span><span class='id identifier rubyid_options'>options</span><span class='period'>.</span><span class='id identifier rubyid_is_a?'>is_a?</span><span class='lparen'>(</span><span class='op'>::</span><span class='const'>Hash</span><span class='rparen'>)</span>
|
735
|
+
<span class='id identifier rubyid_options'>options</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='label'>ttl:</span> <span class='int'>1</span><span class='period'>.</span><span class='id identifier rubyid_hour'>hour</span> <span class='op'>*</span> <span class='int'>1000</span><span class='comma'>,</span> <span class='label'>force:</span> <span class='kw'>false</span><span class='comma'>,</span> <span class='label'>as_entry:</span> <span class='kw'>false</span><span class='rbrace'>}</span><span class='period'>.</span><span class='id identifier rubyid_merge'>merge</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span><span class='rparen'>)</span>
|
736
|
+
|
737
|
+
<span class='comment'># Sanitize options.
|
738
|
+
</span> <span class='id identifier rubyid_options'>options</span> <span class='op'>=</span> <span class='id identifier rubyid_sanitize_options'>sanitize_options</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span><span class='comma'>,</span> <span class='id identifier rubyid_key'>key</span><span class='rparen'>)</span>
|
739
|
+
|
740
|
+
<span class='comment'># Wrap the final key to ensure we don't have colliding namespaces.
|
741
|
+
</span> <span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:complete_key</span><span class='rbracket'>]</span> <span class='op'>||=</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:prefix</span><span class='rbracket'>]</span><span class='rbrace'>}</span><span class='tstring_content'>[</span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:key</span><span class='rbracket'>]</span><span class='rbrace'>}</span><span class='tstring_content'>]</span><span class='tstring_end'>"</span></span>
|
742
|
+
|
743
|
+
<span class='comment'># Compute the hash key used for referencing this value.
|
744
|
+
</span> <span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:hash</span><span class='rbracket'>]</span> <span class='op'>||=</span> <span class='op'>::</span><span class='const'>Elephas</span><span class='op'>::</span><span class='const'>Entry</span><span class='period'>.</span><span class='id identifier rubyid_hashify_key'>hashify_key</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:complete_key</span><span class='rbracket'>]</span><span class='rparen'>)</span>
|
745
|
+
|
746
|
+
<span class='id identifier rubyid_options'>options</span>
|
747
|
+
<span class='kw'>end</span>
|
748
|
+
|
749
|
+
<span class='id identifier rubyid_private'>private</span>
|
750
|
+
<span class='comment'># Computes a new value and saves it to the cache.
|
751
|
+
</span> <span class='comment'>#
|
752
|
+
</span> <span class='comment'># @param options [Hash] A list of options for managing the value.
|
753
|
+
</span> <span class='comment'># @param block [Proc] The block to run to compute the value.
|
754
|
+
</span> <span class='comment'># @return [Object|Entry] The new value.
|
755
|
+
</span> <span class='kw'>def</span> <span class='id identifier rubyid_compute_value'>compute_value</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span><span class='comma'>,</span> <span class='op'>&</span><span class='id identifier rubyid_block'>block</span><span class='rparen'>)</span>
|
756
|
+
<span class='id identifier rubyid_rv'>rv</span> <span class='op'>=</span> <span class='id identifier rubyid_block'>block</span><span class='period'>.</span><span class='id identifier rubyid_call'>call</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span><span class='rparen'>)</span>
|
757
|
+
<span class='id identifier rubyid_rv'>rv</span> <span class='op'>=</span> <span class='op'>::</span><span class='const'>Elephas</span><span class='op'>::</span><span class='const'>Entry</span><span class='period'>.</span><span class='id identifier rubyid_ensure'>ensure</span><span class='lparen'>(</span><span class='id identifier rubyid_rv'>rv</span><span class='comma'>,</span> <span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:complete_key</span><span class='rbracket'>]</span><span class='comma'>,</span> <span class='id identifier rubyid_options'>options</span><span class='rparen'>)</span> <span class='comment'># Make sure is an entry
|
758
|
+
</span> <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_write'>write</span><span class='lparen'>(</span><span class='id identifier rubyid_rv'>rv</span><span class='period'>.</span><span class='id identifier rubyid_hash'>hash</span><span class='comma'>,</span> <span class='id identifier rubyid_rv'>rv</span><span class='comma'>,</span> <span class='id identifier rubyid_options'>options</span><span class='rparen'>)</span> <span class='kw'>if</span> <span class='op'>!</span><span class='id identifier rubyid_rv'>rv</span><span class='period'>.</span><span class='id identifier rubyid_value'>value</span><span class='period'>.</span><span class='id identifier rubyid_nil?'>nil?</span> <span class='op'>&&</span> <span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:ttl</span><span class='rbracket'>]</span> <span class='op'>></span> <span class='int'>0</span> <span class='comment'># We have a value and we have to store it
|
759
|
+
</span> <span class='id identifier rubyid_rv'>rv</span>
|
760
|
+
<span class='kw'>end</span>
|
761
|
+
|
762
|
+
<span class='comment'># Sanitizes options for safe usage.
|
763
|
+
</span> <span class='comment'>#
|
764
|
+
</span> <span class='comment'># @param options [Object] An initial setup.
|
765
|
+
</span> <span class='comment'># @param key [String] The key to associate to this options.
|
766
|
+
</span> <span class='comment'># @return [Hash] An options hash.
|
767
|
+
</span> <span class='kw'>def</span> <span class='id identifier rubyid_sanitize_options'>sanitize_options</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span><span class='comma'>,</span> <span class='id identifier rubyid_key'>key</span><span class='rparen'>)</span>
|
768
|
+
<span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:key</span><span class='rbracket'>]</span> <span class='op'>||=</span> <span class='id identifier rubyid_key'>key</span><span class='period'>.</span><span class='id identifier rubyid_ensure_string'>ensure_string</span>
|
769
|
+
<span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:ttl</span><span class='rbracket'>]</span> <span class='op'>==</span> <span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:ttl</span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_blank?'>blank?</span> <span class='op'>?</span> <span class='int'>1</span><span class='period'>.</span><span class='id identifier rubyid_hour'>hour</span> <span class='op'>*</span> <span class='int'>1000</span> <span class='op'>:</span> <span class='lbracket'>[</span><span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:ttl</span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_to_integer'>to_integer</span><span class='comma'>,</span> <span class='int'>0</span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_max'>max</span>
|
770
|
+
<span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:force</span><span class='rbracket'>]</span> <span class='op'>=</span> <span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:force</span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_to_boolean'>to_boolean</span>
|
771
|
+
<span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:prefix</span><span class='rbracket'>]</span> <span class='op'>=</span> <span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:prefix</span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_present?'>present?</span> <span class='op'>?</span> <span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:prefix</span><span class='rbracket'>]</span> <span class='op'>:</span> <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_prefix'>prefix</span>
|
772
|
+
<span class='id identifier rubyid_options'>options</span>
|
773
|
+
<span class='kw'>end</span>
|
774
|
+
|
775
|
+
<span class='comment'># Choose a backend to use.
|
776
|
+
</span> <span class='comment'>#
|
777
|
+
</span> <span class='comment'># @param options [Backends::Base|Hash] The backend to use. Defaults to the current backend.
|
778
|
+
</span> <span class='kw'>def</span> <span class='id identifier rubyid_choose_backend'>choose_backend</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span><span class='rparen'>)</span>
|
779
|
+
<span class='id identifier rubyid_backend'>backend</span> <span class='op'>=</span> <span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span><span class='period'>.</span><span class='id identifier rubyid_is_a?'>is_a?</span><span class='lparen'>(</span><span class='const'>Hash</span><span class='rparen'>)</span> <span class='op'>?</span> <span class='id identifier rubyid_options'>options</span><span class='period'>.</span><span class='id identifier rubyid_symbolize_keys'>symbolize_keys</span><span class='lbracket'>[</span><span class='symbol'>:backend</span><span class='rbracket'>]</span> <span class='op'>:</span> <span class='id identifier rubyid_options'>options</span><span class='rparen'>)</span>
|
780
|
+
<span class='id identifier rubyid_backend'>backend</span><span class='period'>.</span><span class='id identifier rubyid_is_a?'>is_a?</span><span class='lparen'>(</span><span class='const'>Elephas</span><span class='op'>::</span><span class='const'>Backends</span><span class='op'>::</span><span class='const'>Base</span><span class='rparen'>)</span> <span class='op'>?</span> <span class='id identifier rubyid_backend'>backend</span> <span class='op'>:</span> <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_backend'>backend</span>
|
781
|
+
<span class='kw'>end</span>
|
782
|
+
<span class='kw'>end</span></pre>
|
783
|
+
</td>
|
784
|
+
</tr>
|
785
|
+
</table>
|
786
|
+
</div>
|
374
787
|
|
375
|
-
|
376
|
-
|
788
|
+
|
789
|
+
<span id="prefix=-instance_method"></span>
|
790
|
+
<div class="method_details ">
|
791
|
+
<h3 class="signature " id="prefix-instance_method">
|
377
792
|
|
378
|
-
|
793
|
+
- (<tt>String</tt>) <strong>prefix</strong>
|
379
794
|
|
380
795
|
|
381
796
|
|
@@ -383,7 +798,7 @@
|
|
383
798
|
|
384
799
|
</h3><div class="docstring">
|
385
800
|
<div class="discussion">
|
386
|
-
<p>
|
801
|
+
<p>The default prefix for cache entries.</p>
|
387
802
|
|
388
803
|
|
389
804
|
</div>
|
@@ -414,25 +829,340 @@
|
|
414
829
|
<pre class="lines">
|
415
830
|
|
416
831
|
|
832
|
+
14
|
833
|
+
15
|
834
|
+
16
|
835
|
+
17
|
836
|
+
18
|
837
|
+
19
|
838
|
+
20
|
839
|
+
21
|
840
|
+
22
|
841
|
+
23
|
842
|
+
24
|
843
|
+
25
|
844
|
+
26
|
845
|
+
27
|
846
|
+
28
|
847
|
+
29
|
848
|
+
30
|
849
|
+
31
|
850
|
+
32
|
851
|
+
33
|
852
|
+
34
|
853
|
+
35
|
854
|
+
36
|
855
|
+
37
|
856
|
+
38
|
857
|
+
39
|
858
|
+
40
|
859
|
+
41
|
860
|
+
42
|
861
|
+
43
|
862
|
+
44
|
863
|
+
45
|
864
|
+
46
|
865
|
+
47
|
866
|
+
48
|
867
|
+
49
|
868
|
+
50
|
869
|
+
51
|
870
|
+
52
|
871
|
+
53
|
872
|
+
54
|
873
|
+
55
|
874
|
+
56
|
875
|
+
57
|
876
|
+
58
|
877
|
+
59
|
878
|
+
60
|
879
|
+
61
|
880
|
+
62
|
881
|
+
63
|
882
|
+
64
|
883
|
+
65
|
884
|
+
66
|
885
|
+
67
|
886
|
+
68
|
887
|
+
69
|
888
|
+
70
|
889
|
+
71
|
890
|
+
72
|
891
|
+
73
|
892
|
+
74
|
893
|
+
75
|
894
|
+
76
|
895
|
+
77
|
896
|
+
78
|
897
|
+
79
|
898
|
+
80
|
899
|
+
81
|
900
|
+
82
|
901
|
+
83
|
902
|
+
84
|
903
|
+
85
|
904
|
+
86
|
905
|
+
87
|
906
|
+
88
|
907
|
+
89
|
908
|
+
90
|
909
|
+
91
|
910
|
+
92
|
911
|
+
93
|
912
|
+
94
|
417
913
|
95
|
418
914
|
96
|
419
|
-
97
|
915
|
+
97
|
916
|
+
98
|
917
|
+
99
|
918
|
+
100
|
919
|
+
101
|
920
|
+
102
|
921
|
+
103
|
922
|
+
104
|
923
|
+
105
|
924
|
+
106
|
925
|
+
107
|
926
|
+
108
|
927
|
+
109
|
928
|
+
110
|
929
|
+
111
|
930
|
+
112
|
931
|
+
113
|
932
|
+
114
|
933
|
+
115
|
934
|
+
116
|
935
|
+
117
|
936
|
+
118
|
937
|
+
119
|
938
|
+
120
|
939
|
+
121
|
940
|
+
122
|
941
|
+
123
|
942
|
+
124
|
943
|
+
125
|
944
|
+
126
|
945
|
+
127
|
946
|
+
128
|
947
|
+
129
|
948
|
+
130
|
949
|
+
131
|
950
|
+
132
|
951
|
+
133
|
952
|
+
134
|
953
|
+
135
|
954
|
+
136
|
955
|
+
137
|
956
|
+
138
|
957
|
+
139
|
958
|
+
140
|
959
|
+
141
|
960
|
+
142
|
961
|
+
143
|
962
|
+
144
|
963
|
+
145
|
964
|
+
146
|
965
|
+
147
|
966
|
+
148
|
967
|
+
149
|
968
|
+
150
|
969
|
+
151
|
970
|
+
152
|
971
|
+
153
|
972
|
+
154
|
973
|
+
155
|
974
|
+
156
|
975
|
+
157
|
976
|
+
158
|
977
|
+
159
|
978
|
+
160
|
979
|
+
161
|
980
|
+
162
|
981
|
+
163
|
982
|
+
164
|
983
|
+
165
|
984
|
+
166
|
985
|
+
167
|
986
|
+
168
|
987
|
+
169
|
988
|
+
170</pre>
|
420
989
|
</td>
|
421
990
|
<td>
|
422
|
-
<pre class="code"><span class="info file"># File 'lib/elephas/cache.rb', line
|
423
|
-
|
424
|
-
<span class='kw'>
|
425
|
-
<span class='
|
991
|
+
<pre class="code"><span class="info file"># File 'lib/elephas/cache.rb', line 14</span>
|
992
|
+
|
993
|
+
<span class='kw'>class</span> <span class='const'>Cache</span>
|
994
|
+
<span class='id identifier rubyid_attr_accessor'>attr_accessor</span> <span class='symbol'>:backend</span>
|
995
|
+
<span class='id identifier rubyid_attr_accessor'>attr_accessor</span> <span class='symbol'>:prefix</span>
|
996
|
+
|
997
|
+
<span class='comment'># Initialize the cache.
|
998
|
+
</span> <span class='comment'>#
|
999
|
+
</span> <span class='comment'># @param backend [Backends::Base] The backend to use. By default uses an Hash backend.
|
1000
|
+
</span> <span class='kw'>def</span> <span class='id identifier rubyid_initialize'>initialize</span><span class='lparen'>(</span><span class='id identifier rubyid_backend'>backend</span> <span class='op'>=</span> <span class='kw'>nil</span><span class='rparen'>)</span>
|
1001
|
+
<span class='ivar'>@backend</span> <span class='op'>=</span> <span class='id identifier rubyid_backend'>backend</span> <span class='op'>||</span> <span class='const'>Elephas</span><span class='op'>::</span><span class='const'>Backends</span><span class='op'>::</span><span class='const'>Hash</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span>
|
1002
|
+
<span class='ivar'>@prefix</span> <span class='op'>=</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>elephas-</span><span class='embexpr_beg'>#{</span><span class='op'>::</span><span class='const'>Elephas</span><span class='op'>::</span><span class='const'>Version</span><span class='op'>::</span><span class='const'>STRING</span><span class='rbrace'>}</span><span class='tstring_content'>-cache</span><span class='tstring_end'>"</span></span>
|
1003
|
+
<span class='kw'>end</span>
|
1004
|
+
|
1005
|
+
<span class='comment'># This is the main method of the framework.
|
1006
|
+
</span> <span class='comment'>#
|
1007
|
+
</span> <span class='comment'># It tries reading a key from the cache.
|
1008
|
+
</span> <span class='comment'>#
|
1009
|
+
</span> <span class='comment'># 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.
|
1010
|
+
</span> <span class='comment'>#
|
1011
|
+
</span> <span class='comment'># ```ruby
|
1012
|
+
</span> <span class='comment'># cache = Elephas::Cache.new(Elephas::Backends::Hash.new)
|
1013
|
+
</span> <span class='comment'>#
|
1014
|
+
</span> <span class='comment'># value = cache.use("KEY") do |options|
|
1015
|
+
</span> <span class='comment'># "VALUE"
|
1016
|
+
</span> <span class='comment'># end
|
1017
|
+
</span> <span class='comment'>#
|
1018
|
+
</span> <span class='comment'># value
|
1019
|
+
</span> <span class='comment'># # => "VALUE"
|
1020
|
+
</span> <span class='comment'>#
|
1021
|
+
</span> <span class='comment'># value = cache.use("KEY") do |options|
|
1022
|
+
</span> <span class='comment'># "ANOTHER VALUE"
|
1023
|
+
</span> <span class='comment'># end
|
1024
|
+
</span> <span class='comment'>#
|
1025
|
+
</span> <span class='comment'># value
|
1026
|
+
</span> <span class='comment'># # => "VALUE"
|
1027
|
+
</span> <span class='comment'># ```
|
1028
|
+
</span> <span class='comment'>#
|
1029
|
+
</span> <span class='comment'># @param key [String] The key to lookup.
|
1030
|
+
</span> <span class='comment'># @param options [Hash] A list of options for managing this key.
|
1031
|
+
</span> <span class='comment'># @param block [Proc] An optional block to run to compute the value for the key if nothing is found.
|
1032
|
+
</span> <span class='comment'># @return [Object|Entry] The found or newly-set value associated to the key.
|
1033
|
+
</span> <span class='comment'># @see .setup_options
|
1034
|
+
</span> <span class='kw'>def</span> <span class='id identifier rubyid_use'>use</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='id identifier rubyid_options'>options</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span><span class='comma'>,</span> <span class='op'>&</span><span class='id identifier rubyid_block'>block</span><span class='rparen'>)</span>
|
1035
|
+
<span class='id identifier rubyid_rv'>rv</span> <span class='op'>=</span> <span class='kw'>nil</span>
|
1036
|
+
|
1037
|
+
<span class='comment'># Get options
|
1038
|
+
</span> <span class='id identifier rubyid_options'>options</span> <span class='op'>=</span> <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_setup_options'>setup_options</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span><span class='comma'>,</span> <span class='id identifier rubyid_key'>key</span><span class='rparen'>)</span>
|
1039
|
+
|
1040
|
+
<span class='comment'># Check if the storage has the value (if we don't have to skip the cache)
|
1041
|
+
</span> <span class='id identifier rubyid_rv'>rv</span> <span class='op'>=</span> <span class='id identifier rubyid_choose_backend'>choose_backend</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_read'>read</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:hash</span><span class='rbracket'>]</span><span class='rparen'>)</span> <span class='kw'>if</span> <span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:force</span><span class='rbracket'>]</span> <span class='op'>==</span> <span class='kw'>false</span> <span class='op'>&&</span> <span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:ttl</span><span class='rbracket'>]</span> <span class='op'>></span> <span class='int'>0</span>
|
1042
|
+
<span class='id identifier rubyid_rv'>rv</span> <span class='op'>=</span> <span class='id identifier rubyid_compute_value'>compute_value</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span><span class='comma'>,</span> <span class='op'>&</span><span class='id identifier rubyid_block'>block</span><span class='rparen'>)</span> <span class='kw'>if</span> <span class='id identifier rubyid_rv'>rv</span><span class='period'>.</span><span class='id identifier rubyid_nil?'>nil?</span> <span class='op'>&&</span> <span class='id identifier rubyid_block'>block</span> <span class='comment'># Try to compute the value from the block
|
1043
|
+
</span>
|
1044
|
+
<span class='comment'># Return value
|
1045
|
+
</span> <span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:as_entry</span><span class='rbracket'>]</span> <span class='op'>?</span> <span class='id identifier rubyid_rv'>rv</span> <span class='op'>:</span> <span class='id identifier rubyid_rv'>rv</span><span class='period'>.</span><span class='id identifier rubyid_value'>value</span><span class='period'>.</span><span class='id identifier rubyid_dup'>dup</span>
|
1046
|
+
<span class='kw'>end</span>
|
1047
|
+
|
1048
|
+
<span class='comment'># Reads a value from the cache.
|
1049
|
+
</span> <span class='comment'>#
|
1050
|
+
</span> <span class='comment'># @param key [String] The key to lookup.
|
1051
|
+
</span> <span class='comment'># @param backend [Backends::Base|NilClass] The backend to use. Defaults to the current backend.
|
1052
|
+
</span> <span class='comment'># @return [Object|NilClass] The read value or `nil`.
|
1053
|
+
</span> <span class='kw'>def</span> <span class='id identifier rubyid_read'>read</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='id identifier rubyid_backend'>backend</span> <span class='op'>=</span> <span class='kw'>nil</span><span class='rparen'>)</span>
|
1054
|
+
<span class='id identifier rubyid_choose_backend'>choose_backend</span><span class='lparen'>(</span><span class='lbrace'>{</span><span class='label'>backend:</span> <span class='id identifier rubyid_backend'>backend</span><span class='rbrace'>}</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_read'>read</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='rparen'>)</span>
|
1055
|
+
<span class='kw'>end</span>
|
1056
|
+
|
1057
|
+
<span class='comment'># Writes a value to the cache.
|
1058
|
+
</span> <span class='comment'>#
|
1059
|
+
</span> <span class='comment'># @param key [String] The key to associate the value with.
|
1060
|
+
</span> <span class='comment'># @param value [Object] The value to write. Setting a value to `nil` **doesn't** mean *deleting* the value.
|
1061
|
+
</span> <span class='comment'># @param options [Hash] A list of options for writing.
|
1062
|
+
</span> <span class='comment'># @see .setup_options
|
1063
|
+
</span> <span class='comment'># @return [Object] The value itself.
|
1064
|
+
</span> <span class='kw'>def</span> <span class='id identifier rubyid_write'>write</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='id identifier rubyid_value'>value</span><span class='comma'>,</span> <span class='id identifier rubyid_options'>options</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span><span class='rparen'>)</span>
|
1065
|
+
<span class='id identifier rubyid_choose_backend'>choose_backend</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_write'>write</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='id identifier rubyid_value'>value</span><span class='comma'>,</span> <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_setup_options'>setup_options</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span><span class='comma'>,</span> <span class='id identifier rubyid_key'>key</span><span class='rparen'>)</span><span class='rparen'>)</span>
|
1066
|
+
<span class='kw'>end</span>
|
1067
|
+
|
1068
|
+
<span class='comment'># Deletes a value from the cache.
|
1069
|
+
</span> <span class='comment'>#
|
1070
|
+
</span> <span class='comment'># @param key [String] The key to delete.
|
1071
|
+
</span> <span class='comment'># @param backend [Backends::Base|NilClass] The backend to use. Defaults to the current backend.
|
1072
|
+
</span> <span class='comment'># @return [Boolean] `true` if the key was in the cache, `false` otherwise.
|
1073
|
+
</span> <span class='kw'>def</span> <span class='id identifier rubyid_delete'>delete</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='id identifier rubyid_backend'>backend</span> <span class='op'>=</span> <span class='kw'>nil</span><span class='rparen'>)</span>
|
1074
|
+
<span class='id identifier rubyid_choose_backend'>choose_backend</span><span class='lparen'>(</span><span class='lbrace'>{</span><span class='label'>backend:</span> <span class='id identifier rubyid_backend'>backend</span><span class='rbrace'>}</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_delete'>delete</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='rparen'>)</span>
|
1075
|
+
<span class='kw'>end</span>
|
1076
|
+
|
1077
|
+
<span class='comment'># Checks if a key exists in the cache.
|
1078
|
+
</span> <span class='comment'>#
|
1079
|
+
</span> <span class='comment'># @param key [String] The key to lookup.
|
1080
|
+
</span> <span class='comment'># @param backend [Backends::Base|NilClass] The backend to use. Defaults to the current backend.
|
1081
|
+
</span> <span class='comment'># @return [Boolean] `true` if the key is in the cache, `false` otherwise.
|
1082
|
+
</span> <span class='kw'>def</span> <span class='id identifier rubyid_exists?'>exists?</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='id identifier rubyid_backend'>backend</span> <span class='op'>=</span> <span class='kw'>nil</span><span class='rparen'>)</span>
|
1083
|
+
<span class='id identifier rubyid_choose_backend'>choose_backend</span><span class='lparen'>(</span><span class='lbrace'>{</span><span class='label'>backend:</span> <span class='id identifier rubyid_backend'>backend</span><span class='rbrace'>}</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_exists?'>exists?</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='rparen'>)</span>
|
1084
|
+
<span class='kw'>end</span>
|
1085
|
+
|
1086
|
+
<span class='comment'># Setups options for use into the framework.
|
1087
|
+
</span> <span class='comment'># Valid options are:
|
1088
|
+
</span> <span class='comment'>#
|
1089
|
+
</span> <span class='comment'># * **: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.
|
1090
|
+
</span> <span class='comment'># * **:force**: Setting it to `true` will always skip the cache.
|
1091
|
+
</span> <span class='comment'># * **:key**: The key associated to this value. **You should never set this option directly.**
|
1092
|
+
</span> <span class='comment'># * **:prefix**: The prefix used in cache. This is used to avoid conflicts with other caching frameworks.
|
1093
|
+
</span> <span class='comment'># * **:complete_key**: The complete key used for computing the hash. By default is concatenation of `:key` and `:prefix` options.
|
1094
|
+
</span> <span class='comment'># * **:hash**: The hash used to store the key in the cache. Should be unique
|
1095
|
+
</span> <span class='comment'># * **:as_entry**: In `Elephas::Cache.use`, setting this to `true` will return the entire `Entry` object rather than the value only.
|
1096
|
+
</span> <span class='comment'>#
|
1097
|
+
</span> <span class='comment'># @param options [Object] An initial setup.
|
1098
|
+
</span> <span class='comment'># @param key [String] The key to associate to this options.
|
1099
|
+
</span> <span class='comment'># @return [Hash] An options hash.
|
1100
|
+
</span> <span class='kw'>def</span> <span class='id identifier rubyid_setup_options'>setup_options</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span><span class='comma'>,</span> <span class='id identifier rubyid_key'>key</span><span class='rparen'>)</span>
|
1101
|
+
<span class='id identifier rubyid_options'>options</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span> <span class='kw'>if</span> <span class='op'>!</span><span class='id identifier rubyid_options'>options</span><span class='period'>.</span><span class='id identifier rubyid_is_a?'>is_a?</span><span class='lparen'>(</span><span class='op'>::</span><span class='const'>Hash</span><span class='rparen'>)</span>
|
1102
|
+
<span class='id identifier rubyid_options'>options</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='label'>ttl:</span> <span class='int'>1</span><span class='period'>.</span><span class='id identifier rubyid_hour'>hour</span> <span class='op'>*</span> <span class='int'>1000</span><span class='comma'>,</span> <span class='label'>force:</span> <span class='kw'>false</span><span class='comma'>,</span> <span class='label'>as_entry:</span> <span class='kw'>false</span><span class='rbrace'>}</span><span class='period'>.</span><span class='id identifier rubyid_merge'>merge</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span><span class='rparen'>)</span>
|
1103
|
+
|
1104
|
+
<span class='comment'># Sanitize options.
|
1105
|
+
</span> <span class='id identifier rubyid_options'>options</span> <span class='op'>=</span> <span class='id identifier rubyid_sanitize_options'>sanitize_options</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span><span class='comma'>,</span> <span class='id identifier rubyid_key'>key</span><span class='rparen'>)</span>
|
1106
|
+
|
1107
|
+
<span class='comment'># Wrap the final key to ensure we don't have colliding namespaces.
|
1108
|
+
</span> <span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:complete_key</span><span class='rbracket'>]</span> <span class='op'>||=</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:prefix</span><span class='rbracket'>]</span><span class='rbrace'>}</span><span class='tstring_content'>[</span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:key</span><span class='rbracket'>]</span><span class='rbrace'>}</span><span class='tstring_content'>]</span><span class='tstring_end'>"</span></span>
|
1109
|
+
|
1110
|
+
<span class='comment'># Compute the hash key used for referencing this value.
|
1111
|
+
</span> <span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:hash</span><span class='rbracket'>]</span> <span class='op'>||=</span> <span class='op'>::</span><span class='const'>Elephas</span><span class='op'>::</span><span class='const'>Entry</span><span class='period'>.</span><span class='id identifier rubyid_hashify_key'>hashify_key</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:complete_key</span><span class='rbracket'>]</span><span class='rparen'>)</span>
|
1112
|
+
|
1113
|
+
<span class='id identifier rubyid_options'>options</span>
|
1114
|
+
<span class='kw'>end</span>
|
1115
|
+
|
1116
|
+
<span class='id identifier rubyid_private'>private</span>
|
1117
|
+
<span class='comment'># Computes a new value and saves it to the cache.
|
1118
|
+
</span> <span class='comment'>#
|
1119
|
+
</span> <span class='comment'># @param options [Hash] A list of options for managing the value.
|
1120
|
+
</span> <span class='comment'># @param block [Proc] The block to run to compute the value.
|
1121
|
+
</span> <span class='comment'># @return [Object|Entry] The new value.
|
1122
|
+
</span> <span class='kw'>def</span> <span class='id identifier rubyid_compute_value'>compute_value</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span><span class='comma'>,</span> <span class='op'>&</span><span class='id identifier rubyid_block'>block</span><span class='rparen'>)</span>
|
1123
|
+
<span class='id identifier rubyid_rv'>rv</span> <span class='op'>=</span> <span class='id identifier rubyid_block'>block</span><span class='period'>.</span><span class='id identifier rubyid_call'>call</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span><span class='rparen'>)</span>
|
1124
|
+
<span class='id identifier rubyid_rv'>rv</span> <span class='op'>=</span> <span class='op'>::</span><span class='const'>Elephas</span><span class='op'>::</span><span class='const'>Entry</span><span class='period'>.</span><span class='id identifier rubyid_ensure'>ensure</span><span class='lparen'>(</span><span class='id identifier rubyid_rv'>rv</span><span class='comma'>,</span> <span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:complete_key</span><span class='rbracket'>]</span><span class='comma'>,</span> <span class='id identifier rubyid_options'>options</span><span class='rparen'>)</span> <span class='comment'># Make sure is an entry
|
1125
|
+
</span> <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_write'>write</span><span class='lparen'>(</span><span class='id identifier rubyid_rv'>rv</span><span class='period'>.</span><span class='id identifier rubyid_hash'>hash</span><span class='comma'>,</span> <span class='id identifier rubyid_rv'>rv</span><span class='comma'>,</span> <span class='id identifier rubyid_options'>options</span><span class='rparen'>)</span> <span class='kw'>if</span> <span class='op'>!</span><span class='id identifier rubyid_rv'>rv</span><span class='period'>.</span><span class='id identifier rubyid_value'>value</span><span class='period'>.</span><span class='id identifier rubyid_nil?'>nil?</span> <span class='op'>&&</span> <span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:ttl</span><span class='rbracket'>]</span> <span class='op'>></span> <span class='int'>0</span> <span class='comment'># We have a value and we have to store it
|
1126
|
+
</span> <span class='id identifier rubyid_rv'>rv</span>
|
1127
|
+
<span class='kw'>end</span>
|
1128
|
+
|
1129
|
+
<span class='comment'># Sanitizes options for safe usage.
|
1130
|
+
</span> <span class='comment'>#
|
1131
|
+
</span> <span class='comment'># @param options [Object] An initial setup.
|
1132
|
+
</span> <span class='comment'># @param key [String] The key to associate to this options.
|
1133
|
+
</span> <span class='comment'># @return [Hash] An options hash.
|
1134
|
+
</span> <span class='kw'>def</span> <span class='id identifier rubyid_sanitize_options'>sanitize_options</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span><span class='comma'>,</span> <span class='id identifier rubyid_key'>key</span><span class='rparen'>)</span>
|
1135
|
+
<span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:key</span><span class='rbracket'>]</span> <span class='op'>||=</span> <span class='id identifier rubyid_key'>key</span><span class='period'>.</span><span class='id identifier rubyid_ensure_string'>ensure_string</span>
|
1136
|
+
<span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:ttl</span><span class='rbracket'>]</span> <span class='op'>==</span> <span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:ttl</span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_blank?'>blank?</span> <span class='op'>?</span> <span class='int'>1</span><span class='period'>.</span><span class='id identifier rubyid_hour'>hour</span> <span class='op'>*</span> <span class='int'>1000</span> <span class='op'>:</span> <span class='lbracket'>[</span><span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:ttl</span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_to_integer'>to_integer</span><span class='comma'>,</span> <span class='int'>0</span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_max'>max</span>
|
1137
|
+
<span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:force</span><span class='rbracket'>]</span> <span class='op'>=</span> <span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:force</span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_to_boolean'>to_boolean</span>
|
1138
|
+
<span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:prefix</span><span class='rbracket'>]</span> <span class='op'>=</span> <span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:prefix</span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_present?'>present?</span> <span class='op'>?</span> <span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:prefix</span><span class='rbracket'>]</span> <span class='op'>:</span> <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_prefix'>prefix</span>
|
1139
|
+
<span class='id identifier rubyid_options'>options</span>
|
1140
|
+
<span class='kw'>end</span>
|
1141
|
+
|
1142
|
+
<span class='comment'># Choose a backend to use.
|
1143
|
+
</span> <span class='comment'>#
|
1144
|
+
</span> <span class='comment'># @param options [Backends::Base|Hash] The backend to use. Defaults to the current backend.
|
1145
|
+
</span> <span class='kw'>def</span> <span class='id identifier rubyid_choose_backend'>choose_backend</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span><span class='rparen'>)</span>
|
1146
|
+
<span class='id identifier rubyid_backend'>backend</span> <span class='op'>=</span> <span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span><span class='period'>.</span><span class='id identifier rubyid_is_a?'>is_a?</span><span class='lparen'>(</span><span class='const'>Hash</span><span class='rparen'>)</span> <span class='op'>?</span> <span class='id identifier rubyid_options'>options</span><span class='period'>.</span><span class='id identifier rubyid_symbolize_keys'>symbolize_keys</span><span class='lbracket'>[</span><span class='symbol'>:backend</span><span class='rbracket'>]</span> <span class='op'>:</span> <span class='id identifier rubyid_options'>options</span><span class='rparen'>)</span>
|
1147
|
+
<span class='id identifier rubyid_backend'>backend</span><span class='period'>.</span><span class='id identifier rubyid_is_a?'>is_a?</span><span class='lparen'>(</span><span class='const'>Elephas</span><span class='op'>::</span><span class='const'>Backends</span><span class='op'>::</span><span class='const'>Base</span><span class='rparen'>)</span> <span class='op'>?</span> <span class='id identifier rubyid_backend'>backend</span> <span class='op'>:</span> <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_backend'>backend</span>
|
1148
|
+
<span class='kw'>end</span>
|
426
1149
|
<span class='kw'>end</span></pre>
|
427
1150
|
</td>
|
428
1151
|
</tr>
|
429
1152
|
</table>
|
430
1153
|
</div>
|
431
1154
|
|
432
|
-
|
433
|
-
|
1155
|
+
</div>
|
1156
|
+
|
1157
|
+
|
1158
|
+
<div id="instance_method_details" class="method_details_list">
|
1159
|
+
<h2>Instance Method Details</h2>
|
1160
|
+
|
1161
|
+
|
1162
|
+
<div class="method_details first">
|
1163
|
+
<h3 class="signature first" id="delete-instance_method">
|
434
1164
|
|
435
|
-
|
1165
|
+
- (<tt>Boolean</tt>) <strong>delete</strong>(key, backend = nil)
|
436
1166
|
|
437
1167
|
|
438
1168
|
|
@@ -464,6 +1194,23 @@
|
|
464
1194
|
|
465
1195
|
</li>
|
466
1196
|
|
1197
|
+
<li>
|
1198
|
+
|
1199
|
+
<span class='name'>backend</span>
|
1200
|
+
|
1201
|
+
|
1202
|
+
<span class='type'>(<tt><span class='object_link'><a href="Backends/Base.html" title="Elephas::Backends::Base (class)">Backends::Base</a></span>|NilClass</tt>)</span>
|
1203
|
+
|
1204
|
+
|
1205
|
+
<em class="default">(defaults to: <tt>nil</tt>)</em>
|
1206
|
+
|
1207
|
+
|
1208
|
+
—
|
1209
|
+
<div class='inline'><p>The backend to use. Defaults to the current backend.</p>
|
1210
|
+
</div>
|
1211
|
+
|
1212
|
+
</li>
|
1213
|
+
|
467
1214
|
</ul>
|
468
1215
|
|
469
1216
|
<p class="tag_title">Returns:</p>
|
@@ -490,15 +1237,15 @@
|
|
490
1237
|
<pre class="lines">
|
491
1238
|
|
492
1239
|
|
493
|
-
|
494
|
-
|
495
|
-
|
1240
|
+
94
|
1241
|
+
95
|
1242
|
+
96</pre>
|
496
1243
|
</td>
|
497
1244
|
<td>
|
498
|
-
<pre class="code"><span class="info file"># File 'lib/elephas/cache.rb', line
|
1245
|
+
<pre class="code"><span class="info file"># File 'lib/elephas/cache.rb', line 94</span>
|
499
1246
|
|
500
|
-
<span class='kw'>def</span> <span class='
|
501
|
-
<span class='
|
1247
|
+
<span class='kw'>def</span> <span class='id identifier rubyid_delete'>delete</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='id identifier rubyid_backend'>backend</span> <span class='op'>=</span> <span class='kw'>nil</span><span class='rparen'>)</span>
|
1248
|
+
<span class='id identifier rubyid_choose_backend'>choose_backend</span><span class='lparen'>(</span><span class='lbrace'>{</span><span class='label'>backend:</span> <span class='id identifier rubyid_backend'>backend</span><span class='rbrace'>}</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_delete'>delete</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='rparen'>)</span>
|
502
1249
|
<span class='kw'>end</span></pre>
|
503
1250
|
</td>
|
504
1251
|
</tr>
|
@@ -506,9 +1253,9 @@
|
|
506
1253
|
</div>
|
507
1254
|
|
508
1255
|
<div class="method_details ">
|
509
|
-
<h3 class="signature " id="exists?-
|
1256
|
+
<h3 class="signature " id="exists?-instance_method">
|
510
1257
|
|
511
|
-
|
1258
|
+
- (<tt>Boolean</tt>) <strong>exists?</strong>(key, backend = nil)
|
512
1259
|
|
513
1260
|
|
514
1261
|
|
@@ -540,6 +1287,23 @@
|
|
540
1287
|
|
541
1288
|
</li>
|
542
1289
|
|
1290
|
+
<li>
|
1291
|
+
|
1292
|
+
<span class='name'>backend</span>
|
1293
|
+
|
1294
|
+
|
1295
|
+
<span class='type'>(<tt><span class='object_link'><a href="Backends/Base.html" title="Elephas::Backends::Base (class)">Backends::Base</a></span>|NilClass</tt>)</span>
|
1296
|
+
|
1297
|
+
|
1298
|
+
<em class="default">(defaults to: <tt>nil</tt>)</em>
|
1299
|
+
|
1300
|
+
|
1301
|
+
—
|
1302
|
+
<div class='inline'><p>The backend to use. Defaults to the current backend.</p>
|
1303
|
+
</div>
|
1304
|
+
|
1305
|
+
</li>
|
1306
|
+
|
543
1307
|
</ul>
|
544
1308
|
|
545
1309
|
<p class="tag_title">Returns:</p>
|
@@ -566,15 +1330,15 @@
|
|
566
1330
|
<pre class="lines">
|
567
1331
|
|
568
1332
|
|
569
|
-
|
570
|
-
|
571
|
-
|
1333
|
+
103
|
1334
|
+
104
|
1335
|
+
105</pre>
|
572
1336
|
</td>
|
573
1337
|
<td>
|
574
|
-
<pre class="code"><span class="info file"># File 'lib/elephas/cache.rb', line
|
1338
|
+
<pre class="code"><span class="info file"># File 'lib/elephas/cache.rb', line 103</span>
|
575
1339
|
|
576
|
-
<span class='kw'>def</span> <span class='
|
577
|
-
<span class='
|
1340
|
+
<span class='kw'>def</span> <span class='id identifier rubyid_exists?'>exists?</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='id identifier rubyid_backend'>backend</span> <span class='op'>=</span> <span class='kw'>nil</span><span class='rparen'>)</span>
|
1341
|
+
<span class='id identifier rubyid_choose_backend'>choose_backend</span><span class='lparen'>(</span><span class='lbrace'>{</span><span class='label'>backend:</span> <span class='id identifier rubyid_backend'>backend</span><span class='rbrace'>}</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_exists?'>exists?</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='rparen'>)</span>
|
578
1342
|
<span class='kw'>end</span></pre>
|
579
1343
|
</td>
|
580
1344
|
</tr>
|
@@ -582,9 +1346,9 @@
|
|
582
1346
|
</div>
|
583
1347
|
|
584
1348
|
<div class="method_details ">
|
585
|
-
<h3 class="signature " id="read-
|
1349
|
+
<h3 class="signature " id="read-instance_method">
|
586
1350
|
|
587
|
-
|
1351
|
+
- (<tt>Object|NilClass</tt>) <strong>read</strong>(key, backend = nil)
|
588
1352
|
|
589
1353
|
|
590
1354
|
|
@@ -616,6 +1380,23 @@
|
|
616
1380
|
|
617
1381
|
</li>
|
618
1382
|
|
1383
|
+
<li>
|
1384
|
+
|
1385
|
+
<span class='name'>backend</span>
|
1386
|
+
|
1387
|
+
|
1388
|
+
<span class='type'>(<tt><span class='object_link'><a href="Backends/Base.html" title="Elephas::Backends::Base (class)">Backends::Base</a></span>|NilClass</tt>)</span>
|
1389
|
+
|
1390
|
+
|
1391
|
+
<em class="default">(defaults to: <tt>nil</tt>)</em>
|
1392
|
+
|
1393
|
+
|
1394
|
+
—
|
1395
|
+
<div class='inline'><p>The backend to use. Defaults to the current backend.</p>
|
1396
|
+
</div>
|
1397
|
+
|
1398
|
+
</li>
|
1399
|
+
|
619
1400
|
</ul>
|
620
1401
|
|
621
1402
|
<p class="tag_title">Returns:</p>
|
@@ -642,15 +1423,15 @@
|
|
642
1423
|
<pre class="lines">
|
643
1424
|
|
644
1425
|
|
645
|
-
|
646
|
-
|
647
|
-
|
1426
|
+
74
|
1427
|
+
75
|
1428
|
+
76</pre>
|
648
1429
|
</td>
|
649
1430
|
<td>
|
650
|
-
<pre class="code"><span class="info file"># File 'lib/elephas/cache.rb', line
|
1431
|
+
<pre class="code"><span class="info file"># File 'lib/elephas/cache.rb', line 74</span>
|
651
1432
|
|
652
|
-
<span class='kw'>def</span> <span class='
|
653
|
-
<span class='
|
1433
|
+
<span class='kw'>def</span> <span class='id identifier rubyid_read'>read</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='id identifier rubyid_backend'>backend</span> <span class='op'>=</span> <span class='kw'>nil</span><span class='rparen'>)</span>
|
1434
|
+
<span class='id identifier rubyid_choose_backend'>choose_backend</span><span class='lparen'>(</span><span class='lbrace'>{</span><span class='label'>backend:</span> <span class='id identifier rubyid_backend'>backend</span><span class='rbrace'>}</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_read'>read</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='rparen'>)</span>
|
654
1435
|
<span class='kw'>end</span></pre>
|
655
1436
|
</td>
|
656
1437
|
</tr>
|
@@ -658,9 +1439,9 @@
|
|
658
1439
|
</div>
|
659
1440
|
|
660
1441
|
<div class="method_details ">
|
661
|
-
<h3 class="signature " id="setup_options-
|
1442
|
+
<h3 class="signature " id="setup_options-instance_method">
|
662
1443
|
|
663
|
-
|
1444
|
+
- (<tt>Hash</tt>) <strong>setup_options</strong>(options, key)
|
664
1445
|
|
665
1446
|
|
666
1447
|
|
@@ -744,26 +1525,26 @@ Valid options are:</p>
|
|
744
1525
|
<pre class="lines">
|
745
1526
|
|
746
1527
|
|
747
|
-
113
|
748
|
-
114
|
749
|
-
115
|
750
|
-
116
|
751
|
-
117
|
752
|
-
118
|
753
|
-
119
|
754
|
-
120
|
755
1528
|
121
|
756
1529
|
122
|
757
1530
|
123
|
758
1531
|
124
|
759
1532
|
125
|
760
1533
|
126
|
761
|
-
127
|
1534
|
+
127
|
1535
|
+
128
|
1536
|
+
129
|
1537
|
+
130
|
1538
|
+
131
|
1539
|
+
132
|
1540
|
+
133
|
1541
|
+
134
|
1542
|
+
135</pre>
|
762
1543
|
</td>
|
763
1544
|
<td>
|
764
|
-
<pre class="code"><span class="info file"># File 'lib/elephas/cache.rb', line
|
1545
|
+
<pre class="code"><span class="info file"># File 'lib/elephas/cache.rb', line 121</span>
|
765
1546
|
|
766
|
-
<span class='kw'>def</span> <span class='
|
1547
|
+
<span class='kw'>def</span> <span class='id identifier rubyid_setup_options'>setup_options</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span><span class='comma'>,</span> <span class='id identifier rubyid_key'>key</span><span class='rparen'>)</span>
|
767
1548
|
<span class='id identifier rubyid_options'>options</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span> <span class='kw'>if</span> <span class='op'>!</span><span class='id identifier rubyid_options'>options</span><span class='period'>.</span><span class='id identifier rubyid_is_a?'>is_a?</span><span class='lparen'>(</span><span class='op'>::</span><span class='const'>Hash</span><span class='rparen'>)</span>
|
768
1549
|
<span class='id identifier rubyid_options'>options</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='label'>ttl:</span> <span class='int'>1</span><span class='period'>.</span><span class='id identifier rubyid_hour'>hour</span> <span class='op'>*</span> <span class='int'>1000</span><span class='comma'>,</span> <span class='label'>force:</span> <span class='kw'>false</span><span class='comma'>,</span> <span class='label'>as_entry:</span> <span class='kw'>false</span><span class='rbrace'>}</span><span class='period'>.</span><span class='id identifier rubyid_merge'>merge</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span><span class='rparen'>)</span>
|
769
1550
|
|
@@ -784,9 +1565,9 @@ Valid options are:</p>
|
|
784
1565
|
</div>
|
785
1566
|
|
786
1567
|
<div class="method_details ">
|
787
|
-
<h3 class="signature " id="use-
|
1568
|
+
<h3 class="signature " id="use-instance_method">
|
788
1569
|
|
789
|
-
|
1570
|
+
- (<tt>Object|<span class='object_link'><a href="Entry.html" title="Elephas::Entry (class)">Entry</a></span></tt>) <strong>use</strong>(key, options = {}, &block)
|
790
1571
|
|
791
1572
|
|
792
1573
|
|
@@ -801,14 +1582,16 @@ Valid options are:</p>
|
|
801
1582
|
<p>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.</p>
|
802
1583
|
|
803
1584
|
<p>```ruby
|
804
|
-
|
1585
|
+
cache = Elephas::Cache.new(Elephas::Backends::Hash.new)</p>
|
1586
|
+
|
1587
|
+
<p>value = cache.use(“KEY”) do |options|
|
805
1588
|
“VALUE”
|
806
1589
|
end</p>
|
807
1590
|
|
808
1591
|
<p>value
|
809
1592
|
# => “VALUE”</p>
|
810
1593
|
|
811
|
-
<p>value =
|
1594
|
+
<p>value = cache.use(“KEY”) do |options|
|
812
1595
|
“ANOTHER VALUE”
|
813
1596
|
end</p>
|
814
1597
|
|
@@ -893,7 +1676,7 @@ end</p>
|
|
893
1676
|
<p class="tag_title">See Also:</p>
|
894
1677
|
<ul class="see">
|
895
1678
|
|
896
|
-
<li
|
1679
|
+
<li>setup_options</li>
|
897
1680
|
|
898
1681
|
</ul>
|
899
1682
|
|
@@ -903,31 +1686,31 @@ end</p>
|
|
903
1686
|
<pre class="lines">
|
904
1687
|
|
905
1688
|
|
906
|
-
|
907
|
-
|
908
|
-
|
909
|
-
|
910
|
-
|
911
|
-
|
912
|
-
|
913
|
-
|
914
|
-
|
915
|
-
|
916
|
-
|
917
|
-
|
918
|
-
|
1689
|
+
55
|
1690
|
+
56
|
1691
|
+
57
|
1692
|
+
58
|
1693
|
+
59
|
1694
|
+
60
|
1695
|
+
61
|
1696
|
+
62
|
1697
|
+
63
|
1698
|
+
64
|
1699
|
+
65
|
1700
|
+
66
|
1701
|
+
67</pre>
|
919
1702
|
</td>
|
920
1703
|
<td>
|
921
|
-
<pre class="code"><span class="info file"># File 'lib/elephas/cache.rb', line
|
1704
|
+
<pre class="code"><span class="info file"># File 'lib/elephas/cache.rb', line 55</span>
|
922
1705
|
|
923
|
-
<span class='kw'>def</span> <span class='
|
1706
|
+
<span class='kw'>def</span> <span class='id identifier rubyid_use'>use</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='id identifier rubyid_options'>options</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span><span class='comma'>,</span> <span class='op'>&</span><span class='id identifier rubyid_block'>block</span><span class='rparen'>)</span>
|
924
1707
|
<span class='id identifier rubyid_rv'>rv</span> <span class='op'>=</span> <span class='kw'>nil</span>
|
925
1708
|
|
926
1709
|
<span class='comment'># Get options
|
927
|
-
</span> <span class='id identifier rubyid_options'>options</span> <span class='op'>=</span> <span class='
|
1710
|
+
</span> <span class='id identifier rubyid_options'>options</span> <span class='op'>=</span> <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_setup_options'>setup_options</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span><span class='comma'>,</span> <span class='id identifier rubyid_key'>key</span><span class='rparen'>)</span>
|
928
1711
|
|
929
1712
|
<span class='comment'># Check if the storage has the value (if we don't have to skip the cache)
|
930
|
-
</span> <span class='id identifier rubyid_rv'>rv</span> <span class='op'>=</span> <span class='
|
1713
|
+
</span> <span class='id identifier rubyid_rv'>rv</span> <span class='op'>=</span> <span class='id identifier rubyid_choose_backend'>choose_backend</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_read'>read</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:hash</span><span class='rbracket'>]</span><span class='rparen'>)</span> <span class='kw'>if</span> <span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:force</span><span class='rbracket'>]</span> <span class='op'>==</span> <span class='kw'>false</span> <span class='op'>&&</span> <span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:ttl</span><span class='rbracket'>]</span> <span class='op'>></span> <span class='int'>0</span>
|
931
1714
|
<span class='id identifier rubyid_rv'>rv</span> <span class='op'>=</span> <span class='id identifier rubyid_compute_value'>compute_value</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span><span class='comma'>,</span> <span class='op'>&</span><span class='id identifier rubyid_block'>block</span><span class='rparen'>)</span> <span class='kw'>if</span> <span class='id identifier rubyid_rv'>rv</span><span class='period'>.</span><span class='id identifier rubyid_nil?'>nil?</span> <span class='op'>&&</span> <span class='id identifier rubyid_block'>block</span> <span class='comment'># Try to compute the value from the block
|
932
1715
|
</span>
|
933
1716
|
<span class='comment'># Return value
|
@@ -939,9 +1722,9 @@ end</p>
|
|
939
1722
|
</div>
|
940
1723
|
|
941
1724
|
<div class="method_details ">
|
942
|
-
<h3 class="signature " id="write-
|
1725
|
+
<h3 class="signature " id="write-instance_method">
|
943
1726
|
|
944
|
-
|
1727
|
+
- (<tt>Object</tt>) <strong>write</strong>(key, value, options = {})
|
945
1728
|
|
946
1729
|
|
947
1730
|
|
@@ -1028,7 +1811,7 @@ end</p>
|
|
1028
1811
|
<p class="tag_title">See Also:</p>
|
1029
1812
|
<ul class="see">
|
1030
1813
|
|
1031
|
-
<li
|
1814
|
+
<li>setup_options</li>
|
1032
1815
|
|
1033
1816
|
</ul>
|
1034
1817
|
|
@@ -1038,15 +1821,15 @@ end</p>
|
|
1038
1821
|
<pre class="lines">
|
1039
1822
|
|
1040
1823
|
|
1041
|
-
|
1042
|
-
|
1043
|
-
|
1824
|
+
85
|
1825
|
+
86
|
1826
|
+
87</pre>
|
1044
1827
|
</td>
|
1045
1828
|
<td>
|
1046
|
-
<pre class="code"><span class="info file"># File 'lib/elephas/cache.rb', line
|
1829
|
+
<pre class="code"><span class="info file"># File 'lib/elephas/cache.rb', line 85</span>
|
1047
1830
|
|
1048
|
-
<span class='kw'>def</span> <span class='
|
1049
|
-
<span class='
|
1831
|
+
<span class='kw'>def</span> <span class='id identifier rubyid_write'>write</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='id identifier rubyid_value'>value</span><span class='comma'>,</span> <span class='id identifier rubyid_options'>options</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span><span class='rparen'>)</span>
|
1832
|
+
<span class='id identifier rubyid_choose_backend'>choose_backend</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_write'>write</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='id identifier rubyid_value'>value</span><span class='comma'>,</span> <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_setup_options'>setup_options</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span><span class='comma'>,</span> <span class='id identifier rubyid_key'>key</span><span class='rparen'>)</span><span class='rparen'>)</span>
|
1050
1833
|
<span class='kw'>end</span></pre>
|
1051
1834
|
</td>
|
1052
1835
|
</tr>
|
@@ -1058,7 +1841,7 @@ end</p>
|
|
1058
1841
|
</div>
|
1059
1842
|
|
1060
1843
|
<div id="footer">
|
1061
|
-
Generated on
|
1844
|
+
Generated on Sun Mar 24 12:12:02 2013 by
|
1062
1845
|
<a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
1063
1846
|
0.8.5.2 (ruby-1.9.3).
|
1064
1847
|
</div>
|