Merge pull request #58 from del-xiong/del_xiong_dev

为MyFileConfig函数添加内存缓存 提高网站运行速度
This commit is contained in:
Devil 2021-08-07 00:39:06 +08:00 committed by GitHub
commit d9a99af3a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -334,6 +334,7 @@ function GetUrlHost($url)
*/
function MyFileConfig($key, $value = '', $default = null, $mandatory = false)
{
static $fileConfigCache = [];
// 目录不存在则创建
$config_dir = ROOT.'runtime'.DS.'data'.DS.'config_data'.DS;
\base\FileUtil::CreateDir($config_dir);
@ -349,6 +350,10 @@ function MyFileConfig($key, $value = '', $default = null, $mandatory = false)
// 读内容
if($value === '')
{
$value = $fileConfigCache[$file] ?? '';
if ($value) {
return $value;
}
$value = file_exists($file) ? unserialize(file_get_contents($file)) : $default;
if($mandatory === true)
{
@ -357,10 +362,11 @@ function MyFileConfig($key, $value = '', $default = null, $mandatory = false)
$value = $default;
}
}
$fileConfigCache[$file] = $value;
return $value;
// 写内容
} else {
$fileConfigCache[$file] = $value;
// 目录是否有可写权限
if(!is_writable($config_dir))
{