ホームページ  >  記事  >  バックエンド開発  >  PHP のより包括的なキャッシュ クラス

PHP のより包括的なキャッシュ クラス

巴扎黑
巴扎黑オリジナル
2016-11-24 15:12:041071ブラウズ

/*

* 名前: wrapperCache

* 注: fileCache、memcache/memcached、APC、Xcache、および eaccelerator のラッパー キャッシュ

$cacheObj =wrapperCache::getInstance('memcache',30, array(array('host'=>'localhost')));

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

*/

class WrapperCache {

const DEFAULT_MEMCACHE_PORT = 11211;


const CACHE_TYPE_AUTO = 'auto';

const CACHE_TYPE_EACCELERATOR = 'eaccelerator';

const CACHE_TYPE_APC = apc';

const CACHE_TYPE_MEMCACHE = 'memcache';

const CACHE_TYPE_MEMCACHED = 'memcached';

const CACHE_TYPE_FILE = 'filecache';

const CACHE_TYPE_XCACHE = 'xcache';


private $cache_params;   //パスや接続オプションなどの外部キャッシュの追加パラメータ memcached

public $cache_expire;   //キャッシュの有効期限が切れる秒数

private $cache_type;     //使用するキャッシュのタイプ

private $cache_external; // キャッシュの外部インスタンス。fileCache または memcache にすることができます

private static $instance;// このクラスのインスタンス


// 常に 1 つのインスタンスのみを返します

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

if (!isset(self::$instance)) { //存在しません

self::$instance = new self( $type、$exp_time、$params); //コンストラクターに移動します

}

return self::$instance;

}


// キャッシュコンストラクター、オプションの有効期限とキャッシュパス

private function __construct($type, $exp _時間、$ params) {

$this->cache_expire = $exp_time;

$this->cache_params = $params;

$this->setCacheType($type);

}


公共の機能__destruct() {

unset($this->cache_external);

}


// ユーザーがインスタンスをクローンできないようにします

public function __clone(){

$this ->キャッシュエラー('クローンは許可されていません。');

}


//フォルダーからキャッシュを削除します

public function clearCache(){

switch($this->cache_type){

case self ::CACHE_TYPE_EACCELERATOR :

eaccelerator_clean();

eaccelerator_clear();

ブレーク;


case self::CACHE_TYPE_APC :

apc_clear_cache('user');

Break;


case self::CACHE_TYPE_XCACHE :

xcache_clear_cache(XC_TYPE_VAR, 0);

ブレーク;


case self::CACHE_TYPE_MEMCACHE :

$this->cache_external->flush();

Break;


case self::CACHE_TYPE_MEMCACHED :

$this->cache_external->flush();

Break;


case self::CACHE _TYPE_FILE:

$this->cache_external->deleteCache();

Break;

}

}


//キャッシュの書き込みまたは読み取り

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

if ($value != '') { //書き込みたい

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

$this->put($key, $ value, $ttl);

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

//value

}


//指定されたデータ $key= で新しいキャッシュ ファイルを作成します= キャッシュの名前、データ 保存する情報/値

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

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


switch($this->cache_type){

case self::CACHE_TYPE_EACCELERATOR :

eaccelerator_put($key,データ)、$ttl);

ブレーク;


case self::CACHE_TYPE_APC :

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

Break;


case self::CACHE_TYPE_XCACHE :

xcache_set($key, Serialize($data), $ttl);

Break;


case self::CACHE_TYPE_MEMCACHE :

$data=serialize($data);

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

Break;


case self::CACHE_TYPE_MEMCACHED :

$data=serialize($data);

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

Break;


case self::CACHE_TYPE_FILE :

$this->cache_external->cache($key,$データ);

ブレーク;

}

}


//指定されたキーのキャッシュを返します

private function get($key){

switch($this->cache_type){

case self::CACHE_TYPE_EACCELERATOR :

$data = unserialize(eaccelerator_get ($key));

Break;


case self::CACHE_TYPE_APC :

$data = apc_fetch($key);

Break;


case self::CACHE_TYPE_XCACHE :

$ data = unserialize(xcache_get($key));

Break;


case self::CACHE_TYPE_MEMCACHE :

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

Break;


case self::CACHE_TYPE_MEMCACHED :

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

ブレーク;


case self:: CACHE_TYPE_FILE :

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

Break;

}

return $data;

}


//キャッシュからキーを削除

public function delete($key){

switch($this->cache_type){

case self::CACHE_TYPE_EACCELERATOR :

eaccelerator_rm($key);

Break;


case self::CACHE_TYPE_APC :

apc_delete($key);

Break;


case self::CACHE_TYPE_XCACHE :

xcache_unset($key);

Break;


case self::CACHE_TYPE_MEMCACHE :

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

Break;


case self::CACHE_TYPE_MEMCACHED :

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

Break;


case self::CACHE_TYPE_FILE :

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

Break;

}


}

