PHP比較全面的快取類

巴扎黑
發布: 2016-11-24 15:12:04
原創
1075 人瀏覽過

/*

 * 姓名:    wrapperCache

 * 備註:   fileCache、memcache/memcached、APC、Xcache 和eacheelsator 的包裝緩存, array (array('host'=>'localhost')));

echo $cacheObj->cache('key','value');

*/

classwrapperCache {

3

    const CACHE_TYPE_AUTO           = '自動';


    const CACHE_TYPE_EACCELERATOR  = 'eaccelerator';   = ' apc';

    const CACHE_TYPE_MEMCACHE       = 'memcache';

    const CACHE_TYPE_MECHEC = 'filecache';

    const CACHE_TYPE_XCACHE         = 'xcache';

   $cache_expire;   //快取過期的秒數

    private $cache_type;

    / / 總是只回傳一個實例

    public static function getInstance($type=self:: CACHE_TYPE_AUTO, $exp_time=3600, $params='cache/'){

. { //不存在這個實例

            self::$instance = new self( $型,$exp_time,$params); //進入建構子

     }

    //快取建構函數,選用過期時間與快取路徑

    private function __construct($type, $exp_time, $ params) {

50h this->cache_params = $params;

        $this->setCacheType($type);

    }

   公共職能

    }


// 阻止使用者複製實例

    public function __clone(){

        $this->cacheError ('不允許複製。 ');

    }

    //從資料夾中刪除快取

    public functionclearCache(){

_pide 🠎)      case self::CACHE _TYPE_EACCELERATOR:

                eaccelerator_clean( );

                eaccelerator_clear();

             APC :

                apc_clear_cache('user');


           self::CACHE_TYPE_XCACHE :

                xcache_clear_cache(XC_TYPE_VAR , 0);

                中斷;

       $this->cache_external->flush();


                中斷;

           $ this->cache_external->flush();

                中斷;

         $this->cache_external->deleteCache();

                break;

 


    //寫入或讀取快取

    public function cache($key, $value = '', $ttl = '') {

        if ($value != '') { //想寫

            if ($ttl == '') $ttl = $this->cache_expire; ttl);

        } else return $this->get($key);

        //讀取值

    }

// 快取一個使用給新設定的快取檔案,的名稱,要儲存的資訊/值的資料

    private function put($key, $data, $ttl = '') {


        if ($ttl == '') $ttl = $this-> ;cache__m ;

        switch($this->cache_type){

            case self::CACHE_TYPE_EACCELERATOR :


                eaccelerator_put($key, serialize($data), $ ttl);

                休息;

            case self::CACHE_TYPE_APC :

                apc_store($key, $data, $ttl);


            case self::CACHE_TYPE_XCACHE :

                xcache_set($key, 序列化($data), $ttl );

                中斷;


            c data=serialize($data);

               $this->cache_external->set($key, $data, false , $ttl) ;

                中斷;


          case S ize($data);

               $this->cache_external->set($key, $data, $ttl);

中斷;

            case self::CACHE_TYPE_FILE :


                $this->cache_external->cache($key,$data);

                休息;

        }

    }


    //返回給定鍵的快取

    private function get($key){

        switch($this->cache_type){

             $data = 反序列化(eaccelerator_get ($key));

                中斷;


          case self::CACHE_TYPE_APC :        case self::CACHE_TYPE_APC :    ($key);

             中斷;

              unserialize(xcache_get($ key));

                中斷;

          = unserialize( $this->cache_external->get($key));

                中斷;

    中斷;    self::CACHE_TYPE_MEMCACHED :

                $data = unserialize($this->cache_external->get($key)); 🠎🠎)


            個案自我:: CACHE_TYPE_FILE :

                $data = $this-> cache_external->cache($key);

                中斷;

        }

       
    //從快取中刪除金鑰

    公用函數刪除($key){

switch($this->cache_type){

            case self::CACHE_TYPE_EACCELERATOR :

         );

                中斷;


            case  key);

               中斷;


            c _unset($key);

                中斷;

            case self::CACHE_TYPE_MEMCACHE :


               中斷;

            case self::CACHE_TYPE_MEMCACHED :

