DeTechn Blog

thinkphp快速读取文件数据和保存文件数据

<?php
/**
 * 快速读取文件数据和保存文件数据 针对简单类型数据、字符串、数组.
 *
 * @param string $name  缓存名称
 * @param mixed  $value 缓存值
 * @param string $path  缓存路径
 *
 * @return mixed
 */
function set_config($name,&nbsp;$value = '', $path = DATA_PATH)
{
    static $_cache = array();
    $filename&nbsp;=&nbsp;$path.$name.'.php';
    if ('' !== $value) {
        if (is_null($value)) {
            // 删除缓存
            return false !== strpos($name,&nbsp;&#39;*&#39;)&nbsp;?&nbsp;array_map(&#39;unlink&#39;,&nbsp;glob($filename)) : unlink($filename);
        } else {
            // 缓存数据
            $dir&nbsp;=&nbsp;dirname($filename);
            // 目录不存在则创建
            if (!is_dir($dir)) {
                mkdir($dir, 0755, true);
            }
            $_cache[$name] = $value;

            return file_put_contents($filename,&nbsp;strip_whitespace(&quot;&lt;?php\treturn&nbsp;&quot;.var_export($value, true).';?>'));
        }
    }
    if (isset($_cache[$name])) {
        return $_cache[$name];
    }
    // 获取缓存数据
    if (is_file($filename)) {
        $value =
        include $filename;
        $_cache[$name] = $value;
    } else {
        $value = false;
    }

    return $value;
}
?>

当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »