php 檔案快取

2021-06-07 08:08:00 字數 3338 閱讀 3068

<?php 

class cache

return self::$_instance;

} /**

* 得到快取資訊

* * @param string $id

* @return boolean|array

*/public static function get($id)

$file = $instance->_file($id);

$data = $instance->_filegetcontents($file);

if($data['expire'] == 0 || time() < $data['expire'])

return false; }

/*** 設定乙個快取

* * @param string $id 快取id

* @param array $data 快取內容

* @param int $cachelife 快取生命 預設為0無限生命

*/public static function set($id, $data, $cachelife = 0)

/*** 清除一條快取

* * @param string cache id

* @return void

*/

public static function delete($id)

$file = $instance->_file($id);

//刪除該快取

return unlink($file); }

/*** 判斷快取是否存在

* * @param string $id cache_id

* @return boolean true 快取存在 false 快取不存在

*/public static function has($id)

return true; }

/*** 通過快取id得到快取資訊路徑

* @param string $id

* @return string 快取檔案路徑

*/protected function _file($id)

/*** 通過id得到快取資訊儲存檔名

* * @param $id

* @return string 快取檔名

*/protected function _idtofilename($id)

/*** 通過filename得到快取id

* * @param $id

* @return string 快取id

*/protected function _filenametoid($filename)

/*** 把資料寫入檔案

* * @param string $file 檔名稱

* @param array $contents 資料內容

* @return bool

*/protected function _fileputcontents($file, $contents)

else

$result = false;

$f = @fopen($file, 'w');

if ($f)

@fclose($f);

}@chmod($file,0777);

return $result; }

/*** 從檔案得到資料

* * @param sring $file

* @return boolean|array

*/protected function _filegetcontents($file)

if($this->_options['mode'] == 1)

else

}/**

* 建構函式

*/protected function __construct()

/*** 設定快取路徑

* * @param string $path

* @return self

*/public static function setcachedir($path)

if (!is_writable($path))

$path = rtrim($path,'/') . '/';

$instance->_options['cache_dir'] = $path;

return $instance; }

/*** 設定快取檔案字首

* * @param srting $prefix

* @return self

*/public static function setcacheprefix($prefix)

/*** 設定快取儲存型別

* * @param int $mode

* @return self

*/public static function setcachemode($mode = 1)

else

return $instance; }

/*** 刪除所有快取

* @return boolean

*/public static function flush()

foreach ($glob as $v)

return true; }}

/* 初始化設定cache的配置資訊什麼的 */

cache::setcacheprefix('core'); //設定快取檔案字首

cache::setcachedir('./cache'); //設定存放快取資料夾路徑

//模式1 快取儲存方式

//a:3:s:6:"expire";i:0;s:5:"mtime";i:1318218422;}

//模式2 快取儲存方式

/* <?php

// mktime: 1318224645

return array (

'contents' =>

array (

0 => 1,

1 => 2,

2 => 3,

3 => 34,

4 => 5,

5 => 6,

6 => 6,

),'expire' => 0,

'mtime' => 1318224645,

)?>

* *

*/cache::setcachemode('2');

if(!$row = cache::get('zj2'))

// cache::flush(); 清空所有快取

print_r($row);

PHP檔案快取

專案中要用到臨時儲存乙個陣列,於是想利用php的檔案操作來臨時儲存陣列。php 4,php 5,php 7 fopen 開啟檔案或者 url resource fopen string fi lena me,s trin g mode bool us einc lude path fal se re...

PHP 檔案快取

檔案快取類 class cache class filecache self dir dir 讀取快取檔案 param file string 快取檔案 param boolean 讀取成功返回結果,否則返回 false public function read file 讀取快取檔案 if han...

php檔案快取

快取做快取的目的是為了讓程式執行起來更加迅速。因為如果程式訪問資料庫時資料量較大,執行起來會比較慢。而且每一次重新整理頁面都會訪問依稀資料庫,然後再把資料顯示在頁面上。設定快取也有乙個缺點,那就是快取時間要設定好,如果快取時間較長,那麼資料庫資料變化時,不能及時的在頁面上顯示。例如快取不能用在秒殺商...