Skip to content

Commit 3e54cea

Browse files
committed
丰富phpRedis功能,支持保存数组,支持key前缀,添加魔术方法以支持phpRedis的所有功能
1 parent a59a949 commit 3e54cea

File tree

1 file changed

+124
-113
lines changed

1 file changed

+124
-113
lines changed
Lines changed: 124 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -1,115 +1,126 @@
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 <[email protected]>
10-
// +----------------------------------------------------------------------
11-
12-
defined('THINK_PATH') or exit();
13-
/**
14-
* Redis缓存驱动
15-
* 要求安装phpredis扩展:https://github.com/owlient/phpredis
16-
* @category Extend
17-
* @package Extend
18-
* @subpackage Driver.Cache
19-
* @author 尘缘 <[email protected]>
20-
*/
21-
class CacheRedis extends Cache {
22-
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 <[email protected]>
10+
// +----------------------------------------------------------------------
11+
12+
defined('THINK_PATH') or exit();
13+
14+
/**
15+
* Redis缓存驱动
16+
* 要求安装phpredis扩展:https://github.com/nicolasff/phpredis
17+
* @category Extend
18+
* @package Extend
19+
* @subpackage Driver.Cache
20+
* @author 尘缘 <[email protected]>
21+
*/
22+
class CacheRedis extends Cache {
23+
24+
/**
25+
* 构造函数
26+
* @param array $options 缓存参数
27+
* @access public
28+
*/
29+
public function __construct($options=array()) {
30+
if ( !extension_loaded('redis') ) {
31+
throw_exception(L('_NOT_SUPPERT_').':redis');
32+
}
33+
if(empty($options)) {
34+
$options = array (
35+
'host' => C('REDIS_HOST') ? C('REDIS_HOST') : '127.0.0.1',
36+
'port' => C('REDIS_PORT') ? C('REDIS_PORT') : 6379,
37+
'timeout' => C('DATA_CACHE_TIMEOUT') ? C('DATA_CACHE_TIMEOUT') : false,
38+
'persistent' => false,
39+
);
40+
}
41+
$this->options = $options;
42+
$this->options['expire'] = isset($options['expire']) ? $options['expire'] : C('DATA_CACHE_TIME');
43+
$this->options['prefix'] = isset($options['prefix']) ? $options['prefix'] : C('DATA_CACHE_PREFIX');
44+
$this->options['length'] = isset($options['length']) ? $options['length'] : 0;
45+
46+
$this->handler = new Redis;
47+
$func = $options['persistent'] ? 'pconnect' : 'connect';
48+
$options['timeout'] === false ? $this->handler->$func($options['host'], $options['port']) : $this->handler->$func($options['host'], $options['port'], $options['timeout']);
49+
$this->handler->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP);
50+
$this->handler->setOption(Redis::OPT_PREFIX, $this->options['prefix']);
51+
}
52+
53+
/**
54+
* 是否连接
55+
* @access private
56+
* @return boolen
57+
*/
58+
private function isConnected() {
59+
return '+PONG' == $this->handler->ping() ? true : false;
60+
}
61+
62+
/**
63+
* 读取缓存
64+
* @access public
65+
* @param string $name 缓存变量名
66+
* @return mixed
67+
*/
68+
public function get($name) {
69+
N('cache_read',1);
70+
return $this->handler->get($name);
71+
}
72+
73+
/**
74+
* 写入缓存
75+
* @access public
76+
* @param string $name 缓存变量名
77+
* @param mixed $value 存储数据
78+
* @param integer $expire 有效时间(秒)
79+
* @return boolen
80+
*/
81+
public function set($name, $value, $expire = null) {
82+
N('cache_write',1);
83+
if(is_null($expire)) {
84+
$expire = $this->options['expire'];
85+
}
86+
if(is_int($expire)) {
87+
$result = $this->handler->setex($name, $expire, $value);
88+
}else{
89+
$result = $this->handler->set($name, $value);
90+
}
91+
if($result && $this->options['length']>0) {
92+
// 记录缓存队列
93+
$this->queue($name);
94+
}
95+
return $result;
96+
}
97+
98+
/**
99+
* 删除缓存
100+
* @access public
101+
* @param string $name 缓存变量名
102+
* @return boolen
103+
*/
104+
public function rm($name) {
105+
return $this->handler->delete($name);
106+
}
107+
108+
/**
109+
* 清除缓存
110+
* @access public
111+
* @return boolen
112+
*/
113+
public function clear() {
114+
return $this->handler->flushDB();
115+
}
116+
23117
/**
24-
* 架构函数
25-
* @param array $options 缓存参数
26-
* @access public
27-
*/
28-
public function __construct($options=array()) {
29-
if ( !extension_loaded('redis') ) {
30-
throw_exception(L('_NOT_SUPPERT_').':redis');
31-
}
32-
if(empty($options)) {
33-
$options = array (
34-
'host' => C('REDIS_HOST') ? C('REDIS_HOST') : '127.0.0.1',
35-
'port' => C('REDIS_PORT') ? C('REDIS_PORT') : 6379,
36-
'timeout' => C('DATA_CACHE_TIMEOUT') ? C('DATA_CACHE_TIMEOUT') : false,
37-
'persistent' => false,
38-
);
39-
}
40-
$this->options = $options;
41-
$this->options['expire'] = isset($options['expire'])? $options['expire'] : C('DATA_CACHE_TIME');
42-
$this->options['prefix'] = isset($options['prefix'])? $options['prefix'] : C('DATA_CACHE_PREFIX');
43-
$this->options['length'] = isset($options['length'])? $options['length'] : 0;
44-
$func = $options['persistent'] ? 'pconnect' : 'connect';
45-
$this->handler = new Redis;
46-
$this->connected = $options['timeout'] === false ?
47-
$this->handler->$func($options['host'], $options['port']) :
48-
$this->handler->$func($options['host'], $options['port'], $options['timeout']);
49-
}
50-
51-
/**
52-
* 是否连接
53-
* @access private
54-
* @return boolen
55-
*/
56-
private function isConnected() {
57-
return $this->connected;
58-
}
59-
60-
/**
61-
* 读取缓存
62-
* @access public
63-
* @param string $name 缓存变量名
64-
* @return mixed
65-
*/
66-
public function get($name) {
67-
N('cache_read',1);
68-
return $this->handler->get($this->options['prefix'].$name);
69-
}
70-
71-
/**
72-
* 写入缓存
73-
* @access public
74-
* @param string $name 缓存变量名
75-
* @param mixed $value 存储数据
76-
* @param integer $expire 有效时间(秒)
77-
* @return boolen
78-
*/
79-
public function set($name, $value, $expire = null) {
80-
N('cache_write',1);
81-
if(is_null($expire)) {
82-
$expire = $this->options['expire'];
83-
}
84-
$name = $this->options['prefix'].$name;
85-
if(is_int($expire)) {
86-
$result = $this->handler->setex($name, $expire, $value);
87-
}else{
88-
$result = $this->handler->set($name, $value);
89-
}
90-
if($result && $this->options['length']>0) {
91-
// 记录缓存队列
92-
$this->queue($name);
93-
}
94-
return $result;
95-
}
96-
97-
/**
98-
* 删除缓存
99-
* @access public
100-
* @param string $name 缓存变量名
101-
* @return boolen
102-
*/
103-
public function rm($name) {
104-
return $this->handler->delete($this->options['prefix'].$name);
105-
}
106-
107-
/**
108-
* 清除缓存
109-
* @access public
110-
* @return boolen
111-
*/
112-
public function clear() {
113-
return $this->handler->flushDB();
114-
}
118+
* 魔术方法,phpRedis中所包含的所有操作均可以通过魔术方法直接调用
119+
* @access public
120+
* @return mixed
121+
*/
122+
function __call($method, $args)
123+
{
124+
return call_user_func_array(array($this->handler, $method) , $args);
125+
}
115126
}

0 commit comments

Comments
 (0)