Memcached.php 5.5 KB

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