cacheChajian.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * 缓存,目前是使用磁盘文件存储
  4. */
  5. class cacheChajian extends Chajian{
  6. private $dirvie = 'file'; //redis,file
  7. /**
  8. * 设置缓存
  9. * $time 缓存时间(秒)
  10. */
  11. public function set($key, $data, $time=0)
  12. {
  13. $this->del($key); //删除原来的
  14. $sarr['key'] = $this->getkey($key);
  15. $sarr['data'] = $data;
  16. if($time>0){
  17. $time = time()+$time;
  18. }else{
  19. $time = 0;
  20. }
  21. $sarr['time'] = $time;
  22. if($time>0)$sarr['timedt'] = date('Y-m-d H:i:s', $time);
  23. $sarr['url'] = $this->rock->nowurl();
  24. $this->delexpire();
  25. return $this->rock->createtxt($this->getpath($key, $time, 1), json_encode($sarr));
  26. }
  27. private function getkey($key)
  28. {
  29. return ''.QOM.''.$key.'';
  30. }
  31. private function getpath($key, $time=0, $lx=0)
  32. {
  33. $key = $this->getkey($key);
  34. $ske = '';
  35. if($time>0)$ske='_'.$time.'';
  36. if($lx==0)return ''.ROOT_PATH.'/'.UPDIR.'/cache/'.md5($key).''.$ske.'';
  37. return ''.UPDIR.'/cache/'.md5($key).''.$ske.'';
  38. }
  39. //获取文件名
  40. private function getpaths($key)
  41. {
  42. $key = $this->getkey($key);
  43. $file= ''.ROOT_PATH.'/'.UPDIR.'/cache/'.md5($key).'';
  44. $bar = glob(''.$file.'*');
  45. if(is_array($bar))foreach($bar as $k=>$fil1){
  46. if($k==0){
  47. $file = $fil1;
  48. }else{
  49. unlink($fil1);
  50. }
  51. }
  52. return $file;
  53. }
  54. /**
  55. * 获取缓存
  56. */
  57. public function get($key, $dev='')
  58. {
  59. $file= $this->getpaths($key);
  60. $data= $dev;
  61. if(file_exists($file)){
  62. $filea= explode('_', $file);
  63. $time = (int)arrvalue($filea, count($filea)-1,'0');
  64. if($time==0 || $time>=time()){
  65. $cont = file_get_contents($file);
  66. if(!isempt($cont)){
  67. $sarr = json_decode($cont, true);
  68. $data = arrvalue($sarr, 'data');
  69. }
  70. }else{
  71. unlink($file); //已经过期了
  72. }
  73. }
  74. return $data;
  75. }
  76. /**
  77. * 删除缓存
  78. */
  79. public function del($key)
  80. {
  81. $file= $this->getpaths($key);
  82. if(file_exists($file))@unlink($file);
  83. return true;
  84. }
  85. /**
  86. * 删除所有缓存
  87. */
  88. public function delall()
  89. {
  90. $bar = glob(''.ROOT_PATH.'/'.UPDIR.'/cache/*');
  91. foreach($bar as $k=>$fil1){
  92. unlink($fil1);
  93. }
  94. }
  95. /**
  96. * 删除过期的缓存
  97. */
  98. public function delexpire()
  99. {
  100. $bar = glob(''.ROOT_PATH.'/'.UPDIR.'/cache/*');
  101. $time= time();
  102. foreach($bar as $k=>$fil1){
  103. if(contain($fil1,'_')){
  104. $fil11 = substr($fil1, strripos($fil1, '_')+1);
  105. if(is_numeric($fil11)){
  106. if($fil11<$time){
  107. unlink($fil1);
  108. }
  109. }
  110. }
  111. }
  112. }
  113. }
粤ICP备19079148号