Skip to content

Commit 144994d

Browse files
add multi (#3)
* add multi * fix * Format code Co-authored-by: sy-records <[email protected]>
1 parent a19e648 commit 144994d

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

src/BaseRedis.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,18 @@ class BaseRedis
1616

1717
protected $connection;
1818

19+
protected $multiOnGoing = false;
20+
1921
public function __construct($config = null, $poolName = 'default')
2022
{
2123
$this->pool = Redis::getInstance($config, $poolName);
2224
}
2325

2426
public function __call($name, $arguments)
2527
{
26-
$this->connection = $this->pool->getConnection();
28+
if (! $this->multiOnGoing) {
29+
$this->connection = $this->pool->getConnection();
30+
}
2731

2832
try {
2933
$data = $this->connection->{$name}(...$arguments);
@@ -32,6 +36,9 @@ public function __call($name, $arguments)
3236
throw $e;
3337
}
3438

39+
if ($this->multiOnGoing) {
40+
return $this;
41+
}
3542
$this->pool->close($this->connection);
3643

3744
return $data;
@@ -125,4 +132,32 @@ public function fill()
125132
{
126133
$this->pool->fill();
127134
}
135+
136+
public function multi($mode = \Redis::MULTI)
137+
{
138+
if (! $this->multiOnGoing) {
139+
$this->connection = $this->pool->getConnection();
140+
141+
$this->multiOnGoing = true;
142+
143+
$this->connection->multi($mode);
144+
}
145+
146+
return $this;
147+
}
148+
149+
public function exec()
150+
{
151+
if (! $this->multiOnGoing) {
152+
return;
153+
}
154+
155+
$result = $this->connection->exec();
156+
157+
$this->multiOnGoing = false;
158+
159+
$this->pool->close($this->connection);
160+
161+
return $result;
162+
}
128163
}

0 commit comments

Comments
 (0)