Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable query cache for lock queries #6985

Merged
merged 1 commit into from Jul 6, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -56,7 +56,7 @@ def clear_query_cache
end

def select_all(arel, name = nil, binds = [])
if @query_cache_enabled
if @query_cache_enabled && !locked?(arel)
sql = to_sql(arel, binds)
cache_sql(sql, binds) { super(sql, name, binds) }
else
Expand All @@ -83,6 +83,14 @@ def cache_sql(sql, binds)
result.collect { |row| row.dup }
end
end

def locked?(arel)
if arel.respond_to?(:locked)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there some case where locked is not defined?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, sometimes the arel is actually a String.

Without the check QueryCacheTest#test_cache_does_not_wrap_string_results_in_arrays was failing.

arel.locked
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI we have a scope called locked and this is blowing up ;)

else
false
end
end
end
end
end
8 changes: 8 additions & 0 deletions activerecord/test/cases/query_cache_test.rb
Expand Up @@ -164,6 +164,14 @@ def test_cache_does_not_wrap_string_results_in_arrays
end
end
end

def test_cache_is_ignored_for_locked_relations
task = Task.find 1

Task.cache do
assert_queries(2) { task.lock!; task.lock! }
end
end
end

class QueryCacheExpiryTest < ActiveRecord::TestCase
Expand Down