Files
rails/activesupport/lib/active_support/concurrent_hash.rb
Carl Lerche & Yehuda Katz 647b83d50c Resurrecting 1.9 compatibility.
2009-04-13 16:56:04 -07:00

28 lines
516 B
Ruby

module ActiveSupport
class ConcurrentHash
def initialize(hash = {})
@backup_cache = hash.dup
@frozen_cache = hash.dup.freeze
@mutex = Mutex.new
end
def []=(k,v)
@mutex.synchronize { @backup_cache[k] = v }
@frozen_cache = @backup_cache.dup.freeze
v
end
def [](k)
if @frozen_cache.key?(k)
@frozen_cache[k]
else
@mutex.synchronize { @backup_cache[k] }
end
end
def empty?
@backup_cache.empty?
end
end
end