control.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * The control file of cache module of ZenTaoPMS.
  4. *
  5. * @copyright Copyright 2009-2024 禅道软件(青岛)有限公司(ZenTao Software (Qingdao) Co., Ltd. www.zentao.net)
  6. * @license ZPL(https://zpl.pub/page/zplv12.html) or AGPL(https://www.gnu.org/licenses/agpl-3.0.en.html)
  7. * @author Gang Liu <liugang@chandao.com>
  8. * @package cache
  9. * @link https://www.zentao.net
  10. */
  11. class cache extends control
  12. {
  13. /**
  14. * 设置是否启用缓存。
  15. * Set cache enable.
  16. *
  17. * @access public
  18. * @return void
  19. */
  20. public function setting()
  21. {
  22. if($_POST)
  23. {
  24. $redis = null;
  25. $cache = form::data()->get();
  26. if(isset($cache->redis))
  27. {
  28. $redis = (object)$cache->redis;
  29. unset($cache->redis);
  30. }
  31. if(!isset($redis->host)) $redis->host = '';
  32. if(!isset($redis->port)) $redis->port = '';
  33. if(!isset($redis->username)) $redis->username = '';
  34. if(!isset($redis->password)) $redis->password = '';
  35. if(!isset($redis->database)) $redis->database = '';
  36. if(!isset($redis->serializer)) $redis->serializer = '';
  37. if($cache->enable)
  38. {
  39. $errors = [];
  40. if(empty($cache->driver)) $errors['driver'] = sprintf($this->lang->error->notempty, $this->lang->cache->driver);
  41. if(empty($cache->scope)) $errors['scope'] = sprintf($this->lang->error->notempty, $this->lang->cache->scope);
  42. if(empty($cache->namespace)) $errors['namespace'] = sprintf($this->lang->error->notempty, $this->lang->cache->namespace);
  43. if($cache->driver == 'redis' && empty($redis->host)) $errors['redis[host]'] = sprintf($this->lang->error->notempty, $this->lang->cache->redis->host);
  44. if($cache->driver == 'redis' && empty($redis->port)) $errors['redis[port]'] = sprintf($this->lang->error->notempty, $this->lang->cache->redis->port);
  45. if($cache->driver == 'redis' && empty($redis->serializer)) $errors['redis[serializer]'] = sprintf($this->lang->error->notempty, $this->lang->cache->redis->serializer);
  46. if($cache->driver == 'redis' && empty($redis->database) && $redis->database !== '0') $errors['redis[database]'] = sprintf($this->lang->error->notempty, $this->lang->cache->redis->database);
  47. if($errors) return $this->send(array('result' => 'fail', 'message' => $errors));
  48. if($cache->driver == 'apcu')
  49. {
  50. /* 检查是否加载了 APCu 扩展。Check if the APCu extension is loaded. */
  51. if(!extension_loaded('apcu')) return $this->send(array('result' => 'fail', 'message' => $this->lang->cache->apcu->notLoaded));
  52. if(!ini_get('apc.enabled')) return $this->send(array('result' => 'fail', 'message' => $this->lang->cache->apcu->notEnabled));
  53. }
  54. if($cache->driver == 'redis')
  55. {
  56. /* 检查是否加载了 Redis 扩展。Check if the Redis extension is loaded. */
  57. if(!extension_loaded('redis')) return $this->send(array('result' => 'fail', 'message' => $this->lang->cache->redis->notLoaded));
  58. if($redis->serializer == 'igbinary')
  59. {
  60. if(!extension_loaded('igbinary')) return $this->send(array('result' => 'fail', 'message' => $this->lang->cache->redis->igbinaryNotLoaded));
  61. $reflection = new ReflectionClass(new Redis());
  62. if(!$reflection->hasConstant('SERIALIZER_IGBINARY')) return $this->send(array('result' => 'fail', 'message' => $this->lang->cache->redis->igbinaryNotSupported));
  63. }
  64. /* 检查 Redis 连接是否正常。Check if the Redis connection is normal. */
  65. try
  66. {
  67. helper::connectRedis($redis);
  68. }
  69. catch(Exception $e)
  70. {
  71. return $this->send(array('result' => 'fail', 'message' => $e->getMessage()));
  72. }
  73. }
  74. }
  75. $this->loadModel('setting')->setItems('system.common.cache', $cache);
  76. $this->setting->setItems('system.common.redis', $redis);
  77. $this->cache->clear($cache->enable);
  78. return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'load' => true));
  79. }
  80. if($this->config->cache->enable)
  81. {
  82. $this->view->rate = $this->mao->memory('rate');
  83. $this->view->used = $this->mao->memory('used');
  84. $this->view->total = $this->mao->memory('total');
  85. }
  86. $this->view->title = $this->lang->cache->common;
  87. $this->display();
  88. }
  89. /**
  90. * 清除数据缓存。
  91. * Clear data cache.
  92. *
  93. * @access public
  94. * @return void
  95. */
  96. public function flush()
  97. {
  98. $this->cache->clear($this->config->cache->enable);
  99. return $this->send(array('result' => 'success', 'message' => $this->lang->cache->clearSuccess, 'load' => true));
  100. }
  101. }