($key);


                中斷;

          $this->cache_external->delete($key );

                休息;


       // 重載應用程式變數並自動快取

    public function __set($name, $value) {

        $this->put($name, $value, $this->cache_expire );

    公用函數__get($name) {

        return $this->get($name);

    }

key嗎? unset ($name) {/ /echo 「Unsetting '$name'n";

        $this->delete($name);

    }

           return $this- >cache_type;

    }


    // 設定緩存,若未安裝則觸發錯誤

    public function setCacheType($type)

    public function setCacheType($type) .


      switch($this->cache_type){

            case self::CACHE_TYPE_EACCELERATOR :      ists('eaccelerator_get') $this ->cacheError('eaccelerator 不找到');

                中斷;

            case self::CACHE_TYPE_APC :

                if (

                中斷;

            case self:: CACHE_TYPE_XCACHE :

                if (function_exists('xcache_get')) $this->cacheError('未找到Xcache');🠎;


            案例self::CACHE_TYPE_MEMCACHE :

                if (Meclass_ex if' )) $this->init_mem();

                else $this->cacheError('memcache 找不到');

            個案自我:: CACHE_TYPE_MEMCACHED :

                if (class_exists('Memcached' )) $this->init_mem(true);

              else $this->cacheError('memcached 不找到');

          案例self::CACHE_TYPE_FILE :

                if (class_exists('fileCache '))$this->init_filecache();

                else $this->cacheError('fileCache 找不到');

               break;


            case  unction_exists('eaccelerator_get'))    $this->cache _type = self::CACHE_TYPE_EACCELERATOR;

            . 'apc_fetch'))       $this->cache_type = self::CACHE_TYPE_APC ;

                elseif (function_exists(') _paches('xc_paches)( CHE;

               elseif (class_exists('Memcache '))           $ this->init_mem();

                elseif (class_exists('Memcached'))        class_exists('file)快取'))          $this->init_filecache ();

else $this->cacheError('找不到任何相容的快取');

               break;

       $msg='未選擇任何快取類型' ;


                if (isset($type)) $msg='選擇了無法辨識的快取類型'.$type.'';

               break;

        }

    }

) 創造詞

. ached類別的實例

        if (is_array($this-> ;cache_params)) {

if ($useMemecached) {


                $this->cache_type = self::CACHE_TYPE_MEMCACHED;

🎔

            } else {

                $this ->cache_type = self::CACHE_TYPE_MEMCACHE;

                $this->cache_external = new Memcache;

            }

_ ache  para ache 

                $server['port'] = isset($server['連接埠']) ? (int)$server['port'] : self::DEFAULT_MEMCACHE_PORT;

                if ($useMemecached) { $server['host'], $server['port '] );

                } else {

                   , $.H) = 持久')')[]]]                    $this->cache_external-> addServer($server['host'], $server['port'], $server['persistent']) ;

                }

   約 ache ache  三酸 } $this->cache_type . '需要一個陣列,例如:wrapperCache::getInstance("' . $this->cache_type . '", 30,array(array("host"=>"localhost")));');

    }

    private function init_filecache(){//取得filecache 類別的實例

        $this-> ;cache_type = self::CACHE_TYPE_FIache;paches; this->cache_expire, $this ->cache_params);

    }

    public function getAvailableCache($ return_format='html'){// 返回可用快取

🠎);      $avCaches[] = array(self: :CACHE_TYPE_EACCELERATOR,function_exists('eaccelerator_get'));

        $avCaches[] = array(self::CACHE_TYPE_APC,         function_exists('apc_fetch')) ;

CHE ists('xcache_get'));

      $avCaches[] = array(self::CACHE_TYPE_MEMCACHE,    class_exists('Memcache'));

        $avCaches[] = array(self::CA? [] = array(self::CACHE_TYPE_FILE ,        class_exists('fileCache'));


        if ($return_format == '         foreach ($avCaches為$c) {

$ ret .= '

  • ' 。 $c[0] 。 ' - ';

                    if ($c[1]) ret不相容';

                    $ret .= ' ';

                      } else return $avCaches;

        }

        原生樹函數cacheError($msg){//觸發錯誤

            trigger_error('
    ;wrapperCache 錯誤: '.$msg.

     可以,可以,嘗試使用'auto' 自動選擇相容的快取。 }

  • 相關標籤:
    php
    來源:php.cn
    本網站聲明
    本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
    熱門教學
    更多>
    最新下載
    更多>
    網站特效
    網站源碼
    網站素材
    前端模板
    關於我們 免責聲明 Sitemap
    PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!