php自用資料緩衝類

來源:互聯網
上載者:User

剛換了一個工作,現在沒什麼事做,寫了一個資料緩衝的類。

可以緩衝數組,字元,對象等,執行效率還沒有測試,先放出來吧。

執行個體如下:

 

 * @example

 * require 'MyCache.class.php';

 * $mc = new MyCache("./test/cache");

 * $a = "hello world111";

 * $mc->set("ss", $a);

 * $mc->set("dd", $mc);

 * $mc->set("2s", $a);

 * echo $mc->get("ss");

 * $mc->delete("2s");

 * $mc->edit("ss", "hello");

 * echo $mc->get_type("dd");

 * echo "<pre>";

 * print_r($c);

 

<?php<br />/**<br /> * 自用的php 緩衝類<br /> * 功能,將變數,數組 寫入 檔案<br /> * 檔案名稱即為緩衝變數名<br /> * @example<br /> * require 'MyCache.class.php';<br /> * $mc = new MyCache("./test/cache");<br /> * $a = "hello world111";<br /> * $mc->set("ss", $a);<br /> * $mc->set("dd", $mc);<br /> * $mc->set("2s", $a);<br /> * echo $mc->get("ss");<br /> * echo $mc->get_type("dd");<br /> * $c = $mc->get_all();<br /> * echo "<pre>";<br /> * print_r($c);<br /> * @author: bruce wang 2010 - 03 - 08<br /> * @name: MyCache.class.php<br /> */<br />class MyCache {<br />private $cachePath;//緩衝目錄<br />private $lifeTime;//緩衝有效時間<br />private $pathSeq;//當前系統的分隔字元<br />/**<br /> * 建構函式,初始化<br /> * @param : string 'cachePath', 'lifeTime'<br /> * @return null<br /> */</p><p>function MyCache($cachePath = "dataCache", $lifeTime = 0){<br />//判斷快取檔案夾是否存在,否則建立<br />$this->create_dirs($cachePath);</p><p>$this->pathSeq = DIRECTORY_SEPARATOR;<br />$this->lifeTime = $lifeTime;<br />$this->cachePath = realpath($cachePath);<br />}</p><p>/**<br /> * 建立快取檔案並寫入資料<br /> * @param:string $var 變數名<br /> * @param: mixed $data 變數資料<br /> * @return null<br /> */<br />function set($var, $data){<br />$fileFullName = $this->get_full_name($var);<br />if(!file_exists($fileFullName)){<br />$this->write_cache_file($var, $data);<br />}<br />else{<br />if(!$this->is_file_live($var)){//不在有效期間<br />unlink($fileFullName);//刪除不在有效期間的快取檔案<br />$this->write_cache_file($var, $data);<br />}<br />else{<br />echo('同名快取資料' .$var. '已經存在,請換一個緩衝名稱<br />');<br />}<br />}</p><p>}</p><p>/**<br /> * 取得緩衝<br /> * @param string $var //緩衝名稱<br /> * @return mixed<br /> *<br /> */<br />function get($var){<br />return $this->read_cache_file($var);<br />}</p><p>/**<br /> * 得到快取資料類型<br /> * @param string $var<br /> * @return string<br /> */</p><p>function get_type($var){<br />$data = $this->get($var);<br />return gettype($data);<br />}</p><p>/**<br /> * 顯示所有未到期的快取資料<br /> * @param null<br /> * @return array<br /> */<br />function get_all(){<br />$dh = opendir($this->cachePath);<br />$fileArr = array();<br />while($file = readdir($dh)){<br />if($file != '.' && $file != '..' && (filetype($this->cachePath . $this->pathSeq .$file)!='dir')){<br />$file = substr($file,0,-4);<br />if($this->is_file_live($file)){<br />//$fileContent = $this->get($file);<br />$type = $this->get_type($file);<br />array_push($fileArr, array("name"=>$file, "type"=>$type));<br />}<br />}<br />}<br />return $fileArr;<br />}</p><p>/**<br /> * 建立快取檔案<br /> * @param $var<br /> * @param $data<br /> * @return bool<br /> */<br />private function write_cache_file($var, $data){<br />$str = serialize($data);<br />$fileFullName = $this->get_full_name($var);<br />if(file_put_contents($fileFullName, $str)){<br />return true;<br />}<br />else{<br />return false;<br />}<br />}</p><p>/**<br /> * 讀取快取資料<br /> * @param string $var<br /> * @return mixed<br /> */<br />private function read_cache_file($var){<br />$fullName = $this->get_full_name($var);<br />if(file_exists($fullName)){<br />if(!$this->is_file_live($var)){<br />unlink($fullName);<br />echo('快取資料' . $var . '已到期<br />');<br />}<br />else{<br />$str = file_get_contents($fullName);<br />return unserialize($str);<br />}<br />}<br />else{<br />echo('快取資料' . $var .'不存在.<br />');<br />}<br />}</p><p>/**<br /> * 判斷快取檔案是否在有效期間內<br /> * @param: string $var 變數名<br /> * @return bool<br /> */<br />function is_file_live($var){<br />$fileFullName = $this->get_full_name($var);<br />if(file_exists($fileFullName)){<br />if($this->lifeTime != 0){<br />if((time()-filemtime($fileFullName)) < $this->lifeTime){<br />return true;<br />}<br />else{<br />return false;<br />}<br />}<br />else{<br />return true;<br />}<br />}<br />else{<br />return false;<br />}<br />}</p><p>/**<br /> * 得到快取檔案全部路徑<br /> * @param:string $var//變數名<br /> * @return string<br /> */</p><p>function get_full_name($var){<br />$fileName = $this->varTofileName($var);<br />return $this->cachePath . $this->pathSeq . $fileName;<br />}</p><p>/**<br /> * 得到變數轉換的檔案名稱<br /> * @param: string $var //變數名<br /> * @return string<br /> */<br />private function varTofileName($var){<br />return $var . ".ca";<br />}</p><p>/**<br /> * 根據路徑建立檔案夾<br /> * @param strin $path<br /> * @return bool | null<br /> */<br />private function create_dirs($path){<br />if($path == '' || !isset($path)){<br />return false;<br />}<br />else{<br />if(!file_exists($path)){<br />$this->create_dirs(dirname($path));//迭代<br />try{<br />mkdir($path,0777);//建立檔案夾<br />}<br />catch (Exception $e){<br />echo $e->getMessage();exit;<br />}</p><p>}<br />}<br />}</p><p>} 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.