1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- <?php
- class cache{
- public $dirName = 'cachedir';
- function getCacheFile($name,$dirName=''){
- $self_path = dirname(__FILE__);
- $cfile=$self_path."/".($dirName?$dirName:$this->dirName)."/".md5($name);
- return $cfile;
- }
- function openCacheFile($name){
- $file = $this->getCacheFile($name);
- return fopen($file, 'a+');
- }
- function readCache($name){
- $fhandle = $this->openCacheFile($name);
-
- $ret = $this->getCache($name,$fhandle);
- $this->freeCache($fhandle,false);
- return $ret;
- }
- function writeCache($name,$data,$cache_time=-1){
- $fhandle = $this->openCacheFile($name);
- $ret = $this->setCache($name, $data, $cache_time, $fhandle);
- $this->freeCache($fhandle,false);
- return $ret;
- }
- function getCache($name, $fhandle){
- $cfile=$this->getCacheFile($name);
- clearstatcache();
- if(file_exists($cfile)){
- fseek($fhandle,0);//新增
- $c = @fread($fhandle, filesize($cfile));
- if($c){
- $cls = explode('__$$$__',$c);
- if(isset($cls[1])&& is_numeric($cls[1])){
- if(($cls[1]-time())>-1){
- return $cls[0];
- }
- }
- }
- }
- }
- function freeCacheLock($fhandle,$lock_ret){
- if($lock_ret)
- {
- @flock($fhandle, LOCK_UN);
- }
- }
- function freeCache($fhandle,$lock_ret){
- if($lock_ret)
- {
- @flock($fhandle, LOCK_UN);
- }
- @fclose($fhandle);
- }
- function setCache($name, $data, $cache_time, $fhandle){
- $cfile=$this->getCacheFile($name);
- ftruncate($fhandle,0);
- if($data && $cache_time>-1){
- $datav = $data.'__$$$__'.(time()+$cache_time);
- fwrite($fhandle, $datav);
- }
- //@file_put_contents($cfile,$data);
- return $data;
- }
- function existCache($name){
- $cfile=$this->getCacheFile($name);
- return file_exists($cfile);
- }
- }
- ?>
|