Memcache.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace think\cache\driver;
  12. use think\cache\Driver;
  13. /**
  14. * Memcache缓存类
  15. */
  16. class Memcache extends Driver
  17. {
  18. /**
  19. * 配置参数
  20. * @var array
  21. */
  22. protected $options = [
  23. 'host' => '127.0.0.1',
  24. 'port' => 11211,
  25. 'expire' => 0,
  26. 'timeout' => 0, // 超时时间(单位:毫秒)
  27. 'persistent' => true,
  28. 'prefix' => '',
  29. 'tag_prefix' => 'tag:',
  30. 'serialize' => [],
  31. ];
  32. /**
  33. * 架构函数
  34. * @access public
  35. * @param array $options 缓存参数
  36. * @throws \BadFunctionCallException
  37. */
  38. public function __construct(array $options = [])
  39. {
  40. if (!extension_loaded('memcache')) {
  41. throw new \BadFunctionCallException('not support: memcache');
  42. }
  43. if (!empty($options)) {
  44. $this->options = array_merge($this->options, $options);
  45. }
  46. $this->handler = new \Memcache;
  47. // 支持集群
  48. $hosts = (array) $this->options['host'];
  49. $ports = (array) $this->options['port'];
  50. if (empty($ports[0])) {
  51. $ports[0] = 11211;
  52. }
  53. // 建立连接
  54. foreach ($hosts as $i => $host) {
  55. $port = $ports[$i] ?? $ports[0];
  56. $this->options['timeout'] > 0 ?
  57. $this->handler->addServer($host, (int) $port, $this->options['persistent'], 1, $this->options['timeout']) :
  58. $this->handler->addServer($host, (int) $port, $this->options['persistent'], 1);
  59. }
  60. }
  61. /**
  62. * 判断缓存
  63. * @access public
  64. * @param string $name 缓存变量名
  65. * @return bool
  66. */
  67. public function has($name): bool
  68. {
  69. $key = $this->getCacheKey($name);
  70. return false !== $this->handler->get($key);
  71. }
  72. /**
  73. * 读取缓存
  74. * @access public
  75. * @param string $name 缓存变量名
  76. * @param mixed $default 默认值
  77. * @return mixed
  78. */
  79. public function get($name, $default = false)
  80. {
  81. $this->readTimes++;
  82. $result = $this->handler->get($this->getCacheKey($name));
  83. return false !== $result ? $this->unserialize($result) : $default;
  84. }
  85. /**
  86. * 写入缓存
  87. * @access public
  88. * @param string $name 缓存变量名
  89. * @param mixed $value 存储数据
  90. * @param int|\DateTime $expire 有效时间(秒)
  91. * @return bool
  92. */
  93. public function set($name, $value, $expire = null): bool
  94. {
  95. $this->writeTimes++;
  96. if (is_null($expire)) {
  97. $expire = $this->options['expire'];
  98. }
  99. $key = $this->getCacheKey($name);
  100. $expire = $this->getExpireTime($expire);
  101. $value = $this->serialize($value);
  102. if ($this->handler->set($key, $value, 0, $expire)) {
  103. return true;
  104. }
  105. return false;
  106. }
  107. /**
  108. * 自增缓存(针对数值缓存)
  109. * @access public
  110. * @param string $name 缓存变量名
  111. * @param int $step 步长
  112. * @return false|int
  113. */
  114. public function inc(string $name, int $step = 1)
  115. {
  116. $this->writeTimes++;
  117. $key = $this->getCacheKey($name);
  118. if ($this->handler->get($key)) {
  119. return $this->handler->increment($key, $step);
  120. }
  121. return $this->handler->set($key, $step);
  122. }
  123. /**
  124. * 自减缓存(针对数值缓存)
  125. * @access public
  126. * @param string $name 缓存变量名
  127. * @param int $step 步长
  128. * @return false|int
  129. */
  130. public function dec(string $name, int $step = 1)
  131. {
  132. $this->writeTimes++;
  133. $key = $this->getCacheKey($name);
  134. $value = $this->handler->get($key) - $step;
  135. $res = $this->handler->set($key, $value);
  136. return !$res ? false : $value;
  137. }
  138. /**
  139. * 删除缓存
  140. * @access public
  141. * @param string $name 缓存变量名
  142. * @param bool|false $ttl
  143. * @return bool
  144. */
  145. public function delete($name, $ttl = false): bool
  146. {
  147. $this->writeTimes++;
  148. $key = $this->getCacheKey($name);
  149. return false === $ttl ?
  150. $this->handler->delete($key) :
  151. $this->handler->delete($key, $ttl);
  152. }
  153. /**
  154. * 清除缓存
  155. * @access public
  156. * @return bool
  157. */
  158. public function clear(): bool
  159. {
  160. $this->writeTimes++;
  161. return $this->handler->flush();
  162. }
  163. /**
  164. * 删除缓存标签
  165. * @access public
  166. * @param array $keys 缓存标识列表
  167. * @return void
  168. */
  169. public function clearTag(array $keys): void
  170. {
  171. foreach ($keys as $key) {
  172. $this->handler->delete($key);
  173. }
  174. }
  175. }