ApcuDriver.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * The cache library of zentaopms.
  4. *
  5. * @copyright Copyright 2009-2023 禅道软件(青岛)有限公司(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 Lu Fei <lufei@easycorp.ltd>
  8. * @package cache
  9. * @link https://www.zentao.net
  10. */
  11. namespace ZenTao\Cache\Driver;
  12. use ZenTao\Cache\SimpleCache\CacheInterface;
  13. use ZenTao\Cache\SimpleCache\InvalidArgumentException;
  14. class ApcuDriver implements CacheInterface
  15. {
  16. /**
  17. * 缓存命名空间。
  18. * The cache namespace.
  19. *
  20. * @access private
  21. * @var string
  22. */
  23. private $namespace;
  24. /**
  25. * 缓存默认生命周期。
  26. * The cache default lifetime.
  27. *
  28. * @access private
  29. * @var int
  30. */
  31. private $defaultLifetime;
  32. /**
  33. * 缓存服务范围。private 独享|public 共享。
  34. * The cache scope.
  35. *
  36. * @access private
  37. * @var string
  38. */
  39. private $scope;
  40. /**
  41. * 缓存键连接符。
  42. * Cache key connector.
  43. *
  44. * @access private
  45. * @var string
  46. */
  47. private $connector;
  48. public function __construct($namespace = '', $defaultLifetime = 0, $scope = '', $connector = '')
  49. {
  50. $this->namespace = $namespace;
  51. $this->defaultLifetime = $defaultLifetime;
  52. $this->scope = $scope;
  53. $this->connector = $connector;
  54. }
  55. public function get($key, $default = null)
  56. {
  57. $value = apcu_fetch($key, $success);
  58. return $success === false ? $default : $value;
  59. }
  60. public function set($key, $value, $ttl = null)
  61. {
  62. $ttl = (int)($ttl ?: $this->defaultLifetime);
  63. return apcu_store($key, $value, $ttl);
  64. }
  65. public function delete($key)
  66. {
  67. return apcu_delete($key);
  68. }
  69. public function clear()
  70. {
  71. if($this->scope == 'private') return apcu_clear_cache();
  72. $keys = [];
  73. $info = apcu_cache_info();
  74. $cacheList = $info['cache_list'];
  75. foreach($cacheList as $cache)
  76. {
  77. if(strpos($cache['info'], $this->namespace . $this->connector) === 0) $keys[] = $cache['info'];
  78. }
  79. if(!$keys) return true;
  80. return $this->deleteMultiple($keys);
  81. }
  82. public function getMultiple($keys, $default = null)
  83. {
  84. $values = apcu_fetch($keys);
  85. if($values === false) return [];
  86. return $values;
  87. }
  88. public function setMultiple($values, $ttl = null)
  89. {
  90. $ttl = (int)($ttl ?: $this->defaultLifetime);
  91. $result = apcu_store($values, $ttl);
  92. return $result === true ? true : (is_array($result) && count($result) == 0 ? true : false);
  93. }
  94. public function deleteMultiple($keys)
  95. {
  96. $result = apcu_delete($keys);
  97. return count($result) === count($keys) ? false : true;
  98. }
  99. public function has($key)
  100. {
  101. return (bool) apcu_exists($key);
  102. }
  103. /**
  104. * 获取内存使用情况。
  105. * Get memory usage.
  106. *
  107. * @param string $type
  108. * @return string
  109. */
  110. public function memory($type)
  111. {
  112. $info = apcu_sma_info(true);
  113. if($type == 'total') return \helper::formatKB($info['seg_size']);
  114. if($type == 'free') return \helper::formatKB($info['avail_mem']);
  115. if($type == 'used') return \helper::formatKB($info['seg_size'] - $info['avail_mem']);
  116. if($type == 'rate') return round(($info['seg_size'] - $info['avail_mem']) / $info['seg_size'] * 100, 2);
  117. return '';
  118. }
  119. }