Memcache.php 5.3 KB

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