You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.5 KiB
PHTML

2 years ago
<?php
declare(strict_types=1);
namespace App\Helper;
use App\Exception\BusinessException;
use Hyperf\Utils\ApplicationContext;
/**
* 缓存辅助类
* @method static mixed get(string $key, mixed $default = null)
* @method static bool set(string $key, mixed $value, null|int|\DateInterval $ttl = null)
* @method static bool delete(string $key)
* @method static bool clear()
* @method static bool deleteMultiple(iterable $keys)
* @method static iterable getMultiple(iterable $keys, mixed $default = null)
* @method static bool setMultiple(iterable $values, null|int|\DateInterval $ttl = null)
* @method static bool has(string $key)
*/
class Cache
{
private static $instance;
private static $cacheKeys = [
];
public static function getKey($name, array $params = []): string
{
$cacheKey = self::$cacheKeys[$name] ?? null;
if (is_null($cacheKey)) {
throw new BusinessException('缓存不存在');
}
foreach ($params as $key => $value) {
$cacheKey = str_replace('{' . $key . '}', $value, $cacheKey);
}
return $cacheKey;
}
public static function getInstance()
{
if (is_null(self::$instance)) {
self::$instance = ApplicationContext::getContainer()->get(\Psr\SimpleCache\CacheInterface::class);
}
return self::$instance;
}
public static function __callStatic($method, $args)
{
return call_user_func_array([self::getInstance(), $method], $args);
}
}