Cache.php 3.8 KB

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