Redis.class.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2014 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. * Redis缓存驱动
  16. * 要求安装phpredis扩展:https://github.com/nicolasff/phpredis
  17. */
  18. class Redis extends Cache {
  19. /**
  20. * 架构函数
  21. * @param array $options 缓存参数
  22. * @access public
  23. */
  24. public function __construct($options=array()) {
  25. if ( !extension_loaded('redis') ) {
  26. E(L('_NOT_SUPPORT_').':redis');
  27. }
  28. if(empty($options)) {
  29. $options = array (
  30. 'host' => C('REDIS_HOST') ? C('REDIS_HOST') : '127.0.0.1',
  31. 'port' => C('REDIS_PORT') ? C('REDIS_PORT') : 6379,
  32. 'timeout' => C('DATA_CACHE_TIMEOUT') ? C('DATA_CACHE_TIMEOUT') : false,
  33. 'auth' => C('REDIS_AUTH_PASSWORD') ? C('REDIS_AUTH_PASSWORD'):null,//auth认证的密码
  34. 'persistent' => false,
  35. );
  36. }
  37. $this->options = $options;
  38. $this->options['expire'] = isset($options['expire'])? $options['expire'] : C('DATA_CACHE_TIME');
  39. $this->options['prefix'] = isset($options['prefix'])? $options['prefix'] : C('DATA_CACHE_PREFIX');
  40. $this->options['length'] = isset($options['length'])? $options['length'] : 0;
  41. $func = $options['persistent'] ? 'pconnect' : 'connect';
  42. $this->handler = new \Redis;
  43. $options['timeout'] === false ?
  44. $this->handler->$func($options['host'], $options['port']) :
  45. $this->handler->$func($options['host'], $options['port'], $options['timeout']);
  46. if($this->options['auth']!==null)
  47. {
  48. $this->handler->auth($this->options['auth']); //说明有配置redis的认证配置密码 需要认证一下
  49. }
  50. }
  51. /**
  52. * 读取缓存
  53. * @access public
  54. * @param string $name 缓存变量名
  55. * @return mixed
  56. */
  57. public function get($name) {
  58. N('cache_read',1);
  59. $value = $this->handler->get($this->options['prefix'].$name);
  60. $jsonData = json_decode( $value, true );
  61. return ($jsonData === NULL) ? $value : $jsonData; //检测是否为JSON数据 true 返回JSON解析数组, false返回源数据
  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. N('cache_write',1);
  73. if(is_null($expire)) {
  74. $expire = $this->options['expire'];
  75. }
  76. $name = $this->options['prefix'].$name;
  77. //对数组/对象数据进行缓存处理,保证数据完整性
  78. $value = (is_object($value) || is_array($value)) ? json_encode($value) : $value;
  79. if(is_int($expire) && $expire) {
  80. $result = $this->handler->setex($name, $expire, $value);
  81. }else{
  82. $result = $this->handler->set($name, $value);
  83. }
  84. if($result && $this->options['length']>0) {
  85. // 记录缓存队列
  86. $this->queue($name);
  87. }
  88. return $result;
  89. }
  90. /**
  91. * 删除缓存
  92. * @access public
  93. * @param string $name 缓存变量名
  94. * @return boolean
  95. */
  96. public function rm($name) {
  97. return $this->handler->delete($this->options['prefix'].$name);
  98. }
  99. /**
  100. * 清除缓存
  101. * @access public
  102. * @return boolean
  103. */
  104. public function clear() {
  105. return $this->handler->flushDB();
  106. }
  107. }