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
-
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
+
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
+
23
117
/**
118
+ <<<<<<< HEAD
119
+ * 魔术方法,phpRedis中所包含的所有操作均可以通过魔术方法直接调用
120
+ * @access public
121
+ * @return mixed
122
+ */
123
+ function __call ($ method , $ args )
124
+ {
125
+ return call_user_func_array (array ($ this ->handler , $ method ) , $ args );
126
+ }
127
+ =======
24
128
* 架构函数
25
129
* @param array $ options 缓存参数
26
130
* @access public
@@ -103,4 +207,5 @@ public function rm($name) {
103
207
public function clear () {
104
208
return $ this ->handler ->flushDB ();
105
209
}
210
+ >>>>>>> 457 ff6eed9bfceb06f97a1cf86ac5d2bc2d2b952
106
211
}
0 commit comments