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

173 lines
3.4 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
namespace Snowflake\Cache;
use Exception;
2021-03-08 18:52:25 +08:00
use ReflectionException;
2020-08-31 01:27:08 +08:00
use Snowflake\Abstracts\Component;
2020-09-04 01:05:33 +08:00
use Snowflake\Abstracts\Config;
2020-08-31 01:27:08 +08:00
use Snowflake\Event;
2021-01-12 18:13:02 +08:00
use Snowflake\Exception\ComponentException;
2020-08-31 13:49:40 +08:00
use Snowflake\Exception\ConfigException;
2021-03-08 18:52:25 +08:00
use Snowflake\Exception\NotFindClassException;
2020-08-31 01:27:08 +08:00
use Snowflake\Snowflake;
/**
* Class Redis
2020-08-31 12:38:32 +08:00
* @package Snowflake\Snowflake\Cache
2020-08-31 01:27:08 +08:00
* @see \Redis
*/
class Redis extends Component
{
2020-10-29 18:17:25 +08:00
public string $host = '127.0.0.1';
public string $auth = 'xl.2005113426';
public int $port = 6973;
public int $databases = 0;
public int $timeout = -1;
public string $prefix = 'idd';
2020-08-31 01:27:08 +08:00
/**
* @throws Exception
*/
public function init()
{
2021-02-20 13:02:58 +08:00
$event = Snowflake::app()->getEvent();
2021-02-20 15:21:42 +08:00
$event->on(Event::SYSTEM_RESOURCE_CLEAN, [$this, 'destroy']);
$event->on(Event::SYSTEM_RESOURCE_RELEASES, [$this, 'release']);
2021-01-04 16:03:10 +08:00
$event->on(Event::SERVER_WORKER_START, [$this, 'createPool']);
2020-08-31 01:27:08 +08:00
}
2021-01-04 16:03:10 +08:00
/**
2021-02-20 13:02:58 +08:00
* @throws ComponentException
2021-03-08 18:52:25 +08:00
* @throws ConfigException
* @throws ReflectionException
* @throws NotFindClassException
2021-01-04 16:03:10 +08:00
*/
public function createPool()
{
2021-03-08 18:52:25 +08:00
$connections = Snowflake::app()->getRedisFromPool();
2021-01-04 16:03:10 +08:00
2021-01-04 16:21:31 +08:00
$config = $this->get_config();
$name = $config['host'] . ':' . $config['prefix'] . ':' . $config['databases'];
2021-01-04 16:03:10 +08:00
2021-01-04 16:21:31 +08:00
$length = env('REDIS.POOL_LENGTH', 100);
2021-01-04 16:11:43 +08:00
2021-02-20 13:35:17 +08:00
$connections->initConnections('redis', 'redis:' . $name, 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
{
2020-09-03 18:10:28 +08:00
if (method_exists($this, $name)) {
$data = $this->{$name}(...$arguments);
} else {
$data = $this->proxy()->{$name}(...$arguments);
2020-08-31 01:27:08 +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
/**
* @param $key
* @param int $timeout
* @return bool
* @throws Exception
*/
2021-03-29 15:25:39 +08:00
public function lock($key, $timeout = 5): bool
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;
return $this->proxy()->eval($script, [$key, $timeout], 1);
2020-09-08 15:19:17 +08:00
}
/**
* @param $key
* @return int
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function unlock($key): int
2020-09-08 15:19:17 +08:00
{
$redis = $this->proxy();
return $redis->del($key);
}
2020-08-31 01:27:08 +08:00
/**
2021-02-20 13:02:58 +08:00
* @throws ComponentException
2021-03-08 18:52:25 +08:00
* @throws ConfigException
* @throws NotFindClassException
* @throws ReflectionException
2020-08-31 01:27:08 +08:00
*/
public function release()
{
2021-03-08 18:52:25 +08:00
$connections = Snowflake::app()->getRedisFromPool();
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-01-12 18:13:02 +08:00
* @throws ComponentException
2021-02-20 13:20:37 +08:00
* @throws Exception
2020-08-31 01:27:08 +08:00
*/
public function destroy()
{
2021-03-08 18:52:25 +08:00
$connections = Snowflake::app()->getRedisFromPool();
2020-08-31 01:27:08 +08:00
$connections->destroy($this->get_config(), true);
}
/**
* @return \Redis
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function proxy(): \Redis
2020-08-31 01:27:08 +08:00
{
2021-03-08 18:52:25 +08:00
$connections = Snowflake::app()->getRedisFromPool();
2020-08-31 01:27:08 +08:00
2021-02-23 16:56:34 +08:00
$client = $connections->get($this->get_config(), true);
2020-08-31 01:27:08 +08:00
if (!($client instanceof \Redis)) {
throw new Exception('Redis connections more.');
}
return $client;
}
/**
* @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
{
2020-09-17 13:56:57 +08:00
return Config::get('cache.redis', false, [
2020-08-31 13:49:40 +08:00
'host' => '127.0.0.1',
'port' => '6379',
'prefix' => Config::get('id'),
'auth' => '',
'databases' => '0',
'read_timeout' => -1,
2020-09-17 13:56:57 +08:00
'timeout' => -1,
2020-08-31 13:49:40 +08:00
]);
2020-08-31 01:27:08 +08:00
}
}