Files
kiri-core/kiri-engine/Cache/Redis.php
T

187 lines
3.7 KiB
PHP
Raw Normal View History

2020-08-31 01:27:08 +08:00
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/27 0027
* Time: 11:00
*/
2020-10-29 18:17:25 +08:00
declare(strict_types=1);
2020-08-31 01:27:08 +08:00
2021-08-11 01:04:57 +08:00
namespace Kiri\Cache;
2020-08-31 01:27:08 +08:00
use Exception;
2021-08-11 01:04:57 +08:00
use Kiri\Abstracts\Component;
use Kiri\Abstracts\Config;
use Kiri\Core\Json;
use Kiri\Events\EventProvider;
use Kiri\Exception\ConfigException;
use Kiri\Kiri;
use Kiri\Pool\Redis as PoolRedis;
2022-01-08 18:49:08 +08:00
use Kiri\Annotation\Inject;
2021-12-06 15:54:31 +08:00
use Server\Events\OnWorkerExit;
2022-01-04 00:07:14 +08:00
use Swoole\Timer;
2020-08-31 01:27:08 +08:00
/**
* Class Redis
2021-08-11 01:04:57 +08:00
* @package Kiri\Kiri\Cache
2021-08-03 18:18:09 +08:00
* @mixin \Redis
2020-08-31 01:27:08 +08:00
*/
class Redis extends Component
{
2021-08-02 16:38:50 +08:00
2021-01-04 16:03:10 +08:00
2021-12-06 15:54:31 +08:00
const REDIS_OPTION_HOST = 'host';
const REDIS_OPTION_PORT = 'port';
const REDIS_OPTION_PREFIX = 'prefix';
const REDIS_OPTION_AUTH = 'auth';
const REDIS_OPTION_DATABASES = 'databases';
const REDIS_OPTION_TIMEOUT = 'timeout';
const REDIS_OPTION_POOL = 'pool';
const REDIS_OPTION_POOL_TICK = 'tick';
const REDIS_OPTION_POOL_MIN = 'min';
const REDIS_OPTION_POOL_MAX = 'max';
2021-01-04 16:03:10 +08:00
/**
2021-03-08 18:52:25 +08:00
* @throws ConfigException
2021-04-01 18:35:16 +08:00
* @throws Exception
2021-01-04 16:03:10 +08:00
*/
2021-08-03 18:18:09 +08:00
public function init()
2021-01-04 16:03:10 +08:00
{
2021-12-06 15:54:31 +08:00
$connections = Kiri::getDi()->get(PoolRedis::class);
2021-01-04 16:03:10 +08:00
2021-01-04 16:21:31 +08:00
$config = $this->get_config();
2021-01-04 16:03:10 +08:00
2021-12-06 15:47:12 +08:00
$length = Config::get('cache.redis.pool.max', 10);
2021-08-03 18:18:09 +08:00
$this->eventProvider->on(OnWorkerExit::class, [$this, 'destroy'], 0);
2021-01-04 16:11:43 +08:00
2021-07-05 15:49:37 +08:00
$connections->initConnections('Redis:' . $config['host'], true, $length);
2021-01-04 16:03:10 +08:00
}
2020-08-31 01:27:08 +08:00
/**
* @param $name
* @param $arguments
* @return mixed
* @throws
*/
2020-12-17 14:09:14 +08:00
public function __call($name, $arguments): mixed
2020-08-31 01:27:08 +08:00
{
2021-06-29 12:01:06 +08:00
$time = microtime(true);
2021-06-07 11:43:01 +08:00
if (method_exists($this, $name)) {
$data = $this->{$name}(...$arguments);
2020-09-03 18:10:28 +08:00
} else {
2021-10-25 15:08:18 +08:00
$data = $this->proxy($name, $arguments);
2020-08-31 01:27:08 +08:00
}
2021-06-29 12:01:06 +08:00
if (microtime(true) - $time >= 0.02) {
2021-06-29 14:40:12 +08:00
$this->warning('Redis:' . Json::encode([$name, $arguments]) . (microtime(true) - $time));
2021-06-29 12:01:06 +08:00
}
2020-09-03 18:10:28 +08:00
return $data;
2020-08-31 01:27:08 +08:00
}
2020-09-08 15:19:17 +08:00
2022-01-04 00:07:14 +08:00
/**
* @param $key
* @param int $timeout
* @return bool
*/
public function waite($key, int $timeout = 5): bool
{
$time = time();
while (!$this->setNx($key, 1)) {
2022-01-04 00:07:26 +08:00
if (time()- $time >= $timeout) {
2022-01-04 00:07:14 +08:00
return FALSE;
}
usleep(1000);
}
$this->expire($key, $timeout);
return TRUE;
}
2020-09-08 15:19:17 +08:00
/**
* @param $key
* @param int $timeout
2021-04-01 18:35:16 +08:00
* @return bool|int
2020-09-08 15:19:17 +08:00
* @throws Exception
*/
2021-06-29 12:01:06 +08:00
public function lock($key, int $timeout = 5): bool|int
2020-09-08 15:19:17 +08:00
{
2020-09-08 17:58:54 +08:00
$script = <<<SCRIPT
local _nx = redis.call('setnx',KEYS[1], ARGV[1])
if (_nx ~= 0) then
redis.call('expire',KEYS[1], ARGV[1])
return 1
end
return 0
SCRIPT;
2021-08-03 18:18:09 +08:00
return $this->eval($script, ['{lock}:' . $key, $timeout], 1);
2020-09-08 15:19:17 +08:00
}
/**
* @param $key
* @return int
* @throws Exception
*/
2021-06-07 11:43:01 +08:00
public function unlock($key): int
2020-09-08 15:19:17 +08:00
{
2021-08-03 18:18:09 +08:00
return $this->del('{lock}:' . $key);
2020-09-08 15:19:17 +08:00
}
2020-08-31 01:27:08 +08:00
/**
2021-03-08 18:52:25 +08:00
* @throws ConfigException
2021-04-01 18:35:16 +08:00
* @throws Exception
2020-08-31 01:27:08 +08:00
*/
2021-06-07 11:43:01 +08:00
public function release()
2020-08-31 01:27:08 +08:00
{
2021-12-06 15:54:31 +08:00
$connections = Kiri::getDi()->get(PoolRedis::class);
2020-08-31 01:27:08 +08:00
$connections->release($this->get_config(), true);
}
/**
* 销毁连接池
2020-09-03 18:04:03 +08:00
* @throws ConfigException
2021-02-20 13:20:37 +08:00
* @throws Exception
2020-08-31 01:27:08 +08:00
*/
2021-06-07 11:43:01 +08:00
public function destroy()
2020-08-31 01:27:08 +08:00
{
2021-08-11 01:04:57 +08:00
$connections = Kiri::getDi()->get(PoolRedis::class);
2021-08-17 19:01:37 +08:00
$connections->connection_clear($this->get_config(), true);
2020-08-31 01:27:08 +08:00
}
/**
2021-10-25 15:08:18 +08:00
* @param $name
* @param $arguments
2021-10-25 16:41:15 +08:00
* @return mixed
2021-10-25 15:08:18 +08:00
* @throws ConfigException
2020-08-31 01:27:08 +08:00
* @throws Exception
*/
2021-10-25 16:41:15 +08:00
public function proxy($name, $arguments): mixed
2020-08-31 01:27:08 +08:00
{
2021-12-06 15:54:31 +08:00
$connections = Kiri::getDi()->get(PoolRedis::class);
2020-08-31 01:27:08 +08:00
2021-06-04 18:11:58 +08:00
$config = $this->get_config();
$client = $connections->get($config, true);
2021-08-17 19:16:07 +08:00
if (!($client instanceof Base\Redis)) {
2020-08-31 01:27:08 +08:00
throw new Exception('Redis connections more.');
}
2021-10-25 15:08:18 +08:00
$response = $client->{$name}(...$arguments);
$this->release();
return $response;
2020-08-31 01:27:08 +08:00
}
/**
* @return array
2020-08-31 13:49:40 +08:00
* @throws ConfigException
2020-08-31 01:27:08 +08:00
*/
public function get_config(): array
{
2021-06-04 18:11:58 +08:00
return Config::get('cache.redis', null, true);
2020-08-31 01:27:08 +08:00
}
}