mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
27 lines
508 B
Ruby
27 lines
508 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
|
||
|
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
|