cache_file.class.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. class cache_file {
  3. /*缓存默认配置*/
  4. protected $_setting = array(
  5. 'suf' => '.cache.php', /*缓存文件后缀*/
  6. 'type' => 'array', /*缓存格式:array数组,serialize序列化,null字符串*/
  7. );
  8. /*缓存路径*/
  9. protected $filepath = '';
  10. /**
  11. * 构造函数
  12. * @param array $setting 缓存配置
  13. * @return void
  14. */
  15. public function __construct($setting = '') {
  16. $this->get_setting($setting);
  17. }
  18. /**
  19. * 写入缓存
  20. * @param string $name 缓存名称
  21. * @param mixed $data 缓存数据
  22. * @param array $setting 缓存配置
  23. * @param string $type 缓存类型
  24. * @param string $module 所属模型
  25. * @return mixed 缓存路径/false
  26. */
  27. public function set($name, $data, $setting = '', $type = 'data', $module = ROUTE_M) {
  28. $this->get_setting($setting);
  29. if(empty($type)) $type = 'data';
  30. if(empty($module)) $module = ROUTE_M;
  31. $filepath = CACHE_PATH.'caches_'.$module.'/caches_'.$type.'/';
  32. $filename = $name.$this->_setting['suf'];
  33. if(!is_dir($filepath)) {
  34. mkdir($filepath, 0777, true);
  35. }
  36. if($this->_setting['type'] == 'array') {
  37. $data = "<?php\nreturn ".var_export($data, true).";\n?>";
  38. } elseif($this->_setting['type'] == 'serialize') {
  39. $data = serialize($data);
  40. }
  41. if ($module == 'commons' || ($module == 'commons' && substr($name, 0, 16) != 'category_content')) {
  42. $db = pc_base::load_model('cache_model');
  43. $datas = new_addslashes($data);
  44. if ($db->get_one(array('filename'=>$filename, 'path'=>'caches_'.$module.'/caches_'.$type.'/'), '`filename`')) {
  45. $db->update(array('data'=>$datas), array('filename'=>$filename, 'path'=>'caches_'.$module.'/caches_'.$type.'/'));
  46. } else {
  47. $db->insert(array('filename'=>$filename, 'path'=>'caches_'.$module.'/caches_'.$type.'/', 'data'=>$datas));
  48. }
  49. }
  50. //是否开启互斥锁
  51. if(pc_base::load_config('system', 'lock_ex')) {
  52. $file_size = file_put_contents($filepath.$filename, $data, LOCK_EX);
  53. } else {
  54. $file_size = file_put_contents($filepath.$filename, $data);
  55. }
  56. return $file_size ? $file_size : 'false';
  57. }
  58. /**
  59. * 获取缓存
  60. * @param string $name 缓存名称
  61. * @param array $setting 缓存配置
  62. * @param string $type 缓存类型
  63. * @param string $module 所属模型
  64. * @return mixed $data 缓存数据
  65. */
  66. public function get($name, $setting = '', $type = 'data', $module = ROUTE_M) {
  67. $this->get_setting($setting);
  68. if(empty($type)) $type = 'data';
  69. if(empty($module)) $module = ROUTE_M;
  70. $filepath = CACHE_PATH.'caches_'.$module.'/caches_'.$type.'/';
  71. $filename = $name.$this->_setting['suf'];
  72. if (!file_exists($filepath.$filename)) {
  73. return false;
  74. } else {
  75. if($this->_setting['type'] == 'array') {
  76. $data = @require($filepath.$filename);
  77. } elseif($this->_setting['type'] == 'serialize') {
  78. $data = unserialize(file_get_contents($filepath.$filename));
  79. }
  80. return $data;
  81. }
  82. }
  83. /**
  84. * 删除缓存
  85. * @param string $name 缓存名称
  86. * @param array $setting 缓存配置
  87. * @param string $type 缓存类型
  88. * @param string $module 所属模型
  89. * @return bool
  90. */
  91. public function delete($name, $setting = '', $type = 'data', $module = ROUTE_M) {
  92. $this->get_setting($setting);
  93. if(empty($type)) $type = 'data';
  94. if(empty($module)) $module = ROUTE_M;
  95. $filepath = CACHE_PATH.'caches_'.$module.'/caches_'.$type.'/';
  96. $filename = $name.$this->_setting['suf'];
  97. if(file_exists($filepath.$filename)) {
  98. if ($module == 'commons' && substr($name, 0, 16) != 'category_content') {
  99. $db = pc_base::load_model('cache_model');
  100. $db->delete(array('filename'=>$filename, 'path'=>'caches_'.$module.'/caches_'.$type.'/'));
  101. }
  102. return @unlink($filepath.$filename) ? true : false;
  103. } else {
  104. return false;
  105. }
  106. }
  107. /**
  108. * 和系统缓存配置对比获取自定义缓存配置
  109. * @param array $setting 自定义缓存配置
  110. * @return array $setting 缓存配置
  111. */
  112. public function get_setting($setting = '') {
  113. if($setting) {
  114. $this->_setting = array_merge($this->_setting, $setting);
  115. }
  116. }
  117. public function cacheinfo($name, $setting = '', $type = 'data', $module = ROUTE_M) {
  118. $this->get_setting($setting);
  119. if(empty($type)) $type = 'data';
  120. if(empty($module)) $module = ROUTE_M;
  121. $filepath = CACHE_PATH.'caches_'.$module.'/caches_'.$type.'/';
  122. $filename = $filepath.$name.$this->_setting['suf'];
  123. if(file_exists($filename)) {
  124. $res['filename'] = $name.$this->_setting['suf'];
  125. $res['filepath'] = $filepath;
  126. $res['filectime'] = filectime($filename);
  127. $res['filemtime'] = filemtime($filename);
  128. $res['filesize'] = filesize($filename);
  129. return $res;
  130. } else {
  131. return false;
  132. }
  133. }
  134. }
  135. ?>