Skip to content

Commit

Permalink
Merge pull request rails#48988 from jdelStrother/redis-distr-pipeline
Browse files Browse the repository at this point in the history
Add Redis::Distributed support for pipelined write_multi
  • Loading branch information
byroot authored Aug 21, 2023
2 parents 5e2d79b + c47b409 commit bef4c69
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions activesupport/lib/active_support/cache/redis_cache_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ def initialize(error_handler: DEFAULT_ERROR_HANDLER, **redis_options)

@max_key_bytesize = MAX_KEY_BYTESIZE
@error_handler = error_handler
@supports_pipelining = true

super(universal_options)
end
Expand Down Expand Up @@ -294,15 +293,16 @@ def stats
end

private
def pipelined(&block)
if @supports_pipelining
redis.then { |c| c.pipelined(&block) }
else
redis.then(&block)
end
rescue Redis::Distributed::CannotDistribute
@supports_pipelining = false
retry
def pipeline_entries(entries, &block)
redis.then { |c|
if c.is_a?(Redis::Distributed)
entries.group_by { |k, _v| c.node_for(k) }.each do |node, sub_entries|
node.pipelined { |pipe| yield(pipe, sub_entries) }
end
else
c.pipelined { |pipe| yield(pipe, entries) }
end
}
end

# Store provider interface:
Expand Down Expand Up @@ -387,10 +387,10 @@ def write_multi_entries(entries, expires_in: nil, race_condition_ttl: nil, **opt
return if entries.empty?

failsafe :write_multi_entries do
pipelined do |pipeline|
pipeline_entries(entries) do |pipeline, sharded_entries|
options = options.dup
options[:pipeline] = pipeline
entries.each do |key, entry|
sharded_entries.each do |key, entry|
write_entry key, entry, **options
end
end
Expand Down

0 comments on commit bef4c69

Please sign in to comment.