-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
378 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,28 +4,30 @@ | |
|
||
use Micro\Plugin\Redis\Configuration\ClientOptionsConfigurationInterface; | ||
use Micro\Plugin\Redis\Configuration\RedisClientConfigurationInterface; | ||
use Micro\Plugin\Redis\Redis\RedisInterface; | ||
use Micro\Plugin\Redis\RedisPluginConfigurationInterface; | ||
use \Redis; | ||
use Redis; | ||
|
||
/** | ||
* @author Stanislau Komar <[email protected]> | ||
*/ | ||
class RedisBuilder implements RedisBuilderInterface | ||
{ | ||
/** | ||
* @param RedisPluginConfigurationInterface $pluginConfiguration | ||
* @param RedisFactoryInterface $redisFactory | ||
*/ | ||
public function __construct( | ||
private RedisPluginConfigurationInterface $pluginConfiguration, | ||
private RedisFactoryInterface $redisFactory | ||
private readonly RedisPluginConfigurationInterface $pluginConfiguration, | ||
private readonly RedisFactoryInterface $redisFactory | ||
) | ||
{ | ||
} | ||
|
||
/** | ||
* @param string $redisAlias | ||
* | ||
* @return Redis | ||
* {@inheritDoc} | ||
*/ | ||
public function create(string $redisAlias): Redis | ||
public function create(string $redisAlias): RedisInterface | ||
{ | ||
$clientConfiguration = $this->pluginConfiguration->getClientConfiguration($redisAlias); | ||
$redis = $this->redisFactory->create(); | ||
|
@@ -36,11 +38,12 @@ public function create(string $redisAlias): Redis | |
} | ||
|
||
/** | ||
* @param Redis $redis | ||
* @param RedisInterface $redis | ||
* @param RedisClientConfigurationInterface $configuration | ||
* | ||
* @return void | ||
*/ | ||
protected function initialize(Redis $redis, RedisClientConfigurationInterface $configuration): void | ||
protected function initialize(RedisInterface $redis, RedisClientConfigurationInterface $configuration): void | ||
{ | ||
$connectionMethod = $this->getConnectionMethod($configuration); | ||
|
||
|
@@ -63,18 +66,20 @@ protected function initialize(Redis $redis, RedisClientConfigurationInterface $c | |
} | ||
|
||
/** | ||
* @param Redis $redis | ||
* @param RedisInterface $redis | ||
* @param ClientOptionsConfigurationInterface $configuration | ||
* | ||
* @return void | ||
*/ | ||
protected function setOptions(Redis $redis, ClientOptionsConfigurationInterface $configuration): void | ||
protected function setOptions(RedisInterface $redis, ClientOptionsConfigurationInterface $configuration): void | ||
{ | ||
$redis->setOption(Redis::OPT_SERIALIZER, $this->getRedisOptionValue($configuration->serializer())); | ||
$redis->setOption(Redis::OPT_PREFIX, $configuration->prefix()); | ||
} | ||
|
||
/** | ||
* @param string $redisOption | ||
* | ||
* @return int | ||
*/ | ||
protected function getRedisOptionValue(string $redisOption): int | ||
|
@@ -84,6 +89,7 @@ protected function getRedisOptionValue(string $redisOption): int | |
|
||
/** | ||
* @param RedisClientConfigurationInterface $configuration | ||
* | ||
* @return string | ||
*/ | ||
protected function getConnectionMethod(RedisClientConfigurationInterface $configuration): string | ||
|
@@ -93,6 +99,7 @@ protected function getConnectionMethod(RedisClientConfigurationInterface $config | |
|
||
/** | ||
* @param RedisClientConfigurationInterface $configuration | ||
* | ||
* @return string|null | ||
*/ | ||
protected function getPersistentId(RedisClientConfigurationInterface $configuration): ?string | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
<?php | ||
|
||
namespace Micro\Plugin\Redis\Redis\Decorator; | ||
|
||
|
||
use Micro\Plugin\Redis\Redis\RedisInterface; | ||
|
||
class BaseRedisDecorator implements RedisInterface | ||
{ | ||
/** | ||
* @param RedisInterface $redis | ||
*/ | ||
public function __construct(private readonly \Redis $redis) | ||
{ | ||
} | ||
|
||
/** | ||
* @param string $host can be a host, or the path to a unix domain socket | ||
* @param int $port optional | ||
* @param float $timeout value in seconds (optional, default is 0 meaning unlimited) | ||
* @param string|null $persistentId identity for the requested persistent connection | ||
* @param int $retryInterval retry interval in milliseconds. | ||
* @param float $readTimeout value in seconds (optional, default is 0 meaning unlimited) | ||
* | ||
* @return bool | ||
*/ | ||
public function connect(string $host, | ||
int $port = 6379, | ||
float $timeout = 0.0, | ||
string $persistentId = null, | ||
int $retryInterval = 0, | ||
float $readTimeout = 0.0): bool | ||
{ | ||
return $this->redis->connect( | ||
$host, | ||
$port, | ||
$timeout, | ||
$persistentId, | ||
$retryInterval, | ||
$readTimeout, | ||
); | ||
} | ||
|
||
/** | ||
* @param string $host can be a host, or the path to a unix domain socket | ||
* @param int $port optional | ||
* @param float $timeout value in seconds (optional, default is 0 meaning unlimited) | ||
* @param string|null $persistentId identity for the requested persistent connection | ||
* @param int $retryInterval retry interval in milliseconds. | ||
* @param float $readTimeout value in seconds (optional, default is 0 meaning unlimited) | ||
* | ||
* @return bool | ||
*/ | ||
public function pconnect(string $host, | ||
int $port = 6379, | ||
float $timeout = 0.0, | ||
string $persistentId = null, | ||
int $retryInterval = 0, | ||
float $readTimeout = 0.0): bool | ||
{ | ||
return $this->redis->pconnect( | ||
$host, | ||
$port, | ||
$timeout, | ||
$persistentId, | ||
$retryInterval, | ||
$readTimeout | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function subscribe(array $channels, array|string|callable $callback): mixed | ||
{ | ||
return $this->redis->subscribe($channels, $callback); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function unsubscribe(array $channels): mixed | ||
{ | ||
return $this->prepareReturnResult($this->redis->rawCommand('UNSUBSCRIBE', '')); | ||
//return $this->redis->unsubscribe($channels); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function publish(string $channel, string $message): int|RedisInterface | ||
{ | ||
return $this->prepareReturnResult($this->redis->publish($channel, $message)); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function pubsub(string $keyword, array|string $argument): array|int|RedisInterface | ||
{ | ||
return $this->prepareReturnResult($this->redis->pubsub($keyword, $argument)); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function setex(string $key, int $expire, mixed $value): bool|RedisInterface | ||
{ | ||
return $this->prepareReturnResult($this->redis->setex($key, $expire, $value)); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function setOption(int $option, mixed $value): bool | ||
{ | ||
return $this->redis->setOption($option, $value); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function set(string $key, mixed $data, int $timeout = null): bool|self | ||
{ | ||
return $this->prepareReturnResult($this->redis->set($key, $data, $timeout)); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function del($key1, ...$otherKeys): int|self | ||
{ | ||
return $this->prepareReturnResult($this->redis->del($key1, ...$otherKeys)); | ||
} | ||
|
||
/** | ||
* @param mixed $result | ||
* @return mixed | ||
*/ | ||
protected function prepareReturnResult(mixed $result): mixed | ||
{ | ||
if(!$result) { | ||
return $result; | ||
} | ||
|
||
if($result instanceof \Redis) { | ||
return $this; | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
/** | ||
* @param string $name | ||
* @param array $arguments | ||
* | ||
* @return mixed | ||
*/ | ||
public function __call(string $name, array $arguments): mixed | ||
{ | ||
if(!method_exists($this->redis, $name)) { | ||
throw new \BadMethodCallException(sprintf( | ||
'Method "%s" is not exists in the "%s"', | ||
$name, get_class($this->redis) | ||
)); | ||
} | ||
|
||
return $this->prepareReturnResult($this->redis->{$name}(...$arguments)); | ||
} | ||
} |
Oops, something went wrong.