Memcache.php 5.1 KB

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