Memcache.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 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 $this->handler->get($key) ? true : false;
  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 $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 ($this->tag && !$this->has($name)) {
  89. $first = true;
  90. }
  91. $key = $this->getCacheKey($name);
  92. if ($this->handler->set($key, $value, 0, $expire)) {
  93. isset($first) && $this->setTagItem($key);
  94. return true;
  95. }
  96. return false;
  97. }
  98. /**
  99. * 自增缓存(针对数值缓存)
  100. * @access public
  101. * @param string $name 缓存变量名
  102. * @param int $step 步长
  103. * @return false|int
  104. */
  105. public function inc($name, $step = 1)
  106. {
  107. $key = $this->getCacheKey($name);
  108. if ($this->handler->get($key)) {
  109. return $this->handler->increment($key, $step);
  110. }
  111. return $this->handler->set($key, $step);
  112. }
  113. /**
  114. * 自减缓存(针对数值缓存)
  115. * @access public
  116. * @param string $name 缓存变量名
  117. * @param int $step 步长
  118. * @return false|int
  119. */
  120. public function dec($name, $step = 1)
  121. {
  122. $key = $this->getCacheKey($name);
  123. $value = $this->handler->get($key) - $step;
  124. $res = $this->handler->set($key, $value);
  125. if (!$res) {
  126. return false;
  127. } else {
  128. return $value;
  129. }
  130. }
  131. /**
  132. * 删除缓存
  133. * @param string $name 缓存变量名
  134. * @param bool|false $ttl
  135. * @return bool
  136. */
  137. public function rm($name, $ttl = false)
  138. {
  139. $key = $this->getCacheKey($name);
  140. return false === $ttl ?
  141. $this->handler->delete($key) :
  142. $this->handler->delete($key, $ttl);
  143. }
  144. /**
  145. * 清除缓存
  146. * @access public
  147. * @param string $tag 标签名
  148. * @return bool
  149. */
  150. public function clear($tag = null)
  151. {
  152. if ($tag) {
  153. // 指定标签清除
  154. $keys = $this->getTagItem($tag);
  155. foreach ($keys as $key) {
  156. $this->handler->delete($key);
  157. }
  158. $this->rm('tag_' . md5($tag));
  159. return true;
  160. }
  161. return $this->handler->flush();
  162. }
  163. }