Skip to content

Commit

Permalink
Use Hash for Memoizable::Memory instead of ThreadSafe::Cache
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Dec 19, 2013
1 parent 0cdfc8a commit e267ac8
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 15 deletions.
3 changes: 0 additions & 3 deletions lib/memoizable.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
# encoding: utf-8

require 'monitor'
require 'thread_safe'

require 'memoizable/instance_methods'
require 'memoizable/method_builder'
require 'memoizable/module_methods'
Expand Down
15 changes: 5 additions & 10 deletions lib/memoizable/memory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ class Memory
#
# @api private
def initialize
@memory = ThreadSafe::Cache.new
@monitor = Monitor.new
@memory = Hash.new
freeze
end

Expand All @@ -39,9 +38,9 @@ def [](name)
# @api public
def []=(name, value)
memoized = true
@memory.compute_if_absent(name) do
@memory.fetch(name) do
memoized = false
value
@memory[name] = value
end
fail ArgumentError, "The method #{name} is already memoized" if memoized
end
Expand All @@ -55,12 +54,8 @@ def []=(name, value)
#
# @api public
def fetch(name)
@memory.fetch(name) do # check for the key
@monitor.synchronize do # acquire a lock if the key is not found
@memory.fetch(name) do # recheck under lock
self[name] = yield # set the value
end
end
@memory.fetch(name) do
self[name] = yield
end
end

Expand Down
2 changes: 0 additions & 2 deletions memoizable.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,5 @@ Gem::Specification.new do |gem|
gem.test_files = Dir.glob('spec/{unit,integration}/**/*.rb')
gem.extra_rdoc_files = Dir.glob('**/*.md')

gem.add_runtime_dependency('thread_safe', '~> 0.1.3')

gem.add_development_dependency('bundler', '~> 1.3', '>= 1.3.5')
end

0 comments on commit e267ac8

Please sign in to comment.