// アプリケーション変数をオーバーロードし、自動的にキャッシュされます

public function __set($name, $value) {

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

}


public function __get($name) {

return $this->get($name);

}


public function __isset($key ) {// echo "'$name' は設定されていますか?n"

if ($this->get($key) !== false) return true;

else return false;

}


public function __unset ($name) {//echo "Unsetting '$name'n";

$this->delete($name);

}

//オーバーロードを終了


public function getCacheType(){

return $this->cache_type;

}


// インストールされていない場合にエラーが発生する場合はキャッシュを設定します

public function setCacheType($type){

$this->キャッシュタイプ=strtolower ($type);


switch($this->cache_type){

case self::CACHE_TYPE_EACCELERATOR :

if (!function_exists('eaccelerator_get')) $this->cacheError('eaccelerator ではありませんfound');

Break;


case self::CACHE_TYPE_APC :

if (!function_exists('apc_fetch')) $this->cacheError ('APC が見つかりません');

ブレーク;


case self::CACHE_TYPE_XCACHE :

if (function_exists('xcache_get')) $this->cacheError('Xcache not found');

Break;


case self::CACHE_TYPE_MEMCACHE :

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

else $this->cacheError('memcache not found');

Break ;


case self:: CACHE_TYPE_MEMCACHED :

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

else $this->cacheError('memcached not found');

休憩;


case self::CACHE_TYPE_FILE :

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

else $this->cacheError('fileCache not found');

Break;


case self::CACHE_TYPE_AUTO ://キャッシュ システムを自動選択してみます

if (function_exists('eaccelerator_get')) $this-> cache_type = self::CACHE_TYPE_EACCELERATOR;

elseif ( function_exists('apc_fetch')) $this->cache_type = self::CACHE_TYPE_APC ;

elseif (function_exists('xcache_get')) $this->cache_type = self::CACHE_TYPE_XCACHE;

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

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

elseif (class_存在します('fileCache')) $this->init_filecache ();

else $this->cacheError('互換性のあるキャッシュが見つかりません');

Break;


default://キャッシュが選択されていない、または間違ったものが選択されています

$msg='キャッシュ タイプが選択されていません';

if (isset($type)) $msg='認識できないキャッシュ タイプが選択されました '.$type.'';

$this->cacheError ($msg);

Break;

}

}


private function init_mem($useMemecached = false) { //memcache/memcached クラスのインスタンスを取得します

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

if ($useMemecached) {

$this->cache_type = self::CACHE_TYPE_MEMCACHED;

$this->cache_extern al = new Memcached();

} else {

$this ->cache_type = self::CACHE_TYPE_MEMCACHE;

$this->cache_external = new Memcache;

}

foreach ($this->cache_params as $サーバー) {

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

if ($useMemecached) {

$this->cache_external->addServer($server['host'], $server['port ']);

} else {

$server['persistent'] = isset($server['persistent']) ? (bool)$server['persistent'] : true;

$this->cache_external->addServer($server['host'], $server['port'], $server['persistent']) ;

}

}

} else $this->cacheError($this->cache_type . ' には配列が必要です。 example:wrapperCache::getInstance("' . _タイプ '"、 30,array(array("host"=>"localhost")));');

}


プライベート関数 init_filecache(){// ファイルキャッシュ クラスのインスタンスを取得します

$this-> ;cache_type = self::CACHE_TYPE_FILE ;

$this->cache_external = fileCache::getInstance($this->cache_expire, $this->cache_params);

}


public関数 getAvailableCache($ return_format='html'){// 利用可能なキャッシュを返します

$avCaches = array();

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

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

$avCaches[] = array(self::CACHE_TYPE_XCACHE, function_exists('xcache_get'));

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

$avCaches[] = array(self::CACHE_TYPE_MEMCACHED, class_exists('Memcached'));

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


if ($return_format == 'html') {

$ret = '

    ';

    foreach ($avCa $c としてチェス) {

    $ret .= '

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

    if ($c[1]) $ret .= '見つかった/互換性がある';

    else $ret .= '見つからなかった/互換性がない';

    $ret .= ' ';

    }

    $ret を返します。 '

';

} else return $avCaches;

}


プライベート関数cacheError($msg){//triggers error

trigger_error('
: '.$msg.

'
必要に応じて、互換性のあるキャッシュを自動選択するために「auto」を試してみることもできます。


または、リストからサポートされているキャッシュ:'.$this->getAvailableCache(), E_USER_ERROR);

}

}


声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。