CacheRedis.class.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. defined('THINK_PATH') or exit();
  12. /**
  13. * Redis缓存驱动
  14. * 要求安装phpredis扩展:https://github.com/nicolasff/phpredis
  15. * @category Extend
  16. * @package Extend
  17. * @subpackage Driver.Cache
  18. * @author 尘缘 <130775@qq.com>
  19. */
  20. class CacheRedis extends Cache {
  21. /**
  22. * 架构函数
  23. * @param array $options 缓存参数
  24. * @access public
  25. */
  26. public function __construct($options=array()) {
  27. if ( !extension_loaded('redis') ) {
  28. throw_exception(L('_NOT_SUPPERT_').':redis');
  29. }
  30. if(empty($options)) {
  31. $options = array (
  32. 'host' => C('REDIS_HOST') ? C('REDIS_HOST') : '127.0.0.1',
  33. 'port' => C('REDIS_PORT') ? C('REDIS_PORT') : 6379,
  34. 'timeout' => C('DATA_CACHE_TIMEOUT') ? C('DATA_CACHE_TIMEOUT') : false,
  35. 'persistent' => false,
  36. );
  37. }
  38. $this->options = $options;
  39. $this->options['expire'] = isset($options['expire'])? $options['expire'] : C('DATA_CACHE_TIME');
  40. $this->options['prefix'] = isset($options['prefix'])? $options['prefix'] : C('DATA_CACHE_PREFIX');
  41. $this->options['length'] = isset($options['length'])? $options['length'] : 0;
  42. $func = $options['persistent'] ? 'pconnect' : 'connect';
  43. $this->handler = new Redis;
  44. $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 public
  51. * @param string $name 缓存变量名
  52. * @return mixed
  53. */
  54. public function get($name) {
  55. N('cache_read',1);
  56. $value = $this->handler->get($this->options['prefix'].$name);
  57. $jsonData = json_decode( $value, true );
  58. return ($jsonData === NULL) ? $value : $jsonData; //检测是否为JSON数据 true 返回JSON解析数组, false返回源数据
  59. }
  60. /**
  61. * 写入缓存
  62. * @access public
  63. * @param string $name 缓存变量名
  64. * @param mixed $value 存储数据
  65. * @param integer $expire 有效时间(秒)
  66. * @return boolen
  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. //对数组/对象数据进行缓存处理,保证数据完整性
  75. $value = (is_object($value) || is_array($value)) ? json_encode($value) : $value;
  76. if(is_int($expire)) {
  77. $result = $this->handler->setex($name, $expire, $value);
  78. }else{
  79. $result = $this->handler->set($name, $value);
  80. }
  81. if($result && $this->options['length']>0) {
  82. // 记录缓存队列
  83. $this->queue($name);
  84. }
  85. return $result;
  86. }
  87. /**
  88. * 删除缓存
  89. * @access public
  90. * @param string $name 缓存变量名
  91. * @return boolen
  92. */
  93. public function rm($name) {
  94. return $this->handler->delete($this->options['prefix'].$name);
  95. }
  96. /**
  97. * 清除缓存
  98. * @access public
  99. * @return boolen
  100. */
  101. public function clear() {
  102. return $this->handler->flushDB();
  103. }
  104. }