色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

PHP文件緩存內(nèi)容保存格式實(shí)例分析

本文實(shí)例講述了php文件緩存內(nèi)容保存格式,對(duì)于進(jìn)行php項(xiàng)目開(kāi)發(fā)非常具有實(shí)用價(jià)值。分享給大家供大家參考借鑒。具體分析如下:

1、php文件緩存內(nèi)容保存格式

php文件緩存內(nèi)容保存格式主要有三種:

(1)變量 var_export 格式化成php正常的賦值書(shū)寫(xiě)格式;
(2)變量 serialize 序列化之后保存,用的時(shí)候反序列化;
(3)變量 json_encode格式化之后保存,用的時(shí)候json_decode

互聯(lián)網(wǎng)上測(cè)試結(jié)果是:serialize格式的文件解析效率大于Json,Json的解析效率大于php正常賦值。
所以我們要是緩存數(shù)據(jù)建議采用序列化的形式解析數(shù)據(jù)會(huì)更快。

2、php文件緩存的簡(jiǎn)單案例

<?phpclass Cache_Driver {  //定義緩存的路徑  protected $_cache_path;  //根據(jù)$config中的cache_path值獲取路徑信息  public function Cache_Driver($config) {    if (is_array($config) && isset($config['cache_path'])) {      $this->_cache_path = $config['cache_path'];    } else {      $this->_cache_path = realpath(dirname(__FILE__) . "/") . "/cache/";    }  }  //判斷key值對(duì)應(yīng)的文件是否存在,如果存在,讀取value值,value以序列化存儲(chǔ)  public function get($id) {    if (!file_exists($this->_cache_path . $id)) {      return FALSE;    }    $data = @file_get_contents($this->_cache_path . $id);    $data = unserialize($data);    if (!is_array($data) || !isset($data['time']) || !isset($data['ttl'])) {      return FALSE;    }    if ($data['ttl'] > 0 && time() > $data['time'] + $data['ttl']) {      @unlink($this->_cache_path . $id);      return FALSE;    }    return $data['data'];  }  //設(shè)置緩存信息,根據(jù)key值,生成相應(yīng)的緩存文件  public function set($id, $data, $ttl = 60) {    $contents = array(      'time' => time() ,      'ttl' => $ttl,      'data' => $data    );    if (@file_put_contents($this->_cache_path . $id, serialize($contents))) {      @chmod($this->_cache_path . $id, 0777);      return TRUE;    }    return FALSE;  }  //根據(jù)key值,刪除緩存文件  public function delete($id) {    return @unlink($this->_cache_path . $id);  }  public function clean() {    $dh = @opendir($this->_cache_path);    if (!$dh) return FALSE;    while ($file = @readdir($dh)) {      if ($file == "." || $file == "..") continue;      $path = $this->_cache_path . "/" . $file;      if (is_file($path)) @unlink($path);    }    @closedir($dh);    return TRUE;  }}

希望本文所述php緩存實(shí)例對(duì)大家的php程序開(kāi)發(fā)能起到一定的幫助借鑒作用。

php技術(shù)PHP文件緩存內(nèi)容保存格式實(shí)例分析,轉(zhuǎn)載需保留來(lái)源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 栾城县| 沭阳县| 正定县| 北宁市| 仲巴县| 搜索| 新郑市| 胶南市| 廊坊市| 平利县| 双峰县| 枣强县| 石阡县| 漳浦县| 浦县| 昭觉县| 庆安县| 宁晋县| 肥乡县| 萨迦县| 磐安县| 海伦市| 汶上县| 太谷县| 嘉荫县| 太康县| 宁城县| 赤峰市| 安宁市| 法库县| 白水县| 鲜城| 枝江市| 南和县| 永年县| 凉城县| 英超| 邻水| 扶风县| 邵武市| 云林县|