Memcachesae.class.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. if(empty($options)) {
  29. $options = array (
  30. 'host' => C('MEMCACHE_HOST') ? C('MEMCACHE_HOST') : '127.0.0.1',
  31. 'port' => C('MEMCACHE_PORT') ? C('MEMCACHE_PORT') : 11211,
  32. 'timeout' => C('DATA_CACHE_TIMEOUT') ? C('DATA_CACHE_TIMEOUT') : false,
  33. 'persistent' => false,
  34. );
  35. }
  36. $this->options = $options;
  37. $this->options['expire'] = isset($options['expire'])? $options['expire'] : C('DATA_CACHE_TIME');
  38. $this->options['prefix'] = isset($options['prefix'])? $options['prefix'] : C('DATA_CACHE_PREFIX');
  39. $this->options['length'] = isset($options['length'])? $options['length'] : 0;
  40. // $func = isset($options['persistent']) ? 'pconnect' : 'connect';
  41. $this->handler = memcache_init();//[sae] 下实例化
  42. //[sae] 下不用链接
  43. $this->connected=true;
  44. // $this->connected = $options['timeout'] === false ?
  45. // $this->handler->$func($options['host'], $options['port']) :
  46. // $this->handler->$func($options['host'], $options['port'], $options['timeout']);
  47. }
  48. /**
  49. * 是否连接
  50. * @access private
  51. * @return boolean
  52. */
  53. private function isConnected() {
  54. return $this->connected;
  55. }
  56. /**
  57. * 读取缓存
  58. * @access public
  59. * @param string $name 缓存变量名
  60. * @return mixed
  61. */
  62. public function get($name) {
  63. N('cache_read',1);
  64. return $this->handler->get($_SERVER['HTTP_APPVERSION'].'/'.$this->options['prefix'].$name);
  65. }
  66. /**
  67. * 写入缓存
  68. * @access public
  69. * @param string $name 缓存变量名
  70. * @param mixed $value 存储数据
  71. * @param integer $expire 有效时间(秒)
  72. * @return boolean
  73. */
  74. public function set($name, $value, $expire = null) {
  75. N('cache_write',1);
  76. if(is_null($expire)) {
  77. $expire = $this->options['expire'];
  78. }
  79. $name = $this->options['prefix'].$name;
  80. if($this->handler->set($_SERVER['HTTP_APPVERSION'].'/'.$name, $value, 0, $expire)) {
  81. if($this->options['length']>0) {
  82. // 记录缓存队列
  83. $this->queue($name);
  84. }
  85. return true;
  86. }
  87. return false;
  88. }
  89. /**
  90. * 删除缓存
  91. * @access public
  92. * @param string $name 缓存变量名
  93. * @return boolean
  94. */
  95. public function rm($name, $ttl = false) {
  96. $name = $_SERVER['HTTP_APPVERSION'].'/'.$this->options['prefix'].$name;
  97. return $ttl === false ?
  98. $this->handler->delete($name) :
  99. $this->handler->delete($name, $ttl);
  100. }
  101. /**
  102. * 清除缓存
  103. * @access public
  104. * @return boolean
  105. */
  106. public function clear() {
  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. $queue_name=isset($this->options['queue_name'])?$this->options['queue_name']:'think_queue';
  118. $value = F($queue_name);
  119. if(!$value) {
  120. $value = array();
  121. }
  122. // 进列
  123. if(false===array_search($key, $value)) array_push($value,$key);
  124. if(count($value) > $this->options['length']) {
  125. // 出列
  126. $key = array_shift($value);
  127. // 删除缓存
  128. $this->rm($key);
  129. if (APP_DEBUG) {
  130. //调试模式下记录出队次数
  131. $counter = Think::instance('SaeCounter');
  132. if ($counter->exists($queue_name.'_out_times'))
  133. $counter->incr($queue_name.'_out_times');
  134. else
  135. $counter->create($queue_name.'_out_times', 1);
  136. }
  137. }
  138. return F($queue_name,$value);
  139. }
  140. }