-
Notifications
You must be signed in to change notification settings - Fork 21.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Started work on a per-request query cache
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1267 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
- Loading branch information
Showing
2 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |