Memcachesae.class.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2012 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;
  13. /**
  14. * Memcache缓存驱动
  15. * @category Extend
  16. * @package Extend
  17. * @subpackage Driver.Cache
  18. * @author liu21st <liu21st@gmail.com>
  19. */
  20. class Memcachesae extends Cache
  21. {
  22. /**
  23. * 架构函数
  24. * @param array $options 缓存参数
  25. * @access public
  26. */
  27. public function __construct($options = array())
  28. {
  29. $options = array_merge(array(
  30. 'host' => C('MEMCACHE_HOST') ?: '127.0.0.1',
  31. 'port' => C('MEMCACHE_PORT') ?: 11211,
  32. 'timeout' => C('DATA_CACHE_TIMEOUT') ?: false,
  33. 'persistent' => false,
  34. ), $options);
  35. $this->options = $options;
  36. $this->options['expire'] = isset($options['expire']) ? $options['expire'] : C('DATA_CACHE_TIME');
  37. $this->options['prefix'] = isset($options['prefix']) ? $options['prefix'] : C('DATA_CACHE_PREFIX');
  38. $this->options['length'] = isset($options['length']) ? $options['length'] : 0;
  39. $this->handler = memcache_init(); //[sae] 下实例化
  40. //[sae] 下不用链接
  41. $this->connected = true;
  42. }
  43. /**
  44. * 是否连接
  45. * @access private
  46. * @return boolean
  47. */
  48. private function isConnected()
  49. {
  50. return $this->connected;
  51. }
  52. /**
  53. * 读取缓存
  54. * @access public
  55. * @param string $name 缓存变量名
  56. * @return mixed
  57. */
  58. public function get($name)
  59. {
  60. N('cache_read', 1);
  61. return $this->handler->get($_SERVER['HTTP_APPVERSION'] . '/' . $this->options['prefix'] . $name);
  62. }
  63. /**
  64. * 写入缓存
  65. * @access public
  66. * @param string $name 缓存变量名
  67. * @param mixed $value 存储数据
  68. * @param integer $expire 有效时间(秒)
  69. * @return boolean
  70. */
  71. public function set($name, $value, $expire = null)
  72. {
  73. N('cache_write', 1);
  74. if (is_null($expire)) {
  75. $expire = $this->options['expire'];
  76. }
  77. $name = $this->options['prefix'] . $name;
  78. if ($this->handler->set($_SERVER['HTTP_APPVERSION'] . '/' . $name, $value, 0, $expire)) {
  79. if ($this->options['length'] > 0) {
  80. // 记录缓存队列
  81. $this->queue($name);
  82. }
  83. return true;
  84. }
  85. return false;
  86. }
  87. /**
  88. * 删除缓存
  89. * @access public
  90. * @param string $name 缓存变量名
  91. * @return boolean
  92. */
  93. public function rm($name, $ttl = false)
  94. {
  95. $name = $_SERVER['HTTP_APPVERSION'] . '/' . $this->options['prefix'] . $name;
  96. return false === $ttl ?
  97. $this->handler->delete($name) :
  98. $this->handler->delete($name, $ttl);
  99. }
  100. /**
  101. * 清除缓存
  102. * @access public
  103. * @return boolean
  104. */
  105. public function clear()
  106. {
  107. return $this->handler->flush();
  108. }
  109. /**
  110. * 队列缓存
  111. * @access protected
  112. * @param string $key 队列名
  113. * @return mixed
  114. */
  115. //[sae] 下重写queque队列缓存方法
  116. protected function queue($key)
  117. {
  118. $queue_name = isset($this->options['queue_name']) ? $this->options['queue_name'] : 'think_queue';
  119. $value = F($queue_name);
  120. if (!$value) {
  121. $value = array();
  122. }
  123. // 进列
  124. if (false === array_search($key, $value)) {
  125. array_push($value, $key);
  126. }
  127. if (count($value) > $this->options['length']) {
  128. // 出列
  129. $key = array_shift($value);
  130. // 删除缓存
  131. $this->rm($key);
  132. if (APP_DEBUG) {
  133. //调试模式下记录出队次数
  134. $counter = Think::instance('SaeCounter');
  135. if ($counter->exists($queue_name . '_out_times')) {
  136. $counter->incr($queue_name . '_out_times');
  137. } else {
  138. $counter->create($queue_name . '_out_times', 1);
  139. }
  140. }
  141. }
  142. return F($queue_name, $value);
  143. }
  144. }