This commit is contained in:
2025-08-13 19:50:06 +08:00
parent 371eed25ca
commit 537426593a
4 changed files with 499 additions and 146 deletions

View File

@@ -1,6 +1,7 @@
<?php
// 应用公共文件
use support\think\Cache;
if (!function_exists('__')) {
@@ -420,6 +421,23 @@ if (!function_exists('set_timezone')) {
}
}
if (!function_exists('env')) {
/**
* 获取环境变量值
* @access public
* @param string $name 环境变量名(支持二级 .号分割)
* @param string $default 默认值
* @return mixed
*/
function env(?string $name = null, $default = null)
{
return getenv($name, $default);
}
}
if (!function_exists('get_upload_config')) {
/**
@@ -880,7 +898,43 @@ function runWithLock(string $key, int $expire, \Closure $callback)
release($key, $lockValue);
}
}
if (!function_exists('cache')) {
/**
* 缓存管理
* @param string $name 缓存名称
* @param mixed $value 缓存值
* @param mixed $options 缓存参数
* @param string $tag 缓存标签
* @return mixed
*/
function cache(?string $name = null, $value = '', $options = null, $tag = null)
{
if (is_null($name)) {
return app('cache');
}
if ('' === $value) {
// 获取缓存
return str_starts_with($name, '?') ? Cache::has(substr($name, 1)) : Cache::get($name);
} elseif (is_null($value)) {
// 删除缓存
return Cache::delete($name);
}
// 缓存数据
if (is_array($options)) {
$expire = $options['expire'] ?? null; //修复查询缓存无法设置过期时间
} else {
$expire = $options;
}
if (is_null($tag)) {
return Cache::set($name, $value, $expire);
} else {
return Cache::tag($tag)->set($name, $value, $expire);
}
}
}
/**
* 解锁用Lua防止误删
*/