Skip to content

Commit d987634

Browse files
committed
Merge pull request #14 from daivem/feature/dv
Feature/dv
2 parents b7606e2 + 1894a54 commit d987634

File tree

5 files changed

+50
-3
lines changed

5 files changed

+50
-3
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ application/config/config_auth.php # 登录及验证码配置
3838
-----------------------------------------------
3939
更新日志:
4040

41+
v0.3.6 (2015-12-11)
42+
43+
增加对redis自带的auth身份验证的支持(详见application/config/config_redis.php)
44+
45+
修改string类型的key的value时,可选择保持其原有生存期(ttl)不变
46+
47+
——————————————————
48+
4149
v0.3.5 (2015-11-07)
4250

4351
修正了部分JS的错误

application/config/config_redis.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
'name' => 'localhost server',
66
'host' => '127.0.0.1',
77
'port' => 6379,
8+
'auth' => FALSE, //如无密码,可不设置此键,或将值设置为FALSE or NULL
89
),
910
/*
1011
* 集群服务器
@@ -21,5 +22,5 @@
2122
'127.0.0.1:7012',
2223
),
2324
),
24-
*/
25+
*/
2526
);

application/controllers/edit.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,18 @@ private function _do_index()
9797

9898
if ( $type == 'string' ) {
9999
//string
100+
$keep_ttl = get_post_arg('keep_ttl', 0, 'intval');
101+
$orig_ttl = 0;
102+
if ( $keep_ttl ) {
103+
$orig_ttl = (int)$redis -> ttl($key);
104+
}
100105
$result = $redis -> set($key, $value);
101106
if ( ! $result ) {
102107
show_error('操作失败');
103108
}
109+
if ($orig_ttl > 0) {
110+
$redis -> expire($key, $orig_ttl);
111+
}
104112
} elseif ( $type == 'hash' ) {
105113
//hash
106114
$hkey = get_post_arg('hkey');
@@ -116,7 +124,7 @@ private function _do_index()
116124
&& ( ! $redis -> hExists($key, $hkey) )
117125
){
118126
//如果新的hkey不存在的话
119-
//删掉用来的旧KEY
127+
//删掉原来的旧KEY
120128
$redis -> hDel($key, $old_hkey);
121129
}
122130

application/models/redis_model.php

+23-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class Redis_Model extends CI_Model {
77
private $_host;
88
private $_port;
99
private $_db;
10+
private $_auth;
1011

1112
public function __construct()
1213
{
@@ -50,6 +51,10 @@ private function _init_cluster($redis_config)
5051
} catch (Exception $e) {
5152
show_error('Can not connect to Redis Cluster. Message:' . $e -> getMessage());
5253
}
54+
55+
if ( ! $this -> auth() ) {
56+
show_error('Redis Server (' . $this -> _host . ':' . $this -> _port . ') 认证密码错误!');
57+
}
5358
}
5459

5560

@@ -60,6 +65,7 @@ private function _init_redis($redis_config)
6065
$this -> _host = $redis_config['host'];
6166
$this -> _port = isset($redis_config['port']) ? $redis_config['port'] : 6379;
6267
$this -> _db = isset($redis_config['db']) ? $redis_config['db'] : 0;
68+
$this -> _auth = isset($redis_config['auth']) ? $redis_config['auth'] : FALSE;
6369

6470

6571
$this -> _redis = new Redis();
@@ -101,9 +107,25 @@ public function select_db($db)
101107
{
102108
$db = (int)$db;
103109
$this -> _db = $db;
104-
$this -> _redis -> select($this -> _db);
110+
try {
111+
$this -> _redis -> select($this -> _db);
112+
} catch (Exception $e) {
113+
show_error('连接服务器时发生错误:' . $e -> getMessage());
114+
}
105115
}
106116

117+
/**
118+
* 设置认证密码
119+
*/
120+
public function auth()
121+
{
122+
if ( ( FALSE !== $this -> _auth )
123+
&& ( NULL !== $this -> _auth )
124+
){
125+
return $this -> _redis -> auth($this -> _auth);
126+
}
127+
return TRUE;
128+
}
107129

108130
/**
109131
*

application/views/edit.php

+8
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,15 @@
3535
<textarea name="value" id="value" cols="80" rows="20"><?= nl2br(format_html($value))?></textarea>
3636
</p>
3737
<input type="hidden" name="oldvalue" value="<?= format_html($value)?>">
38+
<?php
39+
if ( ($type == 'string') && ($is_edit) ) {
40+
?>
41+
<p><input type="checkbox" name="keep_ttl" value="1" checked>保持生存期(ttl)不变</p>
42+
<?php
43+
}
44+
?>
3845
<p>
46+
3947
<input type="submit" class="button" value="<?= $is_edit ? '编辑' : '新增'?>">
4048
</p>
4149
</form>

0 commit comments

Comments
 (0)