YacDriver.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 YacDriver implements CacheInterface
  15. {
  16. /**
  17. * @var string
  18. */
  19. private $namespace;
  20. /**
  21. * @var int
  22. */
  23. private $defaultLifetime;
  24. /**
  25. * yac client
  26. *
  27. * @var \Yac
  28. */
  29. protected $yac;
  30. /**
  31. * if your key is longer than this, maybe you can use md5 result as the key
  32. */
  33. const KEY_MAX_LEN = 48;
  34. public function __construct($namespace = '', $defaultLifetime = 0)
  35. {
  36. $this->namespace = $namespace;
  37. $this->defaultLifetime = $defaultLifetime;
  38. $this->yac = new \Yac($namespace);
  39. }
  40. public function get($key, $default = null)
  41. {
  42. $this->assertKeyName($key);
  43. $key = $this->buildKeyName($key);
  44. return $this->yac->get($key) ?: $default;
  45. }
  46. public function set($key, $value, $ttl = null)
  47. {
  48. $this->assertKeyName($key);
  49. $key = $this->buildKeyName($key);
  50. $ttl = is_null($ttl) ? $this->defaultLifetime : $ttl;
  51. return $this->yac->set($key, $value, (int)$ttl);
  52. }
  53. public function delete($key)
  54. {
  55. $this->assertKeyName($key);
  56. $key = $this->buildKeyName($key);
  57. return $this->yac->delete($key);
  58. }
  59. public function clear()
  60. {
  61. return $this->yac->flush();
  62. }
  63. public function getMultiple($keys, $default = null)
  64. {
  65. if(!is_array($keys)) {
  66. return array();
  67. }
  68. $hashKeyMap = array();
  69. foreach($keys as $index => $key)
  70. {
  71. $this->assertKeyName($key);
  72. if(strlen($key) > self::KEY_MAX_LEN)
  73. {
  74. $keys[$index] = $this->buildKeyName($key);
  75. $hashKeyMap[$keys[$index]] = $key;
  76. }
  77. }
  78. $results = $this->yac->get($keys);
  79. if($results !== false)
  80. {
  81. foreach($results as $key => $value)
  82. {
  83. if(isset($hashKeyMap[$key]))
  84. {
  85. $results[$hashKeyMap[$key]] = $value;
  86. unset($results[$key]);
  87. }
  88. }
  89. return $results;
  90. }
  91. $results = array();
  92. foreach($keys as $key)
  93. {
  94. $results[$key] = $default;
  95. }
  96. return $results;
  97. }
  98. public function setMultiple($values, $ttl = null)
  99. {
  100. if(!is_array($values)) return false;
  101. foreach($values as $key => $value)
  102. {
  103. if(strlen($key) > self::KEY_MAX_LEN)
  104. {
  105. $values[$this->buildKeyName($key)] = $value;
  106. unset($values[$key]);
  107. }
  108. }
  109. $ttl = is_null($ttl) ? $this->defaultLifetime : $ttl;
  110. return $this->yac->set($values, $ttl);
  111. }
  112. public function deleteMultiple($keys)
  113. {
  114. foreach($keys as $index => $key)
  115. {
  116. $keys[$index] = $this->buildKeyName($key);
  117. }
  118. return $this->yac->delete($keys);
  119. }
  120. public function has($key)
  121. {
  122. return $this->get($key) !== null;
  123. }
  124. /**
  125. * @param string $key
  126. *
  127. * @return string
  128. */
  129. private function buildKeyName($key)
  130. {
  131. if(strlen($key) > self::KEY_MAX_LEN)
  132. {
  133. $key = md5($key);
  134. }
  135. return $key;
  136. }
  137. /**
  138. * @param string[] $keys
  139. *
  140. * @return string[]
  141. */
  142. private function buildKeyNames($keys)
  143. {
  144. return array_map(function ($key) {
  145. return $this->buildKeyName($key);
  146. }, $keys);
  147. }
  148. /**
  149. * @param mixed $key
  150. *
  151. * @throws InvalidArgumentException
  152. */
  153. private function assertKeyName($key)
  154. {
  155. if(!is_scalar($key) || is_bool($key)) throw new InvalidArgumentException();
  156. }
  157. /**
  158. * @param string[] $keys
  159. *
  160. * @throws InvalidArgumentException
  161. */
  162. private function assertKeyNames($keys)
  163. {
  164. array_map(function ($value) {
  165. $this->assertKeyName($value);
  166. }, $keys);
  167. }
  168. }