Skip to content

Commit

Permalink
Include host and port in cache destination (#63)
Browse files Browse the repository at this point in the history
Database only just works for SQLite, need host and port to distinguish
DBs otherwise.
  • Loading branch information
djmb authored Aug 10, 2023
1 parent eab54dc commit 536e7d0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
8 changes: 6 additions & 2 deletions lib/solid_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ def self.shard_config(shard)
all_shards_config && all_shards_config[shard]
end

def self.shard_databases
@shard_databases ||= each_shard.map.to_h { [ Record.current_shard, Record.connection_db_config.database ] }
def self.shard_destinations
@shard_databases ||= each_shard.map.to_h do
config = Record.connection_db_config
destination = [ config.try(:host), config.try(:port), config.try(:database) ].compact.join("-")
[ Record.current_shard, destination ]
end
end

def self.each_shard
Expand Down
10 changes: 5 additions & 5 deletions lib/solid_cache/cluster/connection_handling.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ def shards
@shards ||= @shard_options || SolidCache.all_shard_keys || [nil]
end

def database_shards
@database_shards ||= shards.compact.to_h { |shard| [ SolidCache.shard_databases[shard], shard ] }
def destination_shards
@destination_shards ||= shards.compact.to_h { |shard| [ SolidCache.shard_destinations[shard], shard ] }
end

def writing_all_shards
Expand Down Expand Up @@ -84,13 +84,13 @@ def in_shards(list)
def shard_for_normalized_key(normalized_key)
return shards.first if shards.count == 1

database = consistent_hash.node(normalized_key)
database_shards[database]
destination = consistent_hash.node(normalized_key)
destination_shards[destination]
end

def consistent_hash
return nil if shards.count == 1
@consistent_hash ||= MaglevHash.new(database_shards.keys)
@consistent_hash ||= MaglevHash.new(destination_shards.keys)
end

def async_if_required
Expand Down
6 changes: 4 additions & 2 deletions test/unit/solid_cache_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ class SolidCacheTest < ActiveSupport::TestCase
shard_names = [:default, :default2, :primary_shard_one, :primary_shard_two, :secondary_shard_one, :secondary_shard_two]
expected = shard_names.to_h do |shard_name|
database_name = SolidCache.connects_to.dig(:shards, shard_name, :writing).to_s
[ shard_name, configs.configs_for(env_name: Rails.env, name: database_name).database ]
config = configs.configs_for(env_name: Rails.env, name: database_name)
destination = [ config.try(:host), config.try(:port), config.try(:database) ].compact.join("-")
[ shard_name, destination ]
end

assert_equal expected, SolidCache.shard_databases
assert_equal expected, SolidCache.shard_destinations
end
end

Expand Down

0 comments on commit 536e7d0

Please sign in to comment.