Redis.class.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. $options = array_merge(array (
  29. 'host' => C('REDIS_HOST') ? : '127.0.0.1',
  30. 'port' => C('REDIS_PORT') ? : 6379,
  31. 'password' => '',
  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. $func = $options['persistent'] ? 'pconnect' : 'connect';
  40. $this->handler = new \Redis;
  41. $options['timeout'] === false ?
  42. $this->handler->$func($options['host'], $options['port']) :
  43. $this->handler->$func($options['host'], $options['port'], $options['timeout']);
  44. $this->handler->auth(C('REDIS_AUTH'));
  45. }
  46. /**
  47. * 读取缓存
  48. * @access public
  49. * @param string $name 缓存变量名
  50. * @return mixed
  51. */
  52. public function get($name) {
  53. N('cache_read',1);
  54. $value = $this->handler->get($this->options['prefix'].$name);
  55. $jsonData = json_decode( $value, true );
  56. return ($jsonData === NULL) ? $value : $jsonData; //检测是否为JSON数据 true 返回JSON解析数组, false返回源数据
  57. }
  58. /**
  59. * 写入缓存
  60. * @access public
  61. * @param string $name 缓存变量名
  62. * @param mixed $value 存储数据
  63. * @param integer $expire 有效时间(秒)
  64. * @return boolean
  65. */
  66. public function set($name, $value, $expire = null) {
  67. N('cache_write',1);
  68. if(is_null($expire)) {
  69. $expire = $this->options['expire'];
  70. }
  71. $name = $this->options['prefix'].$name;
  72. //对数组/对象数据进行缓存处理,保证数据完整性
  73. $value = (is_object($value) || is_array($value)) ? json_encode($value) : $value;
  74. if(is_int($expire) && $expire) {
  75. $result = $this->handler->setex($name, $expire, $value);
  76. }else{
  77. $result = $this->handler->set($name, $value);
  78. }
  79. if($result && $this->options['length']>0) {
  80. // 记录缓存队列
  81. $this->queue($name);
  82. }
  83. return $result;
  84. }
  85. /**
  86. * 删除缓存
  87. * @access public
  88. * @param string $name 缓存变量名
  89. * @return boolean
  90. */
  91. public function rm($name) {
  92. return $this->handler->delete($this->options['prefix'].$name);
  93. }
  94. /**
  95. * 清除缓存
  96. * @access public
  97. * @return boolean
  98. */
  99. public function clear() {
  100. return $this->handler->flushDB();
  101. }
  102. }