aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2011-09-06 16:06:27 -0700
committerAaron Patterson <aaron.patterson@gmail.com>2011-09-06 16:06:27 -0700
commita4fa6eab396e703eb70b70ed708220a6405f2899 (patch)
treeac11c7ef42fdb85860a61acc046cc937d92e03cc /activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb
parent51652a4773ab19c866ea1781b39f46f17faa68f3 (diff)
downloadrails-a4fa6eab396e703eb70b70ed708220a6405f2899.tar.gz
rails-a4fa6eab396e703eb70b70ed708220a6405f2899.tar.bz2
rails-a4fa6eab396e703eb70b70ed708220a6405f2899.zip
adding a statement pool for mysql and sqlite3
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb')
-rw-r--r--activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb38
1 files changed, 33 insertions, 5 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb
index a90c675bf6..a4e21b714b 100644
--- a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb
@@ -1,4 +1,5 @@
require 'active_record/connection_adapters/abstract_adapter'
+require 'active_record/connection_adapters/statement_pool'
require 'active_support/core_ext/string/encoding'
module ActiveRecord
@@ -48,9 +49,40 @@ module ActiveRecord
end
end
+ class StatementPool < ConnectionAdapters::StatementPool
+ def initialize(connection, max = 1000)
+ super
+ @cache = {}
+ end
+
+ def each(&block); @cache.each(&block); end
+ def key?(key); @cache.key?(key); end
+ def [](key); @cache[key]; end
+ def length; @cache.length; end
+
+ def []=(sql, key)
+ while @max <= @cache.size
+ dealloc(@cache.shift.last[:stmt])
+ end
+ @cache[sql] = key
+ end
+
+ def clear
+ @cache.values.each do |hash|
+ dealloc hash[:stmt]
+ end
+ @cache.clear
+ end
+
+ private
+ def dealloc(stmt)
+ stmt.close unless stmt.closed?
+ end
+ end
+
def initialize(connection, logger, config)
super(connection, logger)
- @statements = {}
+ @statements = StatementPool.new(@connection)
@config = config
end
@@ -107,10 +139,6 @@ module ActiveRecord
# Clears the prepared statements cache.
def clear_cache!
- @statements.values.map { |hash| hash[:stmt] }.each { |stmt|
- stmt.close unless stmt.closed?
- }
-
@statements.clear
end