暫無描述

cache.class.v1.php 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. class cache{
  3. public $dirName = 'cachedir';
  4. function getCacheFile($name,$dirName=''){
  5. $self_path = dirname(__FILE__);
  6. $cfile=$self_path."/".($dirName?$dirName:$this->dirName)."/".md5($name);
  7. return $cfile;
  8. }
  9. function openCacheFile($name){
  10. $file = $this->getCacheFile($name);
  11. return fopen($file, 'a+');
  12. }
  13. function readCache($name){
  14. $fhandle = $this->openCacheFile($name);
  15. $ret = $this->getCache($name,$fhandle);
  16. $this->freeCache($fhandle,false);
  17. return $ret;
  18. }
  19. function writeCache($name,$data,$cache_time=-1){
  20. $fhandle = $this->openCacheFile($name);
  21. $ret = $this->setCache($name, $data, $cache_time, $fhandle);
  22. $this->freeCache($fhandle,false);
  23. return $ret;
  24. }
  25. function getCache($name, $fhandle){
  26. $cfile=$this->getCacheFile($name);
  27. clearstatcache();
  28. if(file_exists($cfile)){
  29. fseek($fhandle,0);//新增
  30. $c = @fread($fhandle, filesize($cfile));
  31. if($c){
  32. $cls = explode('__$$$__',$c);
  33. if(isset($cls[1])&& is_numeric($cls[1])){
  34. if(($cls[1]-time())>-1){
  35. return $cls[0];
  36. }
  37. }
  38. }
  39. }
  40. }
  41. function freeCacheLock($fhandle,$lock_ret){
  42. if($lock_ret)
  43. {
  44. @flock($fhandle, LOCK_UN);
  45. }
  46. }
  47. function freeCache($fhandle,$lock_ret){
  48. if($lock_ret)
  49. {
  50. @flock($fhandle, LOCK_UN);
  51. }
  52. @fclose($fhandle);
  53. }
  54. function setCache($name, $data, $cache_time, $fhandle){
  55. $cfile=$this->getCacheFile($name);
  56. ftruncate($fhandle,0);
  57. if($data && $cache_time>-1){
  58. $datav = $data.'__$$$__'.(time()+$cache_time);
  59. fwrite($fhandle, $datav);
  60. }
  61. //@file_put_contents($cfile,$data);
  62. return $data;
  63. }
  64. function existCache($name){
  65. $cfile=$this->getCacheFile($name);
  66. return file_exists($cfile);
  67. }
  68. }
  69. ?>