PHP代码
- <?php
- /**
- * 数据级缓存类
- */
- !defined('IN_TEA') && exit('Access Denied');
- class dataCache {
- public $fileName;
- public $cacheTime;
- function __construct(){
- }
- function C($cacheName,$cacheValue = false,$cacheTime = 0){
- $this->fileName = TEA_CACHE_DIR.'dataCache/'.md5($cacheName);
- if($cacheValue===false){
- # 读取缓存
- if (!file_exists($this->fileName))
- return false;
- $data = tReadFile($this->fileName);
- $this->cacheTime = intval(substr($data,0,5));
- if ($this->isActive())
- return unserialize(substr($data,5));
- else
- return false;
- }
- else
- {
- #写入缓存
- tWriteFile($this->fileName,sprintf("%05d", $cacheTime).serialize($cacheValue),'w+');
- }
- }
- /**
- * 检测缓存是否过期
- *
- * @return bool true/false
- */
- private function isActive(){
- if ($this->cacheTime==0)
- return true;
- else if(0<(time() - (filemtime($this->fileName)+$this->cacheTime))) {
- #缓存过期
- return false;
- }
- else
- return true;
- }
- /**
- * 清理缓存
- *
- */
- public function clearCache(){
- $directory = dir(TEA_CACHE_DIR.'dataCache/');
- while($entry = $directory->read()) {
- $filename = $dir.'/'.$entry;
- if(is_file($filename)) {
- @unlink($filename);
- }
- }
- $directory->close();
- }
- }
- ?>
Follow Me