Memcached.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2017 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 $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 ($this->tag && !$this->has($name)) {
  100. $first = true;
  101. }
  102. $key = $this->getCacheKey($name);
  103. $expire = 0 == $expire ? 0 : $_SERVER['REQUEST_TIME'] + $expire;
  104. if ($this->handler->set($key, $value, $expire)) {
  105. isset($first) && $this->setTagItem($key);
  106. return true;
  107. }
  108. return false;
  109. }
  110. /**
  111. * 自增缓存(针对数值缓存)
  112. * @access public
  113. * @param string $name 缓存变量名
  114. * @param int $step 步长
  115. * @return false|int
  116. */
  117. public function inc($name, $step = 1)
  118. {
  119. $key = $this->getCacheKey($name);
  120. return $this->handler->increment($key, $step);
  121. }
  122. /**
  123. * 自减缓存(针对数值缓存)
  124. * @access public
  125. * @param string $name 缓存变量名
  126. * @param int $step 步长
  127. * @return false|int
  128. */
  129. public function dec($name, $step = 1)
  130. {
  131. $key = $this->getCacheKey($name);
  132. $value = $this->handler->get($key) - $step;
  133. $res = $this->handler->set($key, $value);
  134. if (!$res) {
  135. return false;
  136. } else {
  137. return $value;
  138. }
  139. }
  140. /**
  141. * 删除缓存
  142. * @param string $name 缓存变量名
  143. * @param bool|false $ttl
  144. * @return bool
  145. */
  146. public function rm($name, $ttl = false)
  147. {
  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. * @param string $tag 标签名
  157. * @return bool
  158. */
  159. public function clear($tag = null)
  160. {
  161. if ($tag) {
  162. // 指定标签清除
  163. $keys = $this->getTagItem($tag);
  164. $this->handler->deleteMulti($keys);
  165. $this->rm('tag_' . md5($tag));
  166. return true;
  167. }
  168. return $this->handler->flush();
  169. }
  170. }