Class: Redis::Pipeline

Inherits:
Object
  • Object
show all
Defined in:
lib/redis/pipeline.rb

Direct Known Subclasses

DeprecatedPipeline, Multi

Defined Under Namespace

Classes: Multi

Constant Summary collapse

REDIS_INTERNAL_PATH =
File.expand_path("..", __dir__).freeze
STDLIB_PATH =

Redis use MonitorMixin#synchronize and this class use DelegateClass which we want to filter out. Both are in the stdlib so we can simply filter the entire stdlib out.

File.expand_path("..", MonitorMixin.instance_method(:synchronize).source_location.first).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Pipeline

Returns a new instance of Pipeline.


79
80
81
82
83
84
# File 'lib/redis/pipeline.rb', line 79

def initialize(client)
  @client = client.is_a?(Pipeline) ? client.client : client
  @with_reconnect = true
  @shutdown = false
  @futures = []
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.


74
75
76
# File 'lib/redis/pipeline.rb', line 74

def client
  @client
end

#dbObject

Returns the value of attribute db.


73
74
75
# File 'lib/redis/pipeline.rb', line 73

def db
  @db
end

#futuresObject (readonly) Also known as: materialized_futures

Returns the value of attribute futures.


76
77
78
# File 'lib/redis/pipeline.rb', line 76

def futures
  @futures
end

Class Method Details

.deprecation_warning(method, caller_locations) ⇒ Object

:nodoc:


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/redis/pipeline.rb', line 52

def deprecation_warning(method, caller_locations) # :nodoc:
  callsite = caller_locations.find { |l| !l.path.start_with?(REDIS_INTERNAL_PATH, STDLIB_PATH) }
  callsite ||= caller_locations.last # The caller_locations should be large enough, but just in case.
  ::Redis.deprecate! <<~MESSAGE
    Pipelining commands on a Redis instance is deprecated and will be removed in Redis 5.0.0.

    redis.#{method} do
      redis.get("key")
    end

    should be replaced by

    redis.#{method} do |pipeline|
      pipeline.get("key")
    end

    (called from #{callsite}}
  MESSAGE
end

Instance Method Details

#call(command, timeout: nil, &block) ⇒ Object


106
107
108
109
110
111
112
113
# File 'lib/redis/pipeline.rb', line 106

def call(command, timeout: nil, &block)
  # A pipeline that contains a shutdown should not raise ECONNRESET when
  # the connection is gone.
  @shutdown = true if command.first == :shutdown
  future = Future.new(command, block, timeout)
  @futures << future
  future
end

#call_pipeline(pipeline) ⇒ Object


119
120
121
122
123
124
# File 'lib/redis/pipeline.rb', line 119

def call_pipeline(pipeline)
  @shutdown = true if pipeline.shutdown?
  @futures.concat(pipeline.materialized_futures)
  @db = pipeline.db
  nil
end

#call_with_timeout(command, timeout, &block) ⇒ Object


115
116
117
# File 'lib/redis/pipeline.rb', line 115

def call_with_timeout(command, timeout, &block)
  call(command, timeout: timeout, &block)
end

#commandsObject


126
127
128
# File 'lib/redis/pipeline.rb', line 126

def commands
  @futures.map(&:_command)
end

#empty?Boolean

Returns:

  • (Boolean)

102
103
104
# File 'lib/redis/pipeline.rb', line 102

def empty?
  @futures.empty?
end

#finish(replies, &blk) ⇒ Object


143
144
145
146
147
148
149
150
151
152
153
# File 'lib/redis/pipeline.rb', line 143

def finish(replies, &blk)
  if blk
    futures.each_with_index.map do |future, i|
      future._set(blk.call(replies[i]))
    end
  else
    futures.each_with_index.map do |future, i|
      future._set(replies[i])
    end
  end
end

#shutdown?Boolean

Returns:

  • (Boolean)

98
99
100
# File 'lib/redis/pipeline.rb', line 98

def shutdown?
  @shutdown
end

#timeoutObject


86
87
88
# File 'lib/redis/pipeline.rb', line 86

def timeout
  client.timeout
end

#timeoutsObject


130
131
132
# File 'lib/redis/pipeline.rb', line 130

def timeouts
  @futures.map(&:timeout)
end

#with_reconnect(val = true) ⇒ Object


134
135
136
137
# File 'lib/redis/pipeline.rb', line 134

def with_reconnect(val = true)
  @with_reconnect = false unless val
  yield
end

#with_reconnect?Boolean

Returns:

  • (Boolean)

90
91
92
# File 'lib/redis/pipeline.rb', line 90

def with_reconnect?
  @with_reconnect
end

#without_reconnect(&blk) ⇒ Object


139
140
141
# File 'lib/redis/pipeline.rb', line 139

def without_reconnect(&blk)
  with_reconnect(false, &blk)
end

#without_reconnect?Boolean

Returns:

  • (Boolean)

94
95
96
# File 'lib/redis/pipeline.rb', line 94

def without_reconnect?
  !@with_reconnect
end