diff options
author | Joshua Peek <josh@joshpeek.com> | 2009-01-27 18:17:39 -0600 |
---|---|---|
committer | Joshua Peek <josh@joshpeek.com> | 2009-01-27 18:17:39 -0600 |
commit | a0f2b1d95d3785de92ae271fd7ea23e91c0cadc6 (patch) | |
tree | e125027e317889e6402dac147e03fc112c129aec /activesupport | |
parent | eb9af20b7cc0e374277cf330bdd404f9daab28ec (diff) | |
download | rails-a0f2b1d95d3785de92ae271fd7ea23e91c0cadc6.tar.gz rails-a0f2b1d95d3785de92ae271fd7ea23e91c0cadc6.tar.bz2 rails-a0f2b1d95d3785de92ae271fd7ea23e91c0cadc6.zip |
Reorganize ActionController folder structure
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/lib/active_support/memoizable.rb | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/memoizable.rb b/activesupport/lib/active_support/memoizable.rb index 4ffb5d9520..8e9abeaf91 100644 --- a/activesupport/lib/active_support/memoizable.rb +++ b/activesupport/lib/active_support/memoizable.rb @@ -1,4 +1,42 @@ 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 + + module SafelyMemoizable + def safely_memoize(*symbols) + symbols.each do |symbol| + class_eval <<-RUBY, __FILE__, __LINE__ + 1 + def #{symbol}(*args) + memoized = @_memoized_#{symbol} || ::ActiveSupport::ConcurrentHash.new + memoized[args] ||= memoized_#{symbol}(*args) + end + RUBY + end + end + end + module Memoizable def self.memoized_ivar_for(symbol) "@_memoized_#{symbol.to_s.sub(/\?\Z/, '_query').sub(/!\Z/, '_bang')}".to_sym |