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

Use the default SolidCache::Record role #108

Merged
merged 1 commit into from
Jan 4, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/models/solid_cache/record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ def disable_instrumentation
end

def with_shard(shard, &block)
if shard && shard != Record.current_shard
Record.connected_to(shard: shard, &block)
if shard && SolidCache.connects_to
connected_to(shard: shard, role: default_role, prevent_writes: false, &block)
else
block.call
end
Expand Down
2 changes: 1 addition & 1 deletion lib/solid_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def self.each_shard

if (shards = all_shards_config&.keys)
shards.each do |shard|
Record.connected_to(shard: shard) { yield }
Record.with_shard(shard) { yield }
end
else
yield
Expand Down
9 changes: 9 additions & 0 deletions test/unit/solid_cache_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ class SolidCacheTest < ActiveSupport::TestCase
end
end

test "each_shard uses the default role" do
role = ActiveRecord::Base.connected_to(role: :reading) { SolidCache.each_shard.map { SolidCache::Record.current_role } }
if ENV["NO_CONNECTS_TO"]
assert_equal [ :reading ], role
else
assert_equal [ :writing, :writing, :writing, :writing, :writing ], role
end
end

test "max key bytesize" do
cache = lookup_store(max_key_bytesize: 100)
assert_equal 100, cache.send(:normalize_key, SecureRandom.hex(200), {}).bytesize
Expand Down