Cache.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace app\store\controller\setting;
  3. use app\store\controller\Controller;
  4. use think\Cache as Driver;
  5. /**
  6. * 清理缓存
  7. * Class Index
  8. * @package app\store\controller
  9. */
  10. class Cache extends Controller
  11. {
  12. /**
  13. * 清理缓存
  14. * @return mixed
  15. */
  16. public function clear()
  17. {
  18. if ($this->request->isAjax()) {
  19. $data = $this->postData('cache');
  20. $this->rmCache($data['keys']);
  21. return $this->renderSuccess('操作成功');
  22. }
  23. return $this->fetch('clear', [
  24. 'cacheList' => $this->getItems()
  25. ]);
  26. }
  27. /**
  28. * 数据缓存项目
  29. * @return array
  30. */
  31. private function getItems()
  32. {
  33. $wxapp_id = $this->store['wxapp']['wxapp_id'];
  34. return [
  35. 'category' => [
  36. 'type' => 'cache',
  37. 'key' => 'category_' . $wxapp_id,
  38. 'name' => '商品分类'
  39. ],
  40. 'setting' => [
  41. 'type' => 'cache',
  42. 'key' => 'setting_' . $wxapp_id,
  43. 'name' => '商城设置'
  44. ],
  45. 'wxapp' => [
  46. 'type' => 'cache',
  47. 'key' => 'wxapp_' . $wxapp_id,
  48. 'name' => '小程序设置'
  49. ],
  50. 'dealer' => [
  51. 'type' => 'cache',
  52. 'key' => 'dealer_setting_' . $wxapp_id,
  53. 'name' => '分销设置'
  54. ],
  55. 'sharing' => [
  56. 'type' => 'cache',
  57. 'key' => 'sharing_setting_' . $wxapp_id,
  58. 'name' => '拼团设置'
  59. ],
  60. 'temp' => [
  61. 'type' => 'file',
  62. 'name' => '临时图片',
  63. 'dirPath' => [
  64. 'web' => WEB_PATH . 'temp/' . $wxapp_id . '/',
  65. 'runtime' => RUNTIME_PATH . '/' . 'image/' . $wxapp_id . '/'
  66. ]
  67. ],
  68. ];
  69. }
  70. /**
  71. * 删除缓存
  72. * @param $keys
  73. */
  74. private function rmCache($keys)
  75. {
  76. $cacheList = $this->getItems();
  77. $keys = array_intersect(array_keys($cacheList), $keys);
  78. foreach ($keys as $key) {
  79. $item = $cacheList[$key];
  80. if ($item['type'] === 'cache') {
  81. Driver::has($item['key']) && Driver::rm($item['key']);
  82. } elseif ($item['type'] === 'file') {
  83. $this->deltree($item['dirPath']);
  84. }
  85. }
  86. }
  87. /**
  88. * 删除目录下所有文件
  89. * @param $dirPath
  90. * @return bool
  91. */
  92. private function deltree($dirPath)
  93. {
  94. if (is_array($dirPath)) {
  95. foreach ($dirPath as $path)
  96. $this->deleteFolder($path);
  97. } else {
  98. return $this->deleteFolder($dirPath);
  99. }
  100. return true;
  101. }
  102. /**
  103. * 递归删除指定目录下所有文件
  104. * @param $path
  105. * @return bool
  106. */
  107. private function deleteFolder($path)
  108. {
  109. if (!is_dir($path))
  110. return false;
  111. // 扫描一个文件夹内的所有文件夹和文件
  112. foreach (scandir($path) as $val) {
  113. // 排除目录中的.和..
  114. if (!in_array($val, ['.', '..'])) {
  115. // 如果是目录则递归子目录,继续操作
  116. if (is_dir($path . $val)) {
  117. // 子目录中操作删除文件夹和文件
  118. $this->deleteFolder($path . $val . DS);
  119. // 目录清空后删除空文件夹
  120. rmdir($path . $val . DS);
  121. } else {
  122. // 如果是文件直接删除
  123. unlink($path . $val);
  124. }
  125. }
  126. }
  127. return true;
  128. }
  129. }