aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/concurrent_hash.rb
diff options
context:
space:
mode:
Diffstat (limited to 'activesupport/lib/active_support/concurrent_hash.rb')
-rw-r--r--activesupport/lib/active_support/concurrent_hash.rb27
1 files changed, 27 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/concurrent_hash.rb b/activesupport/lib/active_support/concurrent_hash.rb
new file mode 100644
index 0000000000..40224765a7
--- /dev/null
+++ b/activesupport/lib/active_support/concurrent_hash.rb
@@ -0,0 +1,27 @@
+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