-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
We want to avoid using the ActiveRecord query cache entirely with SolidCache: - Read queries don't need to be cached as that is handled by the local cache - Write queries should not clear the query cache, as you would not expect `Rails.cache.write` to clear out the entire query cache. Ideally we'd just be able to do something like: ```ruby class SolidCache::Record self.uses_query_cache = false end ``` Absent that we need to do a bunch of gymnastics to get the behaviour we want. 1. Selects This is easy enough, we just wrap the query in an `uncached` block 2. Upserts Here we need to avoid calling `connection.exec_insert_all` as that will dirty the query cache. Instead we have to construct the SQL manually and execute it with `connection.exec_query`. 3. Deletes Similarly we need to avoid calling `connection.delete` here. Again we construct the SQL manually and execute it with `connection.exec_delete`.
- Loading branch information
Showing
4 changed files
with
123 additions
and
25 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
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,60 @@ | ||
require "test_helper" | ||
|
||
class QueryCacheTest < ActiveSupport::TestCase | ||
setup do | ||
@cache = nil | ||
@namespace = "test-#{SecureRandom.hex}" | ||
|
||
@cache = lookup_store(expires_in: 60, cluster: { shards: [:default] }) | ||
@peek = lookup_store(expires_in: 60, cluster: { shards: [:default] }) | ||
end | ||
|
||
test "writes don't clear the AR cache" do | ||
SolidCache::Entry.cache do | ||
@cache.write(1, "foo") | ||
assert_equal 1, SolidCache::Entry.count | ||
@cache.write(2, "bar") | ||
assert_equal 1, SolidCache::Entry.count | ||
end | ||
SolidCache::Entry.uncached do | ||
assert_equal 2, SolidCache::Entry.count | ||
end | ||
end | ||
|
||
test "write_multi doesn't clear the AR cache" do | ||
SolidCache::Entry.cache do | ||
@cache.write(1, "foo") | ||
assert_equal 1, SolidCache::Entry.count | ||
@cache.write_multi({ "1" => "bar", "2" => "baz"}) | ||
assert_equal 1, SolidCache::Entry.count | ||
end | ||
SolidCache::Entry.uncached do | ||
assert_equal 2, SolidCache::Entry.count | ||
end | ||
end | ||
|
||
test "deletes don't clear the AR cache" do | ||
SolidCache::Entry.cache do | ||
@cache.write(1, "foo") | ||
assert_equal 1, SolidCache::Entry.count | ||
@cache.delete(1) | ||
assert_equal 1, SolidCache::Entry.count | ||
end | ||
SolidCache::Entry.uncached do | ||
assert_equal 0, SolidCache::Entry.count | ||
end | ||
end | ||
|
||
test "delete matches don't clear the AR cache" do | ||
SolidCache::Entry.cache do | ||
@cache.write("hello1", "foo") | ||
@cache.write("hello2", "bar") | ||
assert_equal 2, SolidCache::Entry.count | ||
@cache.delete_matched("hello%") | ||
assert_equal 2, SolidCache::Entry.count | ||
end | ||
SolidCache::Entry.uncached do | ||
assert_equal 0, SolidCache::Entry.count | ||
end | ||
end | ||
end |