diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2015-05-18 15:34:28 -0700 |
---|---|---|
committer | Ryuta Kamizono <kamipo@gmail.com> | 2015-05-19 11:17:19 -0700 |
commit | f9e27bc582fc66e04f11d42cb420636654872cba (patch) | |
tree | 596c062630ba4e5130d2628ee2b0a535f6be87f7 /activerecord/lib/active_record/connection_adapters/statement_pool.rb | |
parent | 7fb90694f0b7123346666567249500361cd28d68 (diff) | |
download | rails-f9e27bc582fc66e04f11d42cb420636654872cba.tar.gz rails-f9e27bc582fc66e04f11d42cb420636654872cba.tar.bz2 rails-f9e27bc582fc66e04f11d42cb420636654872cba.zip |
Eliminate the duplication code of `StatementPool`
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters/statement_pool.rb')
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/statement_pool.rb | 34 |
1 files changed, 26 insertions, 8 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/statement_pool.rb b/activerecord/lib/active_record/connection_adapters/statement_pool.rb index c6b1bc8b5b..82e9ef3d3d 100644 --- a/activerecord/lib/active_record/connection_adapters/statement_pool.rb +++ b/activerecord/lib/active_record/connection_adapters/statement_pool.rb @@ -4,35 +4,53 @@ module ActiveRecord include Enumerable def initialize(connection, max = 1000) + @cache = Hash.new { |h,pid| h[pid] = {} } @connection = connection @max = max end - def each - raise NotImplementedError + def each(&block) + cache.each(&block) end def key?(key) - raise NotImplementedError + cache.key?(key) end def [](key) - raise NotImplementedError + cache[key] end def length - raise NotImplementedError + cache.length end - def []=(sql, key) - raise NotImplementedError + def []=(sql, stmt) + while @max <= cache.size + dealloc(cache.shift.last) + end + cache[sql] = stmt end def clear - raise NotImplementedError + cache.each_value do |stmt| + dealloc stmt + end + cache.clear end def delete(key) + dealloc cache[key] + cache.delete(key) + end + + private + + def cache + @cache[Process.pid] + end + + def dealloc(stmt) raise NotImplementedError end end |