Skip to content

Commit

Permalink
add multi (#3)
Browse files Browse the repository at this point in the history
* add multi

* fix

* Format code

Co-authored-by: sy-records <[email protected]>
  • Loading branch information
zzss-utils and sy-records authored May 7, 2021
1 parent a19e648 commit 144994d
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion src/BaseRedis.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@ class BaseRedis

protected $connection;

protected $multiOnGoing = false;

public function __construct($config = null, $poolName = 'default')
{
$this->pool = Redis::getInstance($config, $poolName);
}

public function __call($name, $arguments)
{
$this->connection = $this->pool->getConnection();
if (! $this->multiOnGoing) {
$this->connection = $this->pool->getConnection();
}

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

if ($this->multiOnGoing) {
return $this;
}
$this->pool->close($this->connection);

return $data;
Expand Down Expand Up @@ -125,4 +132,32 @@ public function fill()
{
$this->pool->fill();
}

public function multi($mode = \Redis::MULTI)
{
if (! $this->multiOnGoing) {
$this->connection = $this->pool->getConnection();

$this->multiOnGoing = true;

$this->connection->multi($mode);
}

return $this;
}

public function exec()
{
if (! $this->multiOnGoing) {
return;
}

$result = $this->connection->exec();

$this->multiOnGoing = false;

$this->pool->close($this->connection);

return $result;
}
}

0 comments on commit 144994d

Please sign in to comment.