首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 綜合知識

phpGuru Cache 類別:資料與輸出快取實作

     編輯:PHP 綜合知識

前言

快取對效能至關重要,許多開發者過度依賴框架而忽略基礎。本文介紹 phpGuru 開源的 Cache 類別,採用共享記憶體(/dev/shm)儲存,所有方法皆為靜態,使用簡單。

授權說明

採用 phpGuru 授權,商業用途需付費,慈善/教育機構有優惠,詳見 官網。

類別架構

  • Cache:基底類別,處理檔案讀寫、快取判斷、路徑管理。
  • OutputCache:繼承 Cache,提供輸出緩衝區快取(Start()/End())。
  • DataCache:繼承 Cache,提供資料序列化快取(Get()/Put())。

核心程式碼節錄

class Cache {
    public static $enabled = true;
    protected static $store = '/dev/shm/';
    protected static $prefix = 'cache_';

    protected static function write($group, $id, $ttl, $data) {
        $filename = self::getFilename($group, $id);
        if ($fp = @fopen($filename, 'xb')) {
            if (flock($fp, LOCK_EX)) fwrite($fp, $data);
            fclose($fp);
            touch($filename, time() + $ttl);
        }
    }
    protected static function read($group, $id) {
        return file_get_contents(self::getFilename($group, $id));
    }
    protected static function isCached($group, $id) {
        $filename = self::getFilename($group, $id);
        if (self::$enabled && file_exists($filename) && filemtime($filename) > time()) return true;
        @unlink($filename);
        return false;
    }
    protected static function getFilename($group, $id) {
        $id = md5($id);
        return self::$store . self::$prefix . "{$group}_{$id}";
    }
    public static function setPrefix($prefix) { self::$prefix = $prefix; }
    public static function setStore($store) { self::$store = $store; }
}

class OutputCache extends Cache {
    private static $group, $id, $ttl;
    public static function Start($group, $id, $ttl) {
        if (self::isCached($group, $id)) { echo self::read($group, $id); return true; }
        else { ob_start(); self::$group=$group; self::$id=$id; self::$ttl=$ttl; return false; }
    }
    public static function End() {
        $data = ob_get_contents(); ob_end_flush();
        self::write(self::$group, self::$id, self::$ttl, $data);
    }
}

class DataCache extends Cache {
    public static function Get($group, $id) {
        if (self::isCached($group, $id)) return unserialize(self::read($group, $id));
        return null;
    }
    public static function Put($group, $id, $ttl, $data) {
        self::write($group, $id, $ttl, serialize($data));
    }
}

使用範例

輸出快取

if (!OutputCache::Start('group', 'unique_id', 600)) {
    // ... 輸出內容 ...
    OutputCache::End();
}

資料快取

if (!$data = DataCache::Get('group', 'unique_id')) {
    $data = time(); // 取得資料
    DataCache::Put('group', 'unique_id', 10, $data);
}
echo $data;

補充:快取清理腳本

$dir = !empty($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : '.';
$dh = opendir($dir);
while ($filename = readdir($dh)) {
    if ($filename == '.' || $filename == '..') continue;
    if (filemtime($dir . DIRECTORY_SEPARATOR . $filename) < time()) {
        unlink($dir . DIRECTORY_SEPARATOR . $filename);
    }
}
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved