wecenter學習筆記-Cache的實現(xiàn)原理

該文是wecenter學習筆記的一部分

Cache的實現(xiàn)原理

wecenter支持兩種緩存

  • Memcached
  • File

基于Zend_Cache實現(xiàn)的文件緩存機制。

system/core/cache.php

$this->cache_factory = Zend_Cache::factory($this->frontendName, $this->backendName, $this->frontendOptions, $this->backendOptions);

讀緩存

$result = $this->cache_factory->load($this->cachePrefix . $key);

寫緩存

$result = $this->cache_factory->save($value, $this->cachePrefix . $key, array(), $lifetime);

刪除緩存

return $this->cache_factory->remove($key);

清理緩存

return $this->cache_factory->clean(Zend_Cache::CLEANING_MODE_ALL);

Zend Cache

為了方便使用,Zend Cache根據(jù)緩存的數(shù)據(jù)類型構(gòu)造了各種 FrontCache,并根據(jù)存儲類型引入了BackendCache的概念。

** Frontend **

  • Capture

    Capture能將php的start和flush之間的輸出緩存起來,內(nèi)部通過ob_start注冊的output_callback來將php的緩沖內(nèi)容存儲到緩存中。

    Zend/Cache/Frontend/Capture.php#start

    ob_start(array($this, '_flush'));
    ob_implicit_flush(false);
    

    通過調(diào)用ob_flush來刷新緩沖區(qū)

  • Page

    捕獲輸出的部分內(nèi)容

  • Output

    使用方式

    // if it is a cache miss, output buffering is triggered
    if (! $cache->start('mypage')) {
       // output everything as usual
       echo 'Hello world! ';
       echo 'This is cached ('.time().') ';
       $cache->end(); // output buffering ends
    }
    echo 'This is never cached ('.time().').';
    
  • Class

    通過重寫__call,代理目標對象的函數(shù)調(diào)用,并將函數(shù)調(diào)用結(jié)果緩存起來。

    public function __call($name, $parameters)
    {
        $callback = array($this->_cachedEntity, $name);
    
        if (!is_callable($callback, false)) {
            Zend_Cache::throwException('Invalid callback');
        }
    
        $cacheBool1 = $this->_specificOptions['cache_by_default'];
        $cacheBool2 = in_array($name, $this->_specificOptions['cached_methods']);
        $cacheBool3 = in_array($name, $this->_specificOptions['non_cached_methods']);
        $cache      = (($cacheBool1 || $cacheBool2) && (!$cacheBool3));
    
        if (!$cache) {
            // We do not have not cache
            return call_user_func_array($callback, $parameters);
        }
    
        $id = $this->makeId($name, $parameters);
        if (($rs = $this->load($id)) && (array_key_exists(0, $rs))
            && (array_key_exists(1, $rs))
        ) {
            // A cache is available
            $output = $rs[0];
            $return = $rs[1];
        } else {
            // A cache is not available (or not valid for this frontend)
            ob_start();
            ob_implicit_flush(false);
    
            try {
                $return = call_user_func_array($callback, $parameters);
                $output = ob_get_clean();
                $data   = array($output, $return);
    
                $this->save(
                    $data, $id, $this->_tags, $this->_specificLifetime,
                    $this->_priority
                );
            } catch (Exception $e) {
                ob_end_clean();
                throw $e;
            }
        }
    
        echo $output;
        return $return;
    }
    
  • Function

    緩存函數(shù)調(diào)用的結(jié)果

    function call($callback, array $parameters = array(), $tags = array(), $specificLifetime = false, $priority = 8)
    

    調(diào)用這個函數(shù),會跟進函數(shù)名稱和參數(shù)生成緩存主鍵,如果緩存有效,則直接返回緩存結(jié)果,否則調(diào)用函數(shù)并進行緩存,與classs的__call實現(xiàn)基本一致。

  • File

    根據(jù)文件的修改狀態(tài)來判斷緩存是否有效。

** Backend **

需要符合基本的接口

Zend/Cache/Backend/Interface.php

public function setDirectives($directives);
public function load($id, $doNotTestCacheValidity = false);
public function test($id);
public function save($data, $id, $tags = array(), $specificLifetime = false);
public function remove($id);
public function clean($mode = Zend_Cache::CLEANING_MODE_ALL, $tags = array());
  • File

    基于文件系統(tǒng)的緩存。

    每個緩存除了生存緩存文件外,還會生出一個文件名以internal-metadatas---開頭的元數(shù)據(jù)文件,并依靠元數(shù)據(jù)文件來存儲數(shù)據(jù)的基本信息:

    Zend/Cache/Backend/File.php#touch

    'hash' => $metadatas['hash'],
    'mtime' => time(),
    'expire' => $metadatas['expire'] + $extraLifetime,
    'tags' => $metadatas['tags']
    
  • Sqlite

    將數(shù)據(jù)和元數(shù)據(jù)存儲到sqlite中

    Zend/Cache/Backend/Sqlite.php#_buildStructure

        private function _buildStructure()
    {
        $this->_query('DROP INDEX tag_id_index');
        $this->_query('DROP INDEX tag_name_index');
        $this->_query('DROP INDEX cache_id_expire_index');
        $this->_query('DROP TABLE version');
        $this->_query('DROP TABLE cache');
        $this->_query('DROP TABLE tag');
        $this->_query('CREATE TABLE version (num INTEGER PRIMARY KEY)');
        $this->_query('CREATE TABLE cache (id TEXT PRIMARY KEY, content BLOB, lastModified INTEGER, expire INTEGER)');
        $this->_query('CREATE TABLE tag (name TEXT, id TEXT)');
        $this->_query('CREATE INDEX tag_id_index ON tag(id)');
        $this->_query('CREATE INDEX tag_name_index ON tag(name)');
        $this->_query('CREATE INDEX cache_id_expire_index ON cache(id, expire)');
        $this->_query('INSERT INTO version (num) VALUES (1)');
    }
    

    顧名思義,cache存儲緩存數(shù)據(jù),tag存儲標簽, version存儲緩存的版本號。

    每次執(zhí)行操作之前都會調(diào)用 _checkAndBuildStructure 檢查是否需要構(gòu)建表結(jié)構(gòu)

  • memcached

    依賴Memcache客戶端來提供服務。

  • libmemcached

    依賴Memcached來提供服務

  • xcache

    來自于lightd團隊的開源緩存服務器

  • Apc

  • BlackHole

  • Static

  • TwoLevels

  • WinCache

    wincache僅支持NTS(非線程安全版本)的PHP

  • ZendServer Disk

  • ZendServer Share memeory

  • ZendPlatform

涉及的緩存服務太多,不能一一探究。


插件機制 ←o→ 配置參數(shù)管理

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,534評論 19 139
  • NAME dnsmasq - A lightweight DHCP and caching DNS server....
    ximitc閱讀 2,986評論 0 0
  • 轉(zhuǎn)至元數(shù)據(jù)結(jié)尾創(chuàng)建: 董瀟偉,最新修改于: 十二月 23, 2016 轉(zhuǎn)至元數(shù)據(jù)起始第一章:isa和Class一....
    40c0490e5268閱讀 2,041評論 0 9
  • 1、判斷一個變量是否存在的函數(shù):isset判斷一個變量是否為null的函數(shù):is_null判斷一個變量是否為空的函...
    jianghu000閱讀 1,800評論 1 4
  • PHP頁面緩存實現(xiàn)的方法總結(jié) 在php頁面緩存主要用到的是ob系列函數(shù): ob_start():頁面緩存開始的標志...
    桖辶殤閱讀 1,359評論 0 0

友情鏈接更多精彩內(nèi)容