1 В избранное 0 Ответвления 0

OSCHINA-MIRROR/buwangyun-bwsaas

Присоединиться к Gitlife
Откройте для себя и примите участие в публичных проектах с открытым исходным кодом с участием более 10 миллионов разработчиков. Приватные репозитории также полностью бесплатны :)
Присоединиться бесплатно
Клонировать/Скачать
CacheService.php 5 КБ
Копировать Редактировать Исходные данные Просмотреть построчно История
hnlg666 Отправлено 5 лет назад 7545112
<?php
// +----------------------------------------------------------------------
// | Bwsaas
// +----------------------------------------------------------------------
// | Copyright (c) 2015~2020 http://www.buwangyun.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Gitee ( https://gitee.com/buwangyun/bwsaas )
// +----------------------------------------------------------------------
// | Author: buwangyun <hnlg666@163.com>
// +----------------------------------------------------------------------
// | Date: 2020-9-28 10:55:00
// +----------------------------------------------------------------------
namespace buwang\service;
use think\facade\Cache as CacheStatic;
/**
* 缓存类(file缓存 、redis缓存)
* Class CacheService
* @package buwang\service
*/
class CacheService
{
/**
* 标签名
* @var string
*/
protected static $globalCacheName = 'bw_cache_65018';
/**
* 过期时间
* @var int
*/
protected static $expire;
/**
* 获取缓存过期时间
* @param int|null $expire
* @return int
*/
protected static function getExpire(int $expire = null): int
{
if (self::$expire) {
return (int)self::$expire;
}
$expire = !is_null($expire) ? $expire : config('site.cache_time');
if (!is_int($expire))
$expire = (int)$expire;
return self::$expire = $expire;
}
/**
* 写入file缓存
* @param string $name 缓存名称
* @param mixed $value 缓存值
* @param int $expire 缓存时间,为0读取系统缓存时间
* @return bool
*/
public static function set(string $name, $value, int $expire = null): bool
{
return self::handler()->set($name, $value, self::getExpire($expire));
}
/**
* 如果不存在则写入file缓存
* @param string $name
* @param bool $default
* @return mixed
*/
public static function get(string $name, $default = false, int $expire = null)
{
return self::handler()->remember($name, $default, self::getExpire($expire));
}
/**
* 删除file缓存
* @param string $name
* @return bool
* @throws \Psr\SimpleCache\InvalidArgumentException
*/
public static function delete(string $name)
{
return CacheStatic::delete($name);
}
/**
* 缓存句柄
*
* @return \think\cache\TagSet|CacheStatic
*/
public static function handler(?string $cacheName = null)
{
return CacheStatic::tag($cacheName ?: self::$globalCacheName);
}
/**
* 清空缓存池
* @return bool
*/
public static function clear()
{
return self::handler()->clear();
}
/**
* Redis缓存句柄
*
* @return \think\cache\TagSet|CacheStatic
*/
public static function redisHandler(string $type = null)
{
if ($type) {
return CacheStatic::store('redis')->tag($type);
} else {
return CacheStatic::store('redis');
}
}
/**
* 放入令牌桶
* @param string $key
* @param array $value
* @param string $type
* @return bool
*/
public static function setTokenCache($key, $value, $expire = null, string $type = 'user')
{
$key = is_string($key) ? $key : (string)$key;
try {
$redisCahce = self::redisHandler($type);
return $redisCahce->set($key, $value, $expire);
} catch (\Throwable $e) {
return false;
}
}
/**
* 获取token令牌桶
* @param string $key
* @return mixed|null
*/
public static function getTokenCache($key)
{
$key = is_string($key) ? $key : (string)$key;
try {
return self::redisHandler()->get($key, null);
} catch (\Throwable $e) {
return null;
}
}
/**
* 清除令牌桶
* @param string $key
* @return bool
*/
public static function clearToken($key)
{
$key = is_string($key) ? $key : (string)$key;
try {
return self::redisHandler()->delete($key);
} catch (\Throwable $e) {
return false;
}
}
/**
* 清除所有令牌桶
* @param string $type
* @return bool
*/
public static function clearTokenAll($type = 'user')
{
$key = is_string($type) ? $type : (string)$type;
try {
return self::redisHandler($type)->clear();
} catch (\Throwable $e) {
return false;
}
}
/**
* 查看令牌是否存在
* @param string $key
* @return bool
*/
public static function hasToken($key)
{
$key = is_string($key) ? $key : (string)$key;
try {
return self::redisHandler()->has($key);
} catch (\Throwable $e) {
return false;
}
}
}

Опубликовать ( 0 )

Вы можете оставить комментарий после Вход в систему

1
https://gitlife.ru/oschina-mirror/buwangyun-bwsaas.git
git@gitlife.ru:oschina-mirror/buwangyun-bwsaas.git
oschina-mirror
buwangyun-bwsaas
buwangyun-bwsaas
v1.3.2