Don't need ref string; use PPipeline instead of ref TPipeline
This commit is contained in:
parent
5da463e1f7
commit
9a728b15a2
1 changed files with 77 additions and 77 deletions
|
|
@ -20,16 +20,16 @@ const
|
||||||
redisNil* = "\0\0"
|
redisNil* = "\0\0"
|
||||||
|
|
||||||
type
|
type
|
||||||
TPipeline = object
|
PPipeline = ref object
|
||||||
enabled: bool
|
enabled: bool
|
||||||
buffer: ref string
|
buffer: string
|
||||||
expected: int ## number of replies expected if pipelined
|
expected: int ## number of replies expected if pipelined
|
||||||
|
|
||||||
type
|
type
|
||||||
TRedis* {.pure, final.} = object
|
TRedis* {.pure, final.} = object
|
||||||
socket: TSocket
|
socket: TSocket
|
||||||
connected: bool
|
connected: bool
|
||||||
pipeline: ref TPipeline
|
pipeline: PPipeline
|
||||||
|
|
||||||
TRedisStatus* = string
|
TRedisStatus* = string
|
||||||
TRedisInteger* = biggestInt
|
TRedisInteger* = biggestInt
|
||||||
|
|
@ -39,10 +39,9 @@ type
|
||||||
EInvalidReply* = object of ESynch ## Invalid reply from redis
|
EInvalidReply* = object of ESynch ## Invalid reply from redis
|
||||||
ERedis* = object of ESynch ## Error in redis
|
ERedis* = object of ESynch ## Error in redis
|
||||||
|
|
||||||
proc newPipeline(): ref TPipeLine =
|
proc newPipeline(): PPipeline =
|
||||||
new(result)
|
new(result)
|
||||||
result.buffer = new string
|
result.buffer = ""
|
||||||
result.buffer[] = ""
|
|
||||||
result.enabled = false
|
result.enabled = false
|
||||||
result.expected = 0
|
result.expected = 0
|
||||||
|
|
||||||
|
|
@ -180,9 +179,9 @@ proc parseNext(r: TRedis): TRedisList =
|
||||||
|
|
||||||
proc flushPipeline*(r: TRedis, wasMulti = false): TRedisList =
|
proc flushPipeline*(r: TRedis, wasMulti = false): TRedisList =
|
||||||
## Send buffered commands, clear buffer, return results
|
## Send buffered commands, clear buffer, return results
|
||||||
if r.pipeline.buffer[].len > 0:
|
if r.pipeline.buffer.len > 0:
|
||||||
r.socket.send(r.pipeline.buffer[])
|
r.socket.send(r.pipeline.buffer)
|
||||||
r.pipeline.buffer[] = ""
|
r.pipeline.buffer = ""
|
||||||
|
|
||||||
var prevState = r.pipeline.enabled
|
var prevState = r.pipeline.enabled
|
||||||
r.pipeline.enabled = false
|
r.pipeline.enabled = false
|
||||||
|
|
@ -201,7 +200,7 @@ proc flushPipeline*(r: TRedis, wasMulti = false): TRedisList =
|
||||||
r.pipeline.expected = 0
|
r.pipeline.expected = 0
|
||||||
r.pipeline.enabled = prevState
|
r.pipeline.enabled = prevState
|
||||||
|
|
||||||
proc setPipeline*(r: TRedis, state: bool) =
|
proc sePPipeline*(r: TRedis, state: bool) =
|
||||||
## Enable or disable command pipelining (reduces network roundtrips).
|
## Enable or disable command pipelining (reduces network roundtrips).
|
||||||
## Note that when enabled, you must call flushPipeline to actually send commands, except
|
## Note that when enabled, you must call flushPipeline to actually send commands, except
|
||||||
## for multi/exec() which enable and flush the pipeline automatically.
|
## for multi/exec() which enable and flush the pipeline automatically.
|
||||||
|
|
@ -219,7 +218,7 @@ proc sendCommand(r: TRedis, cmd: string, args: varargs[string]) =
|
||||||
request.add(i & "\c\L")
|
request.add(i & "\c\L")
|
||||||
|
|
||||||
if r.pipeline.enabled:
|
if r.pipeline.enabled:
|
||||||
r.pipeline.buffer[].add(request)
|
r.pipeline.buffer.add(request)
|
||||||
r.pipeline.expected += 1
|
r.pipeline.expected += 1
|
||||||
else:
|
else:
|
||||||
r.socket.send(request)
|
r.socket.send(request)
|
||||||
|
|
@ -237,7 +236,7 @@ proc sendCommand(r: TRedis, cmd: string, arg1: string,
|
||||||
|
|
||||||
if r.pipeline.enabled:
|
if r.pipeline.enabled:
|
||||||
r.pipeline.expected += 1
|
r.pipeline.expected += 1
|
||||||
r.pipeline.buffer[].add(request)
|
r.pipeline.buffer.add(request)
|
||||||
else:
|
else:
|
||||||
r.socket.send(request)
|
r.socket.send(request)
|
||||||
|
|
||||||
|
|
@ -830,9 +829,10 @@ proc exec*(r: TRedis): TRedisList =
|
||||||
# between, then with the results
|
# between, then with the results
|
||||||
return r.flushPipeline(true)
|
return r.flushPipeline(true)
|
||||||
|
|
||||||
|
|
||||||
proc multi*(r: TRedis) =
|
proc multi*(r: TRedis) =
|
||||||
## Mark the start of a transaction block
|
## Mark the start of a transaction block
|
||||||
r.setPipeline(true)
|
r.sePPipeline(true)
|
||||||
r.sendCommand("MULTI")
|
r.sendCommand("MULTI")
|
||||||
raiseNoOK(r.parseStatus())
|
raiseNoOK(r.parseStatus())
|
||||||
|
|
||||||
|
|
@ -1015,10 +1015,10 @@ when false:
|
||||||
echo()
|
echo()
|
||||||
echo("Testing with pipelining.")
|
echo("Testing with pipelining.")
|
||||||
echo()
|
echo()
|
||||||
r.setPipeline(true)
|
r.sePPipeline(true)
|
||||||
r.someTests()
|
r.someTests()
|
||||||
var list = r.flushPipeline()
|
var list = r.flushPipeline()
|
||||||
r.setPipeline(false)
|
r.sePPipeline(false)
|
||||||
echo("-- list length is " & $list.len & " --")
|
echo("-- list length is " & $list.len & " --")
|
||||||
for item in list:
|
for item in list:
|
||||||
if not isNil(item):
|
if not isNil(item):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue