mirror of
http://124.126.16.154:8888/singularity/hyperf-admin.git
synced 2026-01-15 05:55:08 +08:00
perf: 优化redis的封装
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,3 +1,3 @@
|
||||
.idea
|
||||
composer.lock
|
||||
/vendor/
|
||||
vendor
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||

|
||||

|
||||
|
||||
# docsify
|
||||
|
||||
|
||||
@@ -14,8 +14,8 @@
|
||||
<script src="//unpkg.com/docsify/lib/docsify.min.js"></script>
|
||||
<script>
|
||||
window.$docsify = {
|
||||
logo: '/logo.jpg',
|
||||
name: 'hyperf-admin',
|
||||
//logo: '/logo.png',
|
||||
repo: 'https://github.com/hyperf-admin',
|
||||
// coverpage: true, // 开启封面
|
||||
loadNavbar: true,
|
||||
|
||||
BIN
docs/logo.jpg
Normal file
BIN
docs/logo.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 16 KiB |
BIN
docs/logo.png
BIN
docs/logo.png
Binary file not shown.
|
Before Width: | Height: | Size: 21 KiB |
@@ -29,7 +29,7 @@ class AuthService
|
||||
$expire = $payload['exp'] - time();
|
||||
if($user && $expire > 0) {
|
||||
// 缓存用户信息
|
||||
Redis::setex($cache_key, json_encode($user), $expire);
|
||||
Redis::setex($cache_key, $expire, json_encode($user));
|
||||
}
|
||||
} else {
|
||||
$user = json_decode($user, true);
|
||||
@@ -64,7 +64,7 @@ class AuthService
|
||||
{
|
||||
$user = $this->user();
|
||||
$cache_key = config('user_info_cache_prefix') . md5(json_encode($user));
|
||||
Redis::connection()->del($cache_key);
|
||||
Redis::del($cache_key);
|
||||
Context::set('user_info', null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,7 +50,7 @@ class GlobalConfig
|
||||
}
|
||||
$data = GlobalModel::query()->where('name', $name)->select('value')->first()->toArray();
|
||||
$data = $data ?: $default;
|
||||
Redis::setex($cache_key, json_encode($data, JSON_UNESCAPED_UNICODE), 5 * MINUTE);
|
||||
Redis::setex($cache_key, 5 * MINUTE, json_encode($data, JSON_UNESCAPED_UNICODE));
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ class ModuleProxy
|
||||
public function __construct()
|
||||
{
|
||||
$this->request = request();
|
||||
$this->modules = Redis::getOrSet('hyperf_admin:system_modules', 500, function () {
|
||||
$this->modules = Redis::conn()->getOrSet('hyperf_admin:system_modules', 500, function () {
|
||||
$list = CommonConfig::getConfigByName('website_config')['value']['system_module'];
|
||||
array_change_v2k($list, 'name');
|
||||
return $list;
|
||||
|
||||
@@ -277,7 +277,7 @@ class PermissionService
|
||||
$this->processUserResource($route_collector, $user_id, $auth_type);
|
||||
$dispatch_data = $route_collector->getData();
|
||||
if(!empty($dispatch_data)) {
|
||||
Redis::setex($cache_key, json_encode($dispatch_data), 86400);
|
||||
Redis::setex($cache_key, DAY, json_encode($dispatch_data));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,156 +4,66 @@ namespace HyperfAdmin\BaseUtils\Redis;
|
||||
use Hyperf\Redis\RedisFactory;
|
||||
use HyperfAdmin\BaseUtils\Log;
|
||||
|
||||
/**
|
||||
* @mixin \Redis
|
||||
* @method static get($key)
|
||||
* @method static set($key, $val)
|
||||
* @method static setex($key, $ttl, $val)
|
||||
*/
|
||||
class Redis
|
||||
{
|
||||
private $pool;
|
||||
|
||||
/**
|
||||
* @param string $name redis pool name
|
||||
*
|
||||
* @return \Redis
|
||||
* @var \Redis
|
||||
*/
|
||||
public static function connection($name = 'default')
|
||||
private $redis;
|
||||
|
||||
public function __construct($pool = 'default')
|
||||
{
|
||||
return container(RedisFactory::class)->get($name);
|
||||
$this->pool = $pool;
|
||||
$this->redis = container(RedisFactory::class)->get($pool);
|
||||
}
|
||||
|
||||
public static function set($name, $value, $cluster = 'default')
|
||||
public static function conn($pool = 'default')
|
||||
{
|
||||
$redis = self::connection($cluster);
|
||||
return new self($pool);
|
||||
}
|
||||
|
||||
public function __call($name, $arguments)
|
||||
{
|
||||
if (method_exists($this, $name)) {
|
||||
return $this->$name($arguments);
|
||||
}
|
||||
|
||||
$start_time = microtime(true);
|
||||
$re = $redis->set($name, $value);
|
||||
$ret = $this->redis->$name(...$arguments);
|
||||
$end_time = microtime(true);
|
||||
Log::get('redis')->debug('set', [
|
||||
'cluster' => $cluster,
|
||||
$use_time = round(($end_time - $start_time) * 1000, 2);
|
||||
$level = $use_time > 100 ? 'error' : 'debug';
|
||||
Log::get('redis')->$level('redis call ' . $name, [
|
||||
'cluster' => $this->pool,
|
||||
'key' => $name,
|
||||
'use_time' => $end_time - $start_time,
|
||||
'result' => $re,
|
||||
]);
|
||||
|
||||
return $re;
|
||||
}
|
||||
|
||||
public static function setex($name, $value, $expire, $cluster = 'default')
|
||||
{
|
||||
$redis = self::connection($cluster);
|
||||
$start_time = microtime(true);
|
||||
$re = $redis->setex($name, $expire * 1, $value);
|
||||
$end_time = microtime(true);
|
||||
Log::get('redis')->debug('setex', [
|
||||
'cluster' => $cluster,
|
||||
'key' => $name,
|
||||
'ttl' => $expire,
|
||||
'use_time' => $end_time - $start_time,
|
||||
'result' => $re,
|
||||
]);
|
||||
|
||||
return $re;
|
||||
}
|
||||
|
||||
public static function get($name, $cluster = 'default')
|
||||
{
|
||||
$redis = self::connection($cluster);
|
||||
$start_time = microtime(true);
|
||||
$re = $redis->get($name);
|
||||
$end_time = microtime(true);
|
||||
Log::get('redis')->debug('get', [
|
||||
'cluster' => $cluster,
|
||||
'key' => $name,
|
||||
'use_time' => $end_time - $start_time,
|
||||
'result' => $re,
|
||||
]);
|
||||
|
||||
return $re;
|
||||
}
|
||||
|
||||
public static function exists($name, $cluster = 'default')
|
||||
{
|
||||
$redis = self::connection($cluster);
|
||||
$start_time = microtime(true);
|
||||
$re = $redis->exists($name);
|
||||
$end_time = microtime(true);
|
||||
Log::get('redis')->debug('exist', [
|
||||
'cluster' => $cluster,
|
||||
'key' => $name,
|
||||
'use_time' => $end_time - $start_time,
|
||||
'result' => $re,
|
||||
]);
|
||||
|
||||
return $re;
|
||||
}
|
||||
|
||||
public static function incr($key, $cluster = 'default')
|
||||
{
|
||||
$redis = self::connection($cluster);
|
||||
$start_time = microtime(true);
|
||||
$ret = $redis->incr($key);
|
||||
$end_time = microtime(true);
|
||||
Log::get('redis')->debug('incr', [
|
||||
'cluster' => $cluster,
|
||||
'key' => $key,
|
||||
'use_time' => $end_time - $start_time,
|
||||
'result' => $ret,
|
||||
'use_time' => $use_time,
|
||||
]);
|
||||
|
||||
return $ret;
|
||||
}
|
||||
|
||||
public static function incrBy($key, $value, $cluster = 'default')
|
||||
public static function __callStatic($name, $arguments)
|
||||
{
|
||||
$redis = self::connection($cluster);
|
||||
$start_time = microtime(true);
|
||||
$ret = $redis->incrBy($key, $value);
|
||||
$end_time = microtime(true);
|
||||
Log::get('redis')->debug('incrBy', [
|
||||
'cluster' => $cluster,
|
||||
'key' => $key,
|
||||
'use_time' => $end_time - $start_time,
|
||||
'result' => $ret,
|
||||
]);
|
||||
|
||||
return $ret;
|
||||
return self::conn()->$name(...$arguments);
|
||||
}
|
||||
|
||||
public static function expire($key, $ttl, $cluster = 'default')
|
||||
public function beyondFrequency($key, $duration, $limit)
|
||||
{
|
||||
$redis = self::connection($cluster);
|
||||
$start_time = microtime(true);
|
||||
$ret = $redis->expire($key, $ttl);
|
||||
$end_time = microtime(true);
|
||||
Log::get('redis')->debug('expire', [
|
||||
'cluster' => $cluster,
|
||||
'key' => $key,
|
||||
'use_time' => $end_time - $start_time,
|
||||
'result' => $ret,
|
||||
]);
|
||||
|
||||
return $ret;
|
||||
}
|
||||
|
||||
public static function ttl($key, $cluster = 'default')
|
||||
{
|
||||
$redis = self::connection($cluster);
|
||||
$start_time = microtime(true);
|
||||
$ret = $redis->ttl($key);
|
||||
$end_time = microtime(true);
|
||||
Log::get('redis')->debug('ttl', [
|
||||
'cluster' => $cluster,
|
||||
'key' => $key,
|
||||
'use_time' => $end_time - $start_time,
|
||||
'result' => $ret,
|
||||
]);
|
||||
|
||||
return $ret;
|
||||
}
|
||||
|
||||
public static function beyondFrequency($key, $duration, $limit, $cluster = 'default')
|
||||
{
|
||||
$num = self::incr($key, $cluster);
|
||||
$num = $this->redis->incr($key);
|
||||
if ($num == 1) {
|
||||
self::expire($key, $duration, $cluster);
|
||||
$this->redis->expire($key, $duration);
|
||||
}
|
||||
$ttl = self::ttl($key, $cluster);
|
||||
$ttl = $this->redis->ttl($key);
|
||||
if ($ttl == -1) {
|
||||
self::expire($key, $duration, $cluster);
|
||||
$this->redis->expire($key, $duration);
|
||||
}
|
||||
if ($num > $limit) {
|
||||
return true;
|
||||
@@ -162,52 +72,23 @@ class Redis
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function hGet($key, $hashKey, $cluster = 'default')
|
||||
{
|
||||
$redis = self::connection($cluster);
|
||||
|
||||
return $redis->hGet($key, $hashKey);
|
||||
}
|
||||
|
||||
public static function hSet($key, $hashKey, $value, $cluster = 'default')
|
||||
{
|
||||
$redis = self::connection($cluster);
|
||||
|
||||
return $redis->hSet($key, $hashKey, $value);
|
||||
}
|
||||
|
||||
public static function hGetAll($key, $cluster = 'default')
|
||||
{
|
||||
$redis = self::connection($cluster);
|
||||
|
||||
return $redis->hGetAll($key);
|
||||
}
|
||||
|
||||
public static function hMset($key, $value, $cluster = 'default')
|
||||
{
|
||||
$redis = self::connection($cluster);
|
||||
|
||||
return $redis->hMset($key, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param int $expired
|
||||
* @param mixed $callable
|
||||
* @param string $cluster
|
||||
*
|
||||
* @return array|null
|
||||
*/
|
||||
public static function getOrSet(string $name, int $expired, callable $callable, $cluster = 'default')
|
||||
public function getOrSet(string $name, int $expired, callable $callable)
|
||||
{
|
||||
if(self::exists($name)) {
|
||||
if ($this->redis->exists($name)) {
|
||||
Log::get('redis')->info(sprintf('get %s from cache', $name));
|
||||
|
||||
return json_decode(self::get($name, $cluster), true);
|
||||
return json_decode($this->redis->get($name), true);
|
||||
}
|
||||
$data = call($callable);
|
||||
if ($data) {
|
||||
self::setex($name, json_encode($data), $expired, $cluster);
|
||||
$this->redis->setex($name, $expired, json_encode($data));
|
||||
}
|
||||
|
||||
return $data;
|
||||
|
||||
Reference in New Issue
Block a user