Skip to content

Commit

Permalink
Remove roles entirely, assume just writers (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
djmb authored Aug 3, 2023
1 parent 60fed98 commit fa9d0dc
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 25 deletions.
4 changes: 0 additions & 4 deletions lib/solid_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,4 @@ def self.all_shards_config
def self.shard_config(shard)
all_shards_config && all_shards_config[shard]
end

def self.shard_first_role(shard)
shard_config(shard)&.first&.first
end
end
2 changes: 1 addition & 1 deletion lib/solid_cache/async_execution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def async(&block)
current_shard = Entry.current_shard
@executor << ->() do
wrap_in_rails_executor do
with_role_and_shard(role: ActiveRecord.writing_role, shard: current_shard) do
with_shard(current_shard) do
block.call(current_shard)
end
end
Expand Down
25 changes: 10 additions & 15 deletions lib/solid_cache/connection_handling.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def writing_all_shards
return enum_for(:writing_all_shards) unless block_given?

shards.each do |shard|
with_role_and_shard(role: ActiveRecord.writing_role, shard: shard) { yield }
with_shard(shard) { yield }
end
end

Expand All @@ -21,33 +21,28 @@ def shards

private
def writing_across_shards(list:)
across_shards(role: ActiveRecord.writing_role, list:) { |list| yield list }
across_shards(list:) { |list| yield list }
end

def reading_across_shards(list:)
across_shards(role: ActiveRecord.reading_role, list:) { |list| yield list }
across_shards(list:) { |list| yield list }
end

def writing_shard(normalized_key:)
with_role_and_shard(role: ActiveRecord.writing_role, shard: shard_for_normalized_key(normalized_key)) { yield }
def with_shard_for_key(normalized_key:)
with_shard(shard_for_normalized_key(normalized_key)) { yield }
end

def reading_shard(normalized_key:)
with_role_and_shard(role: ActiveRecord.reading_role, shard: shard_for_normalized_key(normalized_key)) { yield }
end

def with_role_and_shard(role:, shard:)
if role || shard
role ||= SolidCache.shard_first_role(shard)
Record.connected_to(role: role, shard: shard) { yield }
def with_shard(shard)
if shard
Record.connected_to(shard: shard) { yield }
else
yield
end
end

def across_shards(role:, list:)
def across_shards(list:)
in_shards(list).map do |shard, list|
with_role_and_shard(role: role, shard: shard) { yield list }
with_shard(shard) { yield list }
end
end

Expand Down
10 changes: 5 additions & 5 deletions lib/solid_cache/store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def delete_matched(matcher, options = {})
def increment(name, amount = 1, options = nil)
options = merged_options(options)
key = normalize_key(name, options)
writing_shard(normalized_key: key) do
with_shard_for_key(normalized_key: key) do
failsafe :increment do
Entry.increment(key, amount)
end
Expand All @@ -63,7 +63,7 @@ def increment(name, amount = 1, options = nil)
def decrement(name, amount = 1, options = nil)
options = merged_options(options)
key = normalize_key(name, options)
writing_shard(normalized_key: key) do
with_shard_for_key(normalized_key: key) do
failsafe :increment do
Entry.increment(key, -amount)
end
Expand All @@ -88,7 +88,7 @@ def read_entry(key, **options)
end

def read_serialized_entry(key, raw: false, **options)
reading_shard(normalized_key: key) do
with_shard_for_key(normalized_key: key) do
failsafe(:read_entry) do
Entry.get(key)
end
Expand All @@ -100,7 +100,7 @@ def write_entry(key, entry, raw: false, **options)
payload = serialize_entry(entry, raw: raw, **options)
write_serialized_entry(key, payload, raw: raw, **options)

writing_shard(normalized_key: key) do
with_shard_for_key(normalized_key: key) do
failsafe(:write_entry, returning: false) do
Entry.set(key, payload)
trim(1)
Expand Down Expand Up @@ -162,7 +162,7 @@ def write_multi_entries(entries, expires_in: nil, **options)
end

def delete_entry(key, **options)
writing_shard(normalized_key: key) do
with_shard_for_key(normalized_key: key) do
failsafe(:delete_entry, returning: false) do
Entry.delete_by_key(key)
end
Expand Down

0 comments on commit fa9d0dc

Please sign in to comment.