memoizable 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/memoizable.rb +1 -0
- data/lib/memoizable/instance_methods.rb +1 -1
- data/lib/memoizable/memory.rb +10 -15
- data/lib/memoizable/version.rb +1 -1
- metadata +8 -3
data/lib/memoizable.rb
CHANGED
data/lib/memoizable/memory.rb
CHANGED
@@ -11,7 +11,8 @@ module Memoizable
|
|
11
11
|
#
|
12
12
|
# @api private
|
13
13
|
def initialize
|
14
|
-
@memory
|
14
|
+
@memory = ThreadSafe::Cache.new
|
15
|
+
@monitor = Monitor.new
|
15
16
|
freeze
|
16
17
|
end
|
17
18
|
|
@@ -53,20 +54,14 @@ module Memoizable
|
|
53
54
|
# the value to memoize
|
54
55
|
#
|
55
56
|
# @api public
|
56
|
-
def fetch(name
|
57
|
-
@memory.
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
# @return [Memory]
|
65
|
-
#
|
66
|
-
# @api public
|
67
|
-
def set(data)
|
68
|
-
data.each { |name, value| self[name] = value }
|
69
|
-
self
|
57
|
+
def fetch(name)
|
58
|
+
@memory.fetch(name) do # check for the key
|
59
|
+
@monitor.synchronize do # acquire a lock if the key is not found
|
60
|
+
@memory.fetch(name) do # recheck under lock
|
61
|
+
self[name] = yield # set the value
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
70
65
|
end
|
71
66
|
|
72
67
|
# Test if the name has a value in memory
|
data/lib/memoizable/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: memoizable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-12-
|
12
|
+
date: 2013-12-18 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: thread_safe
|
@@ -96,12 +96,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
96
96
|
- - ! '>='
|
97
97
|
- !ruby/object:Gem::Version
|
98
98
|
version: '0'
|
99
|
+
segments:
|
100
|
+
- 0
|
101
|
+
hash: -3254840289484637315
|
99
102
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
103
|
none: false
|
101
104
|
requirements:
|
102
105
|
- - ! '>='
|
103
106
|
- !ruby/object:Gem::Version
|
104
107
|
version: '0'
|
108
|
+
segments:
|
109
|
+
- 0
|
110
|
+
hash: -3254840289484637315
|
105
111
|
requirements: []
|
106
112
|
rubyforge_project:
|
107
113
|
rubygems_version: 1.8.23
|
@@ -120,4 +126,3 @@ test_files:
|
|
120
126
|
- spec/unit/memoizable/module_methods/memoize_spec.rb
|
121
127
|
- spec/unit/memoizable/module_methods/memoized_predicate_spec.rb
|
122
128
|
- spec/unit/memoizable/module_methods/unmemoized_instance_method_spec.rb
|
123
|
-
has_rdoc:
|