-
Notifications
You must be signed in to change notification settings - Fork 0
/
phpredis_cleint
112 lines (93 loc) · 2.47 KB
/
phpredis_cleint
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// --------------------------------------
// 設定一個程式暫存的 key
// 參考文件:https://github.com/phpredis/phpredis#connect-open
// 成功傳回 1 失敗傳回 0
// --------------------------------------
function setakey($key, $value, $db=3, $expire=3600) {
global $redisdb;
$redis = new Redis();
// 2 秒 timeout
if($redis->pconnect($redisdb['host'], 6379, 1)) {
// success
if($redis->auth($redisdb['auth'])) {
// echo 'Authentication Success';
}else{
return(0);
die('Authentication failed');
}
}else{
// error
return(0);
die('Connection Failed');
}
// 選擇 DB
$redis->select($db);
// echo "Server is running: ".$redis->ping();
// 設定 timeout 值, 目前的時間往後加上去
$server_time = time(true);
// $expire = 14400;
$expire_time = $server_time + $expire;
$r[1] = $redis->set($key, $value);
$r[2] = $redis->expireAt($key, $expire_time);
// var_dump($r);
return($r);
}
// --------------------------------------
// 刪除一個 程式暫存的 key
// 參考文件:https://github.com/phpredis/phpredis#connect-open
// 成功傳回 1 失敗傳回 0
// --------------------------------------
function delakey($key,$db=3) {
global $redisdb;
$redis = new Redis();
// 2 秒 timeout
if($redis->pconnect($redisdb['host'], 6379, 1)) {
// success
if($redis->auth($redisdb['auth'])) {
// echo 'Authentication Success';
}else{
return(0);
die('Authentication failed');
}
}else{
// error
return(0);
die('Connection Failed');
}
// 選擇 DB
$redis->select($db);
// echo "Server is running: ".$redis->ping();
$r = $redis->delete($key);
// Long Number of keys deleted.
return($r);
}
// --------------------------------------
// 刪除一個 程式暫存的 key
// 參考文件:https://github.com/phpredis/phpredis#connect-open
// 成功傳回 1 失敗傳回 0
// --------------------------------------
function getakey($key,$db=3) {
global $redisdb;
$redis = new Redis();
// 2 秒 timeout
if($redis->pconnect($redisdb['host'], 6379, 1)) {
// success
if($redis->auth($redisdb['auth'])) {
// echo 'Authentication Success';
}else{
return(0);
die('Authentication failed');
}
}else{
// error
return(0);
die('Connection Failed');
}
// 選擇 DB
$redis->select($db);
// echo "Server is running: ".$redis->ping();
// 取得 key 的 value
$r = $redis->get($key);
// String or Bool: If key didn't exist, FALSE is returned. Otherwise, the value related to this key is returned.
return($r);
}