memoizable 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,6 @@
1
1
  # encoding: utf-8
2
2
 
3
+ require 'monitor'
3
4
  require 'thread_safe'
4
5
 
5
6
  require 'memoizable/instance_methods'
@@ -30,7 +30,7 @@ module Memoizable
30
30
  #
31
31
  # @api public
32
32
  def memoize(data)
33
- memoized_method_cache.set(data)
33
+ data.each { |name, value| memoized_method_cache[name] = value }
34
34
  self
35
35
  end
36
36
 
@@ -11,7 +11,8 @@ module Memoizable
11
11
  #
12
12
  # @api private
13
13
  def initialize
14
- @memory = ThreadSafe::Cache.new
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, &block)
57
- @memory.compute_if_absent(name, &block)
58
- end
59
-
60
- # Set the memory
61
- #
62
- # @param [Hash]
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
@@ -3,6 +3,6 @@
3
3
  module Memoizable
4
4
 
5
5
  # Gem version
6
- VERSION = '0.3.0'.freeze
6
+ VERSION = '0.3.1'.freeze
7
7
 
8
8
  end # Memoizable
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.0
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-14 00:00:00.000000000 Z
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: