国产+高潮+在线,国产 av 仑乱内谢,www国产亚洲精品久久,51国产偷自视频区视频,成人午夜精品网站在线观看

分享一個(gè)基于本地文件系統(tǒng)的小型cache緩存函數(shù)

設(shè)想

特點(diǎn)是小巧簡單,適用于一些較小的應(yīng)用,沒有Redis環(huán)境,但又需要用到緩存的情況,如下代碼加入到functions.php即可。

if (!function_exists('cache')) {
    /**
     * 緩存管理
     * @param string $name    緩存名稱
     * @param mixed  $value   緩存值
     * @param int    $expire   緩存有效時(shí)長(秒)
     * @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')) {
                    //啟用數(shù)據(jù)壓縮
                    $content = gzuncompress($content);
                }
                return is_string($content) ? unserialize($content) : null;
            }
        } elseif (is_null($value)) {
            // 刪除緩存
            unlink($filename);
            return false;
        }

        // 緩存數(shù)據(jù)
        $dir = dirname($filename);
        if (!is_dir($dir)) {
            mkdir($dir, 0755, true);
        }
        $data = serialize($value);
        if (function_exists('gzcompress')) {
            //數(shù)據(jù)壓縮
            $data = gzcompress($data, 3);
        }
        $data = "<?php\n//" . sprintf('%012d', $expire) . "\n exit();?>\n" . $data;
        file_put_contents($filename, $data, LOCK_EX);
        return true;
    }
}

使用方法:

// 設(shè)置緩存數(shù)據(jù)
cache('name', $value, 3600);
// 獲取緩存數(shù)據(jù)
print_r(cache('name'));
// 刪除緩存數(shù)據(jù)
cache('name', NULL);

1795 2 10
2個(gè)評(píng)論

喵了個(gè)咪

好東西

  • 暫無評(píng)論
wanyuwei

作了下補(bǔ)充,如果系統(tǒng)有開啟redis擴(kuò)展,自動(dòng)使用redis緩存,未開啟則使用文件緩存

function cache(string $name = null, $value = '', $expire = null)
    {
        if (is_null($name)) {
            return false;
        }
        $md5 = hash('md5', $name);
        if (extension_loaded('redis')) {
            //已安裝Redis擴(kuò)展
            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')) {
                    //啟用數(shù)據(jù)壓縮
                    $content = gzuncompress($content);
                }
                return is_string($content) ? unserialize($content) : null;
            }
        } elseif (is_null($value)) {
            // 刪除緩存
            if (!file_exists($filename)) {
                return false;
            }
            unlink($filename);
            return false;
        }

        // 緩存數(shù)據(jù)
        $dir = dirname($filename);
        if (!is_dir($dir)) {
            mkdir($dir, 0755, true);
        }
        $data = serialize($value);
        if (function_exists('gzcompress')) {
            //數(shù)據(jù)壓縮
            $data = gzcompress($data, 3);
        }
        $data = "<?php\n//" . sprintf('%012d', $expire) . "\n exit();?>\n" . $data;
        file_put_contents($filename, $data, LOCK_EX);
        return true;
    }
  • lsmir2 2024-12-13

    這里可以存到全局變量里 不用每次都去調(diào)用這個(gè)函數(shù),

     if (extension_loaded('redis'))
  • liwenshu 2024-12-18

    大佬如何緩存

  • lsmir2 2024-12-19

    傳參

年代過于久遠(yuǎn),無法發(fā)表評(píng)論

設(shè)想

80
積分
0
獲贊數(shù)
0
粉絲數(shù)
2022-05-13 加入
??