Skip to content

Commit 72574b4

Browse files
committed
Merge pull request #9 from vus520/master
Redis扩展强化及h方法添加对p标记的转换支持
2 parents 7d45a8a + dd276fb commit 72574b4

File tree

2 files changed

+130
-24
lines changed

2 files changed

+130
-24
lines changed

Extend/Driver/Cache/CacheRedis.class.php

Lines changed: 127 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,130 @@
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
/**
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+
=======
24128
* 架构函数
25129
* @param array $options 缓存参数
26130
* @access public
@@ -103,4 +207,5 @@ public function rm($name) {
103207
public function clear() {
104208
return $this->handler->flushDB();
105209
}
210+
>>>>>>> 457ff6eed9bfceb06f97a1cf86ac5d2bc2d2b952
106211
}

Extend/Function/extend.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ function h($text, $tags = null) {
208208
$text = preg_replace('/\r?\n/','',$text);
209209
//br
210210
$text = preg_replace('/<br(\s\/)?'.'>/i','[br]',$text);
211+
$text = preg_replace('/<p(\s\/)?'.'>/i','[br]',$text);
211212
$text = preg_replace('/(\[br\]\s*){10,}/i','[br]',$text);
212213
//过滤危险的属性,如:过滤on事件lang js
213214
while(preg_match('/(<[^><]+)( lang|on|action|background|codebase|dynsrc|lowsrc)[^><]+/i',$text,$mat)){
@@ -221,7 +222,7 @@ function h($text, $tags = null) {
221222
}
222223
//允许的HTML标签
223224
$text = preg_replace('/<('.$tags.')( [^><\[\]]*)>/i','[\1\2]',$text);
224-
$text = preg_replace('/<\/('.$tags.')>/Ui','[/\1]',$text);
225+
$text = preg_replace('/<\/('.$tags.')>/Ui','[/\1]',$text);
225226
//过滤多余html
226227
$text = preg_replace('/<\/?(html|head|meta|link|base|basefont|body|bgsound|title|style|script|form|iframe|frame|frameset|applet|id|ilayer|layer|name|script|style|xml)[^><]*>/i','',$text);
227228
//过滤合法的html标签
@@ -484,4 +485,4 @@ function auto_charset($fContents, $from='gbk', $to='utf-8') {
484485
else {
485486
return $fContents;
486487
}
487-
}
488+
}

0 commit comments

Comments
 (0)