PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/core/Cache.class.php

https://bitbucket.org/snail/mrpmvc
PHP | 187 lines | 120 code | 0 blank | 67 comment | 19 complexity | 502bb657a945488cbf995f1f690c7f31 MD5 | raw file
  1. <?php
  2. /**
  3. * MrPmvc默认缓存类
  4. * @author 狂奔的蜗牛
  5. * @email 672308444@163.com
  6. * @version alpha
  7. */
  8. class Cache{
  9. //提示信息
  10. public $tip_message;
  11. //缓存目录
  12. protected $cache_dir;
  13. //缓存文件名
  14. private $cache_file_name;
  15. //缓存文件后缀
  16. private $cache_file_suffix;
  17. public function __construct($dir,$cache_file_suffix = '.php'){
  18. $this->cache_dir = isset($dir)?$dir:dirname(__FILE__).DIRECTORY_SEPARATOR.'default_cache_data';
  19. $this->cache_file_suffix = $cache_file_suffix;
  20. if(!$this->dir_isvalid($this->cache_dir)){
  21. die($this->tip_message);//创建目录失败
  22. }
  23. }
  24. // +----------------------------------------------------------------------
  25. // |添加一个值,如果已经存在,则返回false,写入文件失败返回false
  26. // +----------------------------------------------------------------------
  27. // |
  28. // +----------------------------------------------------------------------
  29. public function add($cache_key,$cache_value,$life_time=1800){
  30. if(file_exists($this->get_cache_file_name($cache_key))){
  31. $this->tip_message = '缓存数据已存在.';
  32. return false;
  33. }
  34. $cache_data['data'] = $cache_value;
  35. $cache_data['life_time'] = $life_time;
  36. //以JSON格式写入文件
  37. if(file_put_contents($this->get_cache_file_name($cache_key), json_encode($cache_data))){
  38. return true;
  39. }else{
  40. $this->tip_message = '写入缓存失败.';
  41. return false;
  42. }
  43. }
  44. // +----------------------------------------------------------------------
  45. // |添加一个值,如果已经存在,则覆写
  46. // +----------------------------------------------------------------------
  47. // |
  48. // +----------------------------------------------------------------------
  49. public function set($cache_key,$cache_value,$life_time=1800){
  50. $cache_data['data'] = $cache_value;
  51. $cache_data['life_time'] = $life_time;
  52. if(file_put_contents($this->get_cache_file_name($cache_key), json_encode($cache_data))){
  53. return true;
  54. }else{
  55. $this->tip_message = '写入缓存失败.';
  56. return false;
  57. }
  58. }
  59. // +----------------------------------------------------------------------
  60. // |获取一个key值
  61. // +----------------------------------------------------------------------
  62. // |
  63. // +----------------------------------------------------------------------
  64. public function get($cache_key){
  65. if(!file_exists($this->get_cache_file_name($cache_key))){
  66. return false;
  67. }
  68. $data = $this->object_to_array(json_decode(file_get_contents($this->get_cache_file_name($cache_key))));
  69. if($this->check_isvalid($data['life_time'])){
  70. unset($data['life_time']);
  71. return $data['data'];
  72. }else{
  73. unlink($this->cache_file_name);
  74. $this->tip_message = '数据已过期.';
  75. return false;
  76. }
  77. }
  78. // +----------------------------------------------------------------------
  79. // |删除一个key值
  80. // +----------------------------------------------------------------------
  81. // |
  82. // +----------------------------------------------------------------------
  83. public function delete($cache_key){
  84. if(file_exists($this->get_cache_file_name($cache_key))){
  85. if(unlink($this->get_cache_file_name($cache_key)))
  86. return true;
  87. else
  88. return false;
  89. }else{
  90. $this->tip_message = '文件不存在.';
  91. return true;
  92. }
  93. }
  94. // +----------------------------------------------------------------------
  95. // |清除所有缓存文件
  96. // +----------------------------------------------------------------------
  97. // |
  98. // +----------------------------------------------------------------------
  99. public function flush(){
  100. $this->delete_file($this->cache_dir);
  101. }
  102. // +----------------------------------------------------------------------
  103. // |自动清除过期文件
  104. // +----------------------------------------------------------------------
  105. // |
  106. // +----------------------------------------------------------------------
  107. public function auto_delete_expired_file(){
  108. $this->delete_file($this->cache_dir,false);
  109. }
  110. // +----------------------------------------------------------------------
  111. // |检查目录是否存在,不存在则创建
  112. // +----------------------------------------------------------------------
  113. // |
  114. // +----------------------------------------------------------------------
  115. private function dir_isvalid($dir){
  116. if (is_dir($dir))
  117. return true;
  118. try {
  119. mkdir($dir,0777);
  120. }catch (Exception $e) {
  121. $this->tip_message = '所设定缓存目录不存在并且创建失败!请检查目录权限!';
  122. return false;
  123. }
  124. return true;
  125. }
  126. // +----------------------------------------------------------------------
  127. // |检查有效时间
  128. // +----------------------------------------------------------------------
  129. // |
  130. // +----------------------------------------------------------------------
  131. private function check_isvalid($expired_time = 0) {
  132. //if(!file_exists($this->cache_file_name)) return false;
  133. if (!(@$mtime = filemtime($this->cache_file_name))) return false;
  134. if (time() -$mtime > $expired_time) return false;
  135. return true;
  136. }
  137. // +----------------------------------------------------------------------
  138. // |获得缓存文件名
  139. // +----------------------------------------------------------------------
  140. // |
  141. // +----------------------------------------------------------------------
  142. private function get_cache_file_name($key){
  143. $this->cache_file_name = $this->cache_dir.DIRECTORY_SEPARATOR.md5($key).$this->cache_file_suffix;
  144. return $this->cache_file_name;
  145. }
  146. // +----------------------------------------------------------------------
  147. // |object对象转换为数组
  148. // +----------------------------------------------------------------------
  149. // |
  150. // +----------------------------------------------------------------------
  151. protected function object_to_array($obj){
  152. $_arr = is_object($obj) ? get_object_vars($obj) : $obj;
  153. foreach ($_arr as $key => $val) {
  154. $val = (is_array($val) || is_object($val)) ? $this->object_to_array($val) : $val;
  155. $arr[$key] = $val;
  156. }
  157. return $arr;
  158. }
  159. // +----------------------------------------------------------------------
  160. // |删除目录下的所有文件
  161. // +----------------------------------------------------------------------
  162. // | $mode true删除所有 false删除过期
  163. // +----------------------------------------------------------------------
  164. protected function delete_file($dir,$mode=true) {
  165. $dh=opendir($dir);
  166. while ($file=readdir($dh)) {
  167. if($file!="." && $file!="..") {
  168. $fullpath=$dir."/".$file;
  169. if(!is_dir($fullpath)) {
  170. if($mode){
  171. unlink($fullpath);
  172. }else{
  173. $this->cache_file_name = $fullpath;
  174. if(!$this->get_isvalid_by_path($fullpath)) unlink($fullpath);
  175. }
  176. } else {
  177. delete_file($fullpath,$mode);
  178. }
  179. }
  180. }
  181. closedir($dh);
  182. }
  183. private function get_isvalid_by_path($path){
  184. $data = $this->object_to_array(json_decode(file_get_contents($path)));
  185. return $this->check_isvalid($data['life_time']);
  186. }
  187. }