public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
rails / activesupport / lib / active_support / concurrent_hash.rb
100644 28 lines (24 sloc) 0.516 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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