diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2005-05-02 07:04:20 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2005-05-02 07:04:20 +0000 |
commit | cce294fd7118f4ae47d130ba85dfae4dc816b3e9 (patch) | |
tree | 5be40c6f2b9e3cba410f53674c8be4c434eee074 /activerecord | |
parent | 5f77f64e924e0e40f5f141dd38df5d54335b7a46 (diff) | |
download | rails-cce294fd7118f4ae47d130ba85dfae4dc816b3e9.tar.gz rails-cce294fd7118f4ae47d130ba85dfae4dc816b3e9.tar.bz2 rails-cce294fd7118f4ae47d130ba85dfae4dc816b3e9.zip |
Started work on a per-request query cache
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1267 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord')
-rwxr-xr-x | activerecord/lib/active_record.rb | 2 | ||||
-rw-r--r-- | activerecord/lib/active_record/query_cache.rb | 64 |
2 files changed, 66 insertions, 0 deletions
diff --git a/activerecord/lib/active_record.rb b/activerecord/lib/active_record.rb index 02d5968be7..a6169272a1 100755 --- a/activerecord/lib/active_record.rb +++ b/activerecord/lib/active_record.rb @@ -67,3 +67,5 @@ require 'active_record/connection_adapters/sqlite_adapter' require 'active_record/connection_adapters/sqlserver_adapter' require 'active_record/connection_adapters/db2_adapter' require 'active_record/connection_adapters/oci_adapter' + +require 'active_record/query_cache' diff --git a/activerecord/lib/active_record/query_cache.rb b/activerecord/lib/active_record/query_cache.rb new file mode 100644 index 0000000000..368b08e506 --- /dev/null +++ b/activerecord/lib/active_record/query_cache.rb @@ -0,0 +1,64 @@ +module ActiveRecord + class QueryCache + def initialize(connection) + @connection = connection + @query_cache = {} + end + + def clear_query_cache + @query_cache = {} + end + + def select_all(sql, name = nil) + @query_cache[sql] ||= @connection.select_all(sql, name) + end + + def select_one(sql, name = nil) + @query_cache[sql] ||= @connection.select_one(sql, name) + end + + def columns(table_name, name = nil) + @query_cache["SHOW FIELDS FROM #{table_name}"] ||= @connection.columns(table_name, name) + end + + def insert(sql, name = nil, pk = nil, id_value = nil) + clear_query_cache + @connection.insert(sql, name, pk, id_value) + end + + def update(sql, name = nil) + clear_query_cache + @connection.update(sql, name) + end + + def delete(sql, name = nil) + clear_query_cache + @connection.delete(sql, name) + end + + private + def method_missing(method, *arguments) + @connection.send(method, *arguments) + end + end + + class Base + # Set the connection for the class with caching on + def self.connection=(spec) + raise ConnectionNotEstablished unless spec + + conn = spec.config[:query_cache] ? + QueryCache.new(self.send(spec.adapter_method, spec.config)) : + self.send(spec.adapter_method, spec.config) + + Thread.current['active_connections'] ||= {} + Thread.current['active_connections'][self] = conn + end + end + + class AbstractAdapter + # Stub method to be able to treat the connection the same whether the query cache has been turned on or not + def clear_query_cache + end + end +end |