Cache.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace app\store\controller\setting;
  13. use app\common\library\helper;
  14. use app\store\controller\Controller;
  15. use think\facade\Cache as CacheDrive;
  16. /**
  17. * 清理缓存
  18. * Class Index
  19. * @package app\store\controller
  20. */
  21. class Cache extends Controller
  22. {
  23. /**
  24. * 数据缓存项目(只显示key和name)
  25. * @return array
  26. */
  27. public function items()
  28. {
  29. $items = [];
  30. foreach ($this->getItems() as $key => $item) {
  31. $items[] = [
  32. 'key' => $key,
  33. 'name' => $item['name']
  34. ];
  35. }
  36. return $this->renderSuccess(compact('items'));
  37. }
  38. /**
  39. * 清理缓存
  40. * @return mixed
  41. */
  42. public function clear()
  43. {
  44. // 删除缓存
  45. $this->rmCache($this->postForm()['keys']);
  46. return $this->renderSuccess('操作成功');
  47. }
  48. /**
  49. * 数据缓存项目
  50. * @return array
  51. */
  52. private function getItems()
  53. {
  54. $storeId = $this->store['store_id'];
  55. return [
  56. 'category' => [
  57. 'type' => 'cache',
  58. 'key' => "category_{$storeId}",
  59. 'name' => '商品分类'
  60. ],
  61. 'setting' => [
  62. 'type' => 'cache',
  63. 'key' => "setting_{$storeId}",
  64. 'name' => '商城设置'
  65. ],
  66. 'wxapp' => [
  67. 'type' => 'cache',
  68. 'key' => "wxapp_{$storeId}",
  69. 'name' => '小程序设置'
  70. ],
  71. 'temp' => [
  72. 'type' => 'file',
  73. 'name' => '临时图片',
  74. 'dirPath' => [
  75. 'web' => web_path() . "temp/{$storeId}/",
  76. 'runtime' => runtime_root_path() . "/image/{$storeId}/",
  77. ]
  78. ],
  79. ];
  80. }
  81. /**
  82. * 删除缓存
  83. * @param $keys
  84. */
  85. private function rmCache(array $keys)
  86. {
  87. $cacheList = $this->getItems();
  88. $keys = array_intersect(array_keys($cacheList), $keys);
  89. foreach ($keys as $key) {
  90. $item = $cacheList[$key];
  91. if ($item['type'] === 'cache') {
  92. $cache = CacheDrive::instance();
  93. $cache->has($item['key']) && $cache->delete($item['key']);
  94. } elseif ($item['type'] === 'file') {
  95. $this->deltree($item['dirPath']);
  96. }
  97. }
  98. }
  99. /**
  100. * 删除目录下所有文件
  101. * @param $dirPath
  102. * @return bool
  103. */
  104. private function deltree($dirPath)
  105. {
  106. if (is_array($dirPath)) {
  107. foreach ($dirPath as $path)
  108. $this->deleteFolder($path);
  109. } else {
  110. return $this->deleteFolder($dirPath);
  111. }
  112. return true;
  113. }
  114. /**
  115. * 递归删除指定目录下所有文件
  116. * @param $path
  117. * @return bool
  118. */
  119. private function deleteFolder(string $path)
  120. {
  121. if (!is_dir($path)) return false;
  122. // 扫描一个文件夹内的所有文件夹和文件
  123. foreach (scandir($path) as $val) {
  124. // 排除目录中的.和..
  125. if (!in_array($val, ['.', '..'])) {
  126. // 如果是目录则递归子目录,继续操作
  127. if (is_dir("{$path}{$val}")) {
  128. // 子目录中操作删除文件夹和文件
  129. $this->deleteFolder("{$path}{$val}/");
  130. // 目录清空后删除空文件夹
  131. rmdir("{$path}{$val}/");
  132. } else {
  133. // 如果是文件直接删除
  134. unlink("{$path}{$val}");
  135. }
  136. }
  137. }
  138. return true;
  139. }
  140. }