You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
4.5 KiB

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