elephas 1.1.3 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.travis.yml +1 -1
- data/Gemfile +1 -1
- data/README.md +4 -2
- data/Rakefile +1 -1
- data/doc/Elephas/Cache.html +83 -63
- data/doc/Elephas/Entry.html +897 -59
- data/doc/Elephas/Providers/Base.html +31 -78
- data/doc/Elephas/Providers/Hash.html +137 -23
- data/doc/Elephas/Providers/RubyOnRails.html +20 -21
- data/doc/Elephas/Providers.html +3 -3
- data/doc/Elephas/Version.html +6 -6
- data/doc/Elephas.html +4 -4
- data/doc/_index.html +4 -4
- data/doc/file.README.html +8 -6
- data/doc/frames.html +1 -1
- data/doc/index.html +8 -6
- data/doc/top-level-namespace.html +3 -3
- data/elephas.gemspec +10 -8
- data/lib/elephas/cache.rb +124 -103
- data/lib/elephas/entry.rb +23 -22
- data/lib/elephas/provider.rb +13 -5
- data/lib/elephas/providers/hash.rb +14 -14
- data/lib/elephas/providers/ruby_on_rails.rb +10 -11
- data/lib/elephas/version.rb +4 -4
- data/lib/elephas.rb +1 -3
- data/locales/en.yml +9 -0
- data/locales/it.yml +9 -0
- data/spec/coverage_helper.rb +1 -1
- data/spec/elephas/cache_spec.rb +19 -19
- data/spec/elephas/entry_spec.rb +1 -1
- data/spec/elephas/provider_spec.rb +1 -1
- data/spec/elephas/providers/hash_spec.rb +3 -3
- data/spec/elephas/providers/ruby_on_rails_spec.rb +2 -2
- data/spec/spec_helper.rb +1 -1
- metadata +18 -23
- data/doc/Elephas/Exceptions.html +0 -115
data/doc/Elephas/Version.html
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
<title>
|
|
7
7
|
Module: Elephas::Version
|
|
8
8
|
|
|
9
|
-
— Documentation by YARD 0.8.
|
|
9
|
+
— Documentation by YARD 0.8.3
|
|
10
10
|
|
|
11
11
|
</title>
|
|
12
12
|
|
|
@@ -119,7 +119,7 @@
|
|
|
119
119
|
|
|
120
120
|
</div>
|
|
121
121
|
</dt>
|
|
122
|
-
<dd><pre class="code"><span class='int'>
|
|
122
|
+
<dd><pre class="code"><span class='int'>2</span></pre></dd>
|
|
123
123
|
|
|
124
124
|
<dt id="MINOR-constant" class="">MINOR =
|
|
125
125
|
<div class="docstring">
|
|
@@ -134,7 +134,7 @@
|
|
|
134
134
|
|
|
135
135
|
</div>
|
|
136
136
|
</dt>
|
|
137
|
-
<dd><pre class="code"><span class='int'>
|
|
137
|
+
<dd><pre class="code"><span class='int'>0</span></pre></dd>
|
|
138
138
|
|
|
139
139
|
<dt id="PATCH-constant" class="">PATCH =
|
|
140
140
|
<div class="docstring">
|
|
@@ -149,7 +149,7 @@
|
|
|
149
149
|
|
|
150
150
|
</div>
|
|
151
151
|
</dt>
|
|
152
|
-
<dd><pre class="code"><span class='int'>
|
|
152
|
+
<dd><pre class="code"><span class='int'>0</span></pre></dd>
|
|
153
153
|
|
|
154
154
|
<dt id="STRING-constant" class="">STRING =
|
|
155
155
|
<div class="docstring">
|
|
@@ -180,9 +180,9 @@
|
|
|
180
180
|
</div>
|
|
181
181
|
|
|
182
182
|
<div id="footer">
|
|
183
|
-
Generated on
|
|
183
|
+
Generated on Sat Feb 2 09:15:15 2013 by
|
|
184
184
|
<a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
|
185
|
-
0.8.
|
|
185
|
+
0.8.3 (ruby-1.9.3).
|
|
186
186
|
</div>
|
|
187
187
|
|
|
188
188
|
</body>
|
data/doc/Elephas.html
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
<title>
|
|
7
7
|
Module: Elephas
|
|
8
8
|
|
|
9
|
-
— Documentation by YARD 0.8.
|
|
9
|
+
— Documentation by YARD 0.8.3
|
|
10
10
|
|
|
11
11
|
</title>
|
|
12
12
|
|
|
@@ -88,7 +88,7 @@
|
|
|
88
88
|
|
|
89
89
|
<h2>Overview</h2><div class="docstring">
|
|
90
90
|
<div class="discussion">
|
|
91
|
-
<p>This file is part of the elephas gem. Copyright (C)
|
|
91
|
+
<p>This file is part of the elephas gem. Copyright (C) 2013 and above Shogun <shogun_panda@me.com>.
|
|
92
92
|
Licensed under the MIT license, which can be found at <a href="http://www.opensource.org/licenses/mit-license.php">http://www.opensource.org/licenses/mit-license.php</a>.</p>
|
|
93
93
|
|
|
94
94
|
|
|
@@ -121,9 +121,9 @@ 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
|
|
124
|
+
Generated on Sat Feb 2 09:15:15 2013 by
|
|
125
125
|
<a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
|
126
|
-
0.8.
|
|
126
|
+
0.8.3 (ruby-1.9.3).
|
|
127
127
|
</div>
|
|
128
128
|
|
|
129
129
|
</body>
|
data/doc/_index.html
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
<head>
|
|
5
5
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|
6
6
|
<title>
|
|
7
|
-
Documentation by YARD 0.8.
|
|
7
|
+
Documentation by YARD 0.8.3
|
|
8
8
|
|
|
9
9
|
</title>
|
|
10
10
|
|
|
@@ -56,7 +56,7 @@
|
|
|
56
56
|
|
|
57
57
|
<iframe id="search_frame"></iframe>
|
|
58
58
|
|
|
59
|
-
<div id="content"><h1 class="noborder title">Documentation by YARD 0.8.
|
|
59
|
+
<div id="content"><h1 class="noborder title">Documentation by YARD 0.8.3</h1>
|
|
60
60
|
<div id="listing">
|
|
61
61
|
<h1 class="alphaindex">Alphabetic Index</h1>
|
|
62
62
|
|
|
@@ -198,9 +198,9 @@
|
|
|
198
198
|
</div>
|
|
199
199
|
|
|
200
200
|
<div id="footer">
|
|
201
|
-
Generated on
|
|
201
|
+
Generated on Sat Feb 2 09:15:15 2013 by
|
|
202
202
|
<a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
|
203
|
-
0.8.
|
|
203
|
+
0.8.3 (ruby-1.9.3).
|
|
204
204
|
</div>
|
|
205
205
|
|
|
206
206
|
</body>
|
data/doc/file.README.html
CHANGED
|
@@ -2,11 +2,11 @@
|
|
|
2
2
|
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
3
3
|
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
|
4
4
|
<head>
|
|
5
|
-
<meta http-equiv="Content-Type" content="text/html; charset=
|
|
5
|
+
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII" />
|
|
6
6
|
<title>
|
|
7
7
|
File: README
|
|
8
8
|
|
|
9
|
-
— Documentation by YARD 0.8.
|
|
9
|
+
— Documentation by YARD 0.8.3
|
|
10
10
|
|
|
11
11
|
</title>
|
|
12
12
|
|
|
@@ -69,7 +69,9 @@
|
|
|
69
69
|
|
|
70
70
|
<p>A storage agnostic caching framework.</p>
|
|
71
71
|
|
|
72
|
-
<p><a href="http://
|
|
72
|
+
<p><a href="http://sw.cow.tc/elephas">http://sw.cow.tc/elephas</a></p>
|
|
73
|
+
|
|
74
|
+
<p><a href="http://rdoc.info/gems/elephas">http://rdoc.info/gems/elephas</a></p>
|
|
73
75
|
|
|
74
76
|
<h2>Usage</h2>
|
|
75
77
|
|
|
@@ -116,14 +118,14 @@
|
|
|
116
118
|
|
|
117
119
|
<h2>Copyright</h2>
|
|
118
120
|
|
|
119
|
-
<p>Copyright (C)
|
|
121
|
+
<p>Copyright (C) 2013 and above Shogun (<a href="mailto:shogun_panda@me.com">shogun_panda@me.com</a>).
|
|
120
122
|
Licensed under the MIT license, which can be found at <a href="http://www.opensource.org/licenses/mit-license.php">http://www.opensource.org/licenses/mit-license.php</a>.</p>
|
|
121
123
|
</div></div>
|
|
122
124
|
|
|
123
125
|
<div id="footer">
|
|
124
|
-
Generated on
|
|
126
|
+
Generated on Sat Feb 2 09:15:15 2013 by
|
|
125
127
|
<a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
|
126
|
-
0.8.
|
|
128
|
+
0.8.3 (ruby-1.9.3).
|
|
127
129
|
</div>
|
|
128
130
|
|
|
129
131
|
</body>
|
data/doc/frames.html
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
|
5
5
|
<head>
|
|
6
6
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
|
|
7
|
-
<title>Documentation by YARD 0.8.
|
|
7
|
+
<title>Documentation by YARD 0.8.3</title>
|
|
8
8
|
</head>
|
|
9
9
|
<script type="text/javascript" charset="utf-8">
|
|
10
10
|
window.onload = function() {
|
data/doc/index.html
CHANGED
|
@@ -2,11 +2,11 @@
|
|
|
2
2
|
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
3
3
|
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
|
4
4
|
<head>
|
|
5
|
-
<meta http-equiv="Content-Type" content="text/html; charset=
|
|
5
|
+
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII" />
|
|
6
6
|
<title>
|
|
7
7
|
File: README
|
|
8
8
|
|
|
9
|
-
— Documentation by YARD 0.8.
|
|
9
|
+
— Documentation by YARD 0.8.3
|
|
10
10
|
|
|
11
11
|
</title>
|
|
12
12
|
|
|
@@ -69,7 +69,9 @@
|
|
|
69
69
|
|
|
70
70
|
<p>A storage agnostic caching framework.</p>
|
|
71
71
|
|
|
72
|
-
<p><a href="http://
|
|
72
|
+
<p><a href="http://sw.cow.tc/elephas">http://sw.cow.tc/elephas</a></p>
|
|
73
|
+
|
|
74
|
+
<p><a href="http://rdoc.info/gems/elephas">http://rdoc.info/gems/elephas</a></p>
|
|
73
75
|
|
|
74
76
|
<h2>Usage</h2>
|
|
75
77
|
|
|
@@ -116,14 +118,14 @@
|
|
|
116
118
|
|
|
117
119
|
<h2>Copyright</h2>
|
|
118
120
|
|
|
119
|
-
<p>Copyright (C)
|
|
121
|
+
<p>Copyright (C) 2013 and above Shogun (<a href="mailto:shogun_panda@me.com">shogun_panda@me.com</a>).
|
|
120
122
|
Licensed under the MIT license, which can be found at <a href="http://www.opensource.org/licenses/mit-license.php">http://www.opensource.org/licenses/mit-license.php</a>.</p>
|
|
121
123
|
</div></div>
|
|
122
124
|
|
|
123
125
|
<div id="footer">
|
|
124
|
-
Generated on
|
|
126
|
+
Generated on Sat Feb 2 09:15:15 2013 by
|
|
125
127
|
<a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
|
126
|
-
0.8.
|
|
128
|
+
0.8.3 (ruby-1.9.3).
|
|
127
129
|
</div>
|
|
128
130
|
|
|
129
131
|
</body>
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
<title>
|
|
7
7
|
Top Level Namespace
|
|
8
8
|
|
|
9
|
-
— Documentation by YARD 0.8.
|
|
9
|
+
— Documentation by YARD 0.8.3
|
|
10
10
|
|
|
11
11
|
</title>
|
|
12
12
|
|
|
@@ -103,9 +103,9 @@
|
|
|
103
103
|
</div>
|
|
104
104
|
|
|
105
105
|
<div id="footer">
|
|
106
|
-
Generated on
|
|
106
|
+
Generated on Sat Feb 2 09:15:15 2013 by
|
|
107
107
|
<a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
|
108
|
-
0.8.
|
|
108
|
+
0.8.3 (ruby-1.9.3).
|
|
109
109
|
</div>
|
|
110
110
|
|
|
111
111
|
</body>
|
data/elephas.gemspec
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# encoding: utf-8
|
|
2
2
|
#
|
|
3
|
-
# This file is part of the elephas gem. Copyright (C)
|
|
3
|
+
# This file is part of the elephas gem. Copyright (C) 2013 and above Shogun <shogun_panda@me.com>.
|
|
4
4
|
# Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
|
|
5
5
|
#
|
|
6
6
|
|
|
@@ -9,7 +9,7 @@ require File.expand_path('../lib/elephas/version', __FILE__)
|
|
|
9
9
|
Gem::Specification.new do |gem|
|
|
10
10
|
gem.name = "elephas"
|
|
11
11
|
gem.version = Elephas::Version::STRING
|
|
12
|
-
gem.homepage = "http://
|
|
12
|
+
gem.homepage = "http://sw.cow.tc/elephas"
|
|
13
13
|
gem.summary = %q{A storage agnostic caching framework.}
|
|
14
14
|
gem.description = %q{A storage agnostic caching framework.}
|
|
15
15
|
gem.rubyforge_project = "elephas"
|
|
@@ -22,13 +22,15 @@ Gem::Specification.new do |gem|
|
|
|
22
22
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
|
23
23
|
gem.require_paths = ["lib"]
|
|
24
24
|
|
|
25
|
-
gem.
|
|
25
|
+
gem.required_ruby_version = ">= 1.9.2"
|
|
26
26
|
|
|
27
|
-
gem.
|
|
28
|
-
|
|
29
|
-
gem.add_development_dependency("
|
|
27
|
+
gem.add_dependency("lazier", "~> 2.5.0")
|
|
28
|
+
|
|
29
|
+
gem.add_development_dependency("rspec", "~> 2.12.0")
|
|
30
|
+
gem.add_development_dependency("rake", "~> 10.0.3")
|
|
31
|
+
gem.add_development_dependency("simplecov", "~> 0.7.1")
|
|
30
32
|
gem.add_development_dependency("pry", ">= 0")
|
|
31
|
-
gem.add_development_dependency("yard", "~> 0.8.
|
|
33
|
+
gem.add_development_dependency("yard", "~> 0.8.3")
|
|
32
34
|
gem.add_development_dependency("redcarpet", "~> 2.2.2")
|
|
33
|
-
gem.add_development_dependency("github-markup", "~> 0.7.
|
|
35
|
+
gem.add_development_dependency("github-markup", "~> 0.7.5")
|
|
34
36
|
end
|
data/lib/elephas/cache.rb
CHANGED
|
@@ -1,132 +1,153 @@
|
|
|
1
1
|
# encoding: utf-8
|
|
2
2
|
#
|
|
3
|
-
# This file is part of the elephas gem. Copyright (C)
|
|
3
|
+
# This file is part of the elephas gem. Copyright (C) 2013 and above Shogun <shogun_panda@me.com>.
|
|
4
4
|
# Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
|
|
5
5
|
#
|
|
6
6
|
|
|
7
7
|
module Elephas
|
|
8
8
|
# This is the main class of the framework. Use only this class to access the cache.
|
|
9
9
|
class Cache
|
|
10
|
+
# @attribute provider
|
|
11
|
+
# @return [Provider] The provider used for the caching.
|
|
10
12
|
class << self
|
|
11
|
-
# The provider used for the caching.
|
|
12
13
|
attr_accessor :provider
|
|
14
|
+
end
|
|
13
15
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
options = self.setup_options(options, key)
|
|
44
|
-
|
|
45
|
-
# Check if the storage has the value (if we don't have to skip the cache)
|
|
46
|
-
rv = self.provider.read(options[:hash]) if options[:force] == false && options[:ttl] > 0
|
|
47
|
-
|
|
48
|
-
if rv.nil? && block_given? then # Try to compute the value from the block
|
|
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
|
|
52
|
-
end
|
|
53
|
-
|
|
54
|
-
# Return value
|
|
55
|
-
options[:as_entry] ? rv : rv.value.dup
|
|
56
|
-
end
|
|
16
|
+
# This is the main method of the framework.
|
|
17
|
+
#
|
|
18
|
+
# It tries reading a key from the cache.
|
|
19
|
+
#
|
|
20
|
+
# If it doesn't find it, it uses the provided block (which receives options as argument) to compute its value and then store it into the cache for later usages.
|
|
21
|
+
#
|
|
22
|
+
# ```ruby
|
|
23
|
+
# value = Elephas::Cache.use("KEY") do |options|
|
|
24
|
+
# "VALUE"
|
|
25
|
+
# end
|
|
26
|
+
#
|
|
27
|
+
# value
|
|
28
|
+
# # => "VALUE"
|
|
29
|
+
#
|
|
30
|
+
# value = Elephas::Cache.use("KEY") do |options|
|
|
31
|
+
# "ANOTHER VALUE"
|
|
32
|
+
# end
|
|
33
|
+
#
|
|
34
|
+
# value
|
|
35
|
+
# # => "VALUE"
|
|
36
|
+
# ```
|
|
37
|
+
#
|
|
38
|
+
# @param key [String] The key to lookup.
|
|
39
|
+
# @param options [Hash] A list of options for managing this key.
|
|
40
|
+
# @param block [Proc] An optional block to run to compute the value for the key if nothing is found.
|
|
41
|
+
# @return [Object|Entry] The found or newly-set value associated to the key.
|
|
42
|
+
# @see .setup_options
|
|
43
|
+
def self.use(key, options = {}, &block)
|
|
44
|
+
rv = nil
|
|
57
45
|
|
|
58
|
-
#
|
|
59
|
-
|
|
60
|
-
# @param key [String] The key to lookup.
|
|
61
|
-
# @return [Object|NilClass] The read value or `nil`.
|
|
62
|
-
def read(key)
|
|
63
|
-
self.provider.read(key)
|
|
64
|
-
end
|
|
46
|
+
# Get options
|
|
47
|
+
options = ::Elephas::Cache.setup_options(options, key)
|
|
65
48
|
|
|
66
|
-
#
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
# @param value [Object] The value to write. Setting a value to `nil` **doesn't** mean *deleting* the value.
|
|
70
|
-
# @param options [Hash] A list of options for writing.
|
|
71
|
-
# @see .setup_options
|
|
72
|
-
# @return [Object] The value itself.
|
|
73
|
-
def write(key, value, options = {})
|
|
74
|
-
self.provider.write(key, value, self.setup_options(options, key))
|
|
75
|
-
end
|
|
49
|
+
# Check if the storage has the value (if we don't have to skip the cache)
|
|
50
|
+
rv = @provider.read(options[:hash]) if options[:force] == false && options[:ttl] > 0
|
|
51
|
+
rv = compute_value(options, &block) if rv.nil? && block # Try to compute the value from the block
|
|
76
52
|
|
|
77
|
-
#
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
# @return [Boolean] `true` if the key was in the cache, `false` otherwise.
|
|
81
|
-
def delete(key)
|
|
82
|
-
self.provider.delete(key)
|
|
83
|
-
end
|
|
53
|
+
# Return value
|
|
54
|
+
options[:as_entry] ? rv : rv.value.dup
|
|
55
|
+
end
|
|
84
56
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
57
|
+
# Reads a value from the cache.
|
|
58
|
+
#
|
|
59
|
+
# @param key [String] The key to lookup.
|
|
60
|
+
# @return [Object|NilClass] The read value or `nil`.
|
|
61
|
+
def self.read(key)
|
|
62
|
+
@provider.read(key)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# Writes a value to the cache.
|
|
66
|
+
#
|
|
67
|
+
# @param key [String] The key to associate the value with.
|
|
68
|
+
# @param value [Object] The value to write. Setting a value to `nil` **doesn't** mean *deleting* the value.
|
|
69
|
+
# @param options [Hash] A list of options for writing.
|
|
70
|
+
# @see .setup_options
|
|
71
|
+
# @return [Object] The value itself.
|
|
72
|
+
def self.write(key, value, options = {})
|
|
73
|
+
@provider.write(key, value, ::Elephas::Cache.setup_options(options, key))
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# Deletes a value from the cache.
|
|
77
|
+
#
|
|
78
|
+
# @param key [String] The key to delete.
|
|
79
|
+
# @return [Boolean] `true` if the key was in the cache, `false` otherwise.
|
|
80
|
+
def self.delete(key)
|
|
81
|
+
@provider.delete(key)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# Checks if a key exists in the cache.
|
|
85
|
+
#
|
|
86
|
+
# @param key [String] The key to lookup.
|
|
87
|
+
# @return [Boolean] `true` if the key is in the cache, `false` otherwise.
|
|
88
|
+
def self.exists?(key)
|
|
89
|
+
@provider.exists?(key)
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
# Returns the default prefix for cache entries.
|
|
93
|
+
#
|
|
94
|
+
# @return [String] The default prefix for cache entries.
|
|
95
|
+
def self.default_prefix
|
|
96
|
+
"elephas-#{::Elephas::Version::STRING}-cache"
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# Setups options for use into the framework.
|
|
100
|
+
# Valid options are:
|
|
101
|
+
#
|
|
102
|
+
# * **: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.
|
|
103
|
+
# * **:force**: Setting it to `true` will always skip the cache.
|
|
104
|
+
# * **:key**: The key associated to this value. **You should never set this option directly.**
|
|
105
|
+
# * **:prefix**: The prefix used in cache. This is used to avoid conflicts with other caching frameworks.
|
|
106
|
+
# * **:complete_key**: The complete key used for computing the hash. By default is concatenation of `:key` and `:prefix` options.
|
|
107
|
+
# * **:hash**: The hash used to store the key in the cache. Should be unique
|
|
108
|
+
# * **:as_entry**: In `Elephas::Cache.use`, setting this to `true` will return the entire `Entry` object rather than the value only.
|
|
109
|
+
#
|
|
110
|
+
# @param options [Object] An initial setup.
|
|
111
|
+
# @param key [String] The key to associate to this options.
|
|
112
|
+
# @return [Hash] An options hash.
|
|
113
|
+
def self.setup_options(options, key)
|
|
114
|
+
options = {} if !options.is_a?(::Hash)
|
|
115
|
+
options = {ttl: 1.hour * 1000, force: false, as_entry: false}.merge(options)
|
|
116
|
+
|
|
117
|
+
# Sanitize options.
|
|
118
|
+
options = sanitize_options(options, key)
|
|
119
|
+
|
|
120
|
+
# Wrap the final key to ensure we don't have colliding namespaces.
|
|
121
|
+
options[:complete_key] ||= "#{options[:prefix]}[#{options[:key]}]"
|
|
122
|
+
|
|
123
|
+
# Compute the hash key used for referencing this value.
|
|
124
|
+
options[:hash] ||= ::Elephas::Entry.hashify_key(options[:complete_key])
|
|
92
125
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
126
|
+
options
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
private
|
|
130
|
+
# Computes a new value and saves it to the cache.
|
|
131
|
+
# @param options [Hash] A list of options for managing the value.
|
|
132
|
+
# @param block [Proc] The block to run to compute the value.
|
|
133
|
+
# @return [Object|Entry] The new value.
|
|
134
|
+
def self.compute_value(options, &block)
|
|
135
|
+
rv = block.call(options)
|
|
136
|
+
rv = ::Elephas::Entry.ensure(rv, options[:complete_key], options) # Make sure is an entry
|
|
137
|
+
Elephas::Cache.write(rv.hash, rv, options) if !rv.value.nil? && options[:ttl] > 0 # We have a value and we have to store it
|
|
138
|
+
rv
|
|
98
139
|
end
|
|
99
140
|
|
|
100
|
-
#
|
|
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
|
-
#
|
|
141
|
+
# Sanitizes options for safe usage.
|
|
111
142
|
# @param options [Object] An initial setup.
|
|
112
143
|
# @param key [String] The key to associate to this options.
|
|
113
144
|
# @return [Hash] An options hash.
|
|
114
|
-
def
|
|
115
|
-
options = {} if !options.is_a?(::Hash)
|
|
116
|
-
options = {:ttl => 1.hour * 1000, :force => false, :as_entry => false}.merge(options)
|
|
145
|
+
def self.sanitize_options(options, key)
|
|
117
146
|
options[:key] ||= key.ensure_string
|
|
118
147
|
options[:ttl] == options[:ttl].blank? ? 1.hour * 1000 : [options[:ttl].to_integer, 0].max
|
|
119
148
|
options[:force] = options[:force].to_boolean
|
|
120
149
|
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
150
|
options
|
|
129
151
|
end
|
|
130
|
-
end
|
|
131
152
|
end
|
|
132
153
|
end
|
data/lib/elephas/entry.rb
CHANGED
|
@@ -1,25 +1,27 @@
|
|
|
1
1
|
# encoding: utf-8
|
|
2
2
|
#
|
|
3
|
-
# This file is part of the elephas gem. Copyright (C)
|
|
3
|
+
# This file is part of the elephas gem. Copyright (C) 2013 and above Shogun <shogun_panda@me.com>.
|
|
4
4
|
# Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
|
|
5
5
|
#
|
|
6
6
|
|
|
7
7
|
module Elephas
|
|
8
8
|
# Represents a cache entry.
|
|
9
|
+
#
|
|
10
|
+
# @attribute key
|
|
11
|
+
# @return [String] The key for this entry.
|
|
12
|
+
# @attribute hash
|
|
13
|
+
# @return [String] The hashed (unique) key for this entry.
|
|
14
|
+
# @attribute value
|
|
15
|
+
# @return [Object] The value contained in this entry.
|
|
16
|
+
# @attribute ttl
|
|
17
|
+
# @return [Fixnum] The expected TTL of the entry, in milliseconds.
|
|
18
|
+
# @attribute updated_at
|
|
19
|
+
# @return [Fixnum] The last update date of the entry, in UNIX timestamp (with milliseconds).
|
|
9
20
|
class Entry
|
|
10
|
-
# The key for this entry.
|
|
11
21
|
attr_accessor :key
|
|
12
|
-
|
|
13
|
-
# The hashed (unique) key for this entry.
|
|
14
22
|
attr_accessor :hash
|
|
15
|
-
|
|
16
|
-
# The value contained in this entry.
|
|
17
23
|
attr_accessor :value
|
|
18
|
-
|
|
19
|
-
# The expected TTL of the entry, in milliseconds.
|
|
20
24
|
attr_accessor :ttl
|
|
21
|
-
|
|
22
|
-
# The last update date of the entry, in UNIX timestamp (with milliseconds).
|
|
23
25
|
attr_accessor :updated_at
|
|
24
26
|
|
|
25
27
|
# Creates a new entry.
|
|
@@ -29,11 +31,10 @@ module Elephas
|
|
|
29
31
|
# @param hash [String] The hash for this entry. Should be unique. It is automatically created if not provided.
|
|
30
32
|
# @param ttl [Integer] The time to live (TTL) for this entry. If set to 0 then the entry is not cached.
|
|
31
33
|
def initialize(key, value, hash = nil, ttl = 360000)
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
self.ttl = ttl
|
|
34
|
+
@key = key
|
|
35
|
+
@hash = hash.present? ? hash : self.class.hashify_key(key)
|
|
36
|
+
@value = value
|
|
37
|
+
@ttl = ttl
|
|
37
38
|
self.refresh
|
|
38
39
|
end
|
|
39
40
|
|
|
@@ -42,10 +43,9 @@ module Elephas
|
|
|
42
43
|
# @param save [Boolean] If to save the refresh value in the cache.
|
|
43
44
|
# @return [Float] The new updated_at value.
|
|
44
45
|
def refresh(save = false)
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
self.updated_at
|
|
46
|
+
@updated_at = Time.now.to_f
|
|
47
|
+
Elephas::Cache.provider.write(@hash, self) if save
|
|
48
|
+
@updated_at
|
|
49
49
|
end
|
|
50
50
|
|
|
51
51
|
# Checks if the entry is still valid.
|
|
@@ -57,12 +57,12 @@ module Elephas
|
|
|
57
57
|
provider.now - self.updated_at < self.ttl / 1000
|
|
58
58
|
end
|
|
59
59
|
|
|
60
|
-
#
|
|
60
|
+
# Compares to another Entry.
|
|
61
61
|
#
|
|
62
62
|
# @param other [Entry] The entry to compare with
|
|
63
63
|
# @return [Boolean] `true` if the entries are the same, `false` otherwise.
|
|
64
64
|
def ==(other)
|
|
65
|
-
other.is_a?(::Elephas::Entry) && [
|
|
65
|
+
other.is_a?(::Elephas::Entry) && [@key, @hash, @value] == [other.key, other.hash, other.value]
|
|
66
66
|
end
|
|
67
67
|
|
|
68
68
|
# Returns a unique hash for the key.
|
|
@@ -73,10 +73,11 @@ module Elephas
|
|
|
73
73
|
Digest::SHA2.hexdigest(key.ensure_string)
|
|
74
74
|
end
|
|
75
75
|
|
|
76
|
-
#
|
|
76
|
+
# Ensures that the value is an Entry.
|
|
77
77
|
#
|
|
78
78
|
# @param value [Object] The object to check.
|
|
79
79
|
# @param key [Object] The key associated to this object.
|
|
80
|
+
# @param options [Hash] Options to manage the value.
|
|
80
81
|
# @return [Entry] The wrapped object.
|
|
81
82
|
def self.ensure(value, key, options = {})
|
|
82
83
|
rv = value
|