Cache.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace app\admin\controller\setting;
  3. use app\admin\controller\Controller;
  4. use think\Cache as CacheDriver;
  5. /**
  6. * 清理缓存
  7. * Class Index
  8. * @package app\admin\controller
  9. */
  10. class Cache extends Controller
  11. {
  12. /**
  13. * 清理缓存
  14. * @param bool $isForce
  15. * @return mixed
  16. */
  17. public function clear($isForce = false)
  18. {
  19. if ($this->request->isAjax()) {
  20. $this->rmCache($this->postData('cache'));
  21. return $this->renderSuccess('操作成功');
  22. }
  23. return $this->fetch('clear', [
  24. 'isForce' => !!$isForce ?: config('app_debug'),
  25. ]);
  26. }
  27. /**
  28. * 删除缓存
  29. * @param $data
  30. */
  31. private function rmCache($data)
  32. {
  33. // 数据缓存
  34. if (in_array('data', $data['item'])) {
  35. // 强制模式
  36. $isForce = isset($data['isForce']) ? !!$data['isForce'] : false;
  37. // 清除缓存
  38. CacheDriver::clear($isForce ? null : 'cache');
  39. }
  40. // 临时文件
  41. if (in_array('temp', $data['item'])) {
  42. $paths = [
  43. 'temp' => WEB_PATH . 'temp/',
  44. 'runtime' => RUNTIME_PATH . 'image/'
  45. ];
  46. foreach ($paths as $path) {
  47. $this->deleteFolder($path);
  48. }
  49. }
  50. }
  51. /**
  52. * 递归删除指定目录下所有文件
  53. * @param $path
  54. * @return bool
  55. */
  56. private function deleteFolder($path)
  57. {
  58. if (!is_dir($path))
  59. return false;
  60. // 扫描一个文件夹内的所有文件夹和文件
  61. foreach (scandir($path) as $val) {
  62. // 排除目录中的.和..
  63. if (!in_array($val, ['.', '..', '.gitignore'])) {
  64. // 如果是目录则递归子目录,继续操作
  65. if (is_dir($path . $val)) {
  66. // 子目录中操作删除文件夹和文件
  67. $this->deleteFolder($path . $val . '/');
  68. // 目录清空后删除空文件夹
  69. rmdir($path . $val . '/');
  70. } else {
  71. // 如果是文件直接删除
  72. unlink($path . $val);
  73. }
  74. }
  75. }
  76. return true;
  77. }
  78. }