分享一个基于本地文件系统的小型cache缓存函数

古树

特点是小巧简单,适用于一些较小的应用,没有Redis环境,但又需要用到缓存的情况,如下代码加入到functions.php即可。

if (!function_exists('cache')) {
    /**
     * 缓存管理
     * @param string $name    缓存名称
     * @param mixed  $value   缓存值
     * @param int    $expire   缓存有效时长(秒)
     * @return mixed
     */
    function cache(string $name = null, $value = '', $expire = null)
    {
        if (is_null($name)) {
            return false;
        }
        $md5 = hash('md5', $name);
        $filename = runtime_path() . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR . substr($md5, 0, 2) . DIRECTORY_SEPARATOR . substr($md5, 2) . '.php';
        if ('' === $value) {
            // 获取缓存
            if (!file_exists($filename)) {
                return false;
            }
            $content = @file_get_contents($filename);
            if (false !== $content) {
                $expire = (int) substr($content, 8, 12);
                if (0 != $expire && time() - $expire > filemtime($filename)) {
                    //缓存过期删除缓存文件
                    unlink($filename);
                    return false;
                }
                $content = substr($content, 32);
                if (function_exists('gzcompress')) {
                    //启用数据压缩
                    $content = gzuncompress($content);
                }
                return is_string($content) ? unserialize($content) : null;
            }
        } elseif (is_null($value)) {
            // 删除缓存
            unlink($filename);
            return false;
        }

        // 缓存数据
        $dir = dirname($filename);
        if (!is_dir($dir)) {
            mkdir($dir, 0755, true);
        }
        $data = serialize($value);
        if (function_exists('gzcompress')) {
            //数据压缩
            $data = gzcompress($data, 3);
        }
        $data = "<?php\n//" . sprintf('%012d', $expire) . "\n exit();?>\n" . $data;
        file_put_contents($filename, $data, LOCK_EX);
        return true;
    }
}

使用方法:

// 设置缓存数据
cache('name', $value, 3600);
// 获取缓存数据
print_r(cache('name'));
// 删除缓存数据
cache('name', NULL);

859 2 7
2个评论

喵了个咪

好东西

  • 暂无评论
wanyuwei

作了下补充,如果系统有开启redis扩展,自动使用redis缓存,未开启则使用文件缓存

function cache(string $name = null, $value = '', $expire = null)
    {
        if (is_null($name)) {
            return false;
        }
        $md5 = hash('md5', $name);
        if (extension_loaded('redis')) {
            //已安装Redis扩展
            if ('' === $value) {
                // 获取缓存
                if (!\support\Cache::has($name)) {
                    return false;
                }
                $content = \support\Cache::get($name);
                if (false !== $content) {
                    return is_string($content) ? unserialize($content) : null;
                }
            }elseif (is_null($value)) {
                // 删除缓存
                \support\Cache::delete($name);
            }
            $data = serialize($value);
            \support\Cache::set($name,$data,$expire);
            return true;
        }
        $filename = runtime_path() . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR . substr($md5, 0, 2) . DIRECTORY_SEPARATOR . substr($md5, 2) . '.php';
        if ('' === $value) {
            // 获取缓存
            if (!file_exists($filename)) {
                return false;
            }
            $content = @file_get_contents($filename);
            if (false !== $content) {
                $expire = (int) substr($content, 8, 12);
                if (0 != $expire && time() - $expire > filemtime($filename)) {
                    //缓存过期删除缓存文件
                    unlink($filename);
                    return false;
                }
                $content = substr($content, 32);
                if (function_exists('gzcompress')) {
                    //启用数据压缩
                    $content = gzuncompress($content);
                }
                return is_string($content) ? unserialize($content) : null;
            }
        } elseif (is_null($value)) {
            // 删除缓存
            if (!file_exists($filename)) {
                return false;
            }
            unlink($filename);
            return false;
        }

        // 缓存数据
        $dir = dirname($filename);
        if (!is_dir($dir)) {
            mkdir($dir, 0755, true);
        }
        $data = serialize($value);
        if (function_exists('gzcompress')) {
            //数据压缩
            $data = gzcompress($data, 3);
        }
        $data = "<?php\n//" . sprintf('%012d', $expire) . "\n exit();?>\n" . $data;
        file_put_contents($filename, $data, LOCK_EX);
        return true;
    }
  • 暂无评论

古树

60
积分
0
获赞数
0
粉丝数
2022-05-13 加入
🔝