Redis.class.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. 'auth' => C('REDIS_AUTH') ? : null,
  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. //支持认证模式 @author 1135
  45. if($this->options['auth']!=null)
  46. {
  47. $this->handler->auth($this->options['auth']);
  48. }
  49. //支持选择实例 @author 1135
  50. $this->options['db'] = isset($this->options['db']) ? $this->options['db'] : 0;
  51. $this->handler->select($this->options['db']);
  52. }
  53. /**
  54. * 读取缓存
  55. * @access public
  56. * @param string $name 缓存变量名
  57. * @return mixed
  58. public function get($name) {
  59. N('cache_read',1);
  60. $value = $this->handler->get($this->options['prefix'].$name);
  61. $jsonData = json_decode( $value, true );
  62. return ($jsonData === NULL) ? $value : $jsonData; //检测是否为JSON数据 true 返回JSON解析数组, false返回源数据
  63. }*/
  64. /**
  65. * 删除缓存
  66. * @access public
  67. * @param string $name 缓存变量名
  68. * @return boolean
  69. */
  70. public function rm($name) {
  71. return $this->handler->delete($this->options['prefix'].$name);
  72. }
  73. /**
  74. * 清除缓存
  75. * @access public
  76. * @return boolean
  77. */
  78. public function clear() {
  79. return $this->handler->flushDB();
  80. }
  81. }