1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: zjw |
5
|
|
|
* Date: 2017/8/25 |
6
|
|
|
* Time: 下午3:59 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace monsterhunter\redis\relational; |
10
|
|
|
|
11
|
|
|
class RedisClient |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var \Predis\Client | \Redis |
15
|
|
|
*/ |
16
|
|
|
public $client; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
public $params; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string client类 |
25
|
|
|
*/ |
26
|
|
|
public $clientClass; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* 初始化 |
30
|
|
|
*/ |
31
|
6 |
|
public function init() |
32
|
|
|
{ |
33
|
6 |
|
if ($this->params['password'] == 'null' || $this->params['password'] == '') { |
34
|
6 |
|
unset($this->params['password']); |
35
|
|
|
} |
36
|
6 |
|
$this->client = new $this->clientClass($this->params); |
37
|
6 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* 设置记录 |
41
|
|
|
* @param $token string |
42
|
|
|
* @param array $arr ['access_token'=>'','client_id' => 'client1', 'expires'=>1, 'union_id' => 'union_id', 'user_id' => '1', 'scope'=>1]; |
43
|
|
|
*/ |
44
|
6 |
|
public function hset($token, array $arr) |
45
|
|
|
{ |
46
|
6 |
|
$tokenKey = $this->getPrefix() . $token; |
47
|
6 |
|
$this->client->hmset($tokenKey, $arr); |
48
|
6 |
|
$extraKey = $this->buildExtraKey($token, $arr); |
49
|
6 |
|
$this->client->set($extraKey, $token); |
50
|
6 |
|
if (isset($arr['expires']) && intval($arr['expires']) > time()) { |
51
|
6 |
|
$this->client->expire($tokenKey, $arr['expires'] - time()); |
52
|
6 |
|
$this->client->expire($extraKey, $arr['expires'] - time()); |
53
|
|
|
} |
54
|
6 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* 通过键获取记录 |
58
|
|
|
* @param $token |
59
|
|
|
* @return array |
60
|
|
|
*/ |
61
|
6 |
|
public function hget($token) |
62
|
|
|
{ |
63
|
6 |
|
$key = $this->getPrefix() . $token; |
64
|
6 |
|
return $this->client->hgetall($key); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* 通过键删除 |
69
|
|
|
* @param $token |
70
|
|
|
*/ |
71
|
5 |
|
public function deleteByToken($token) |
72
|
|
|
{ |
73
|
5 |
|
$key = $this->getPrefix() . $token; |
74
|
5 |
|
$this->client->del([$key]); |
75
|
5 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* 删除与用户相关的所有记录 |
79
|
|
|
* delete by user_id |
80
|
|
|
* @param $userId |
81
|
|
|
*/ |
82
|
1 |
|
public function deleteByUserId($userId) |
83
|
|
|
{ |
84
|
1 |
|
$userIdKeys = $this->getKeysByUserId($userId); |
85
|
1 |
|
$this->delKeys($userIdKeys); |
86
|
1 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* 删除与clientId和userId相关的所有记录 |
90
|
|
|
* @param $clientId |
91
|
|
|
* @param $userId |
92
|
|
|
*/ |
93
|
1 |
|
public function deleteByUserIdAndClientId($clientId, $userId) |
94
|
|
|
{ |
95
|
1 |
|
$userIidClientIdKeys = $this->getKeysByClientIdAndUserId($clientId, $userId); |
96
|
1 |
|
$this->delKeys($userIidClientIdKeys); |
97
|
1 |
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* 删除与clientId有关的所有token |
101
|
|
|
* @param $clientId |
102
|
|
|
* @return void |
103
|
|
|
*/ |
104
|
1 |
|
public function deleteByClientId($clientId) |
105
|
|
|
{ |
106
|
1 |
|
$clientIdKeys = $this->getKeysByClientId($clientId); |
107
|
1 |
|
$this->delKeys($clientIdKeys); |
108
|
1 |
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* 删除当前记录之外的所有记录 |
112
|
|
|
* @param $currentToken |
113
|
|
|
* @param $clientId |
114
|
|
|
* @param $userId |
115
|
|
|
*/ |
116
|
1 |
|
public function revokeOldToken($currentToken, $clientId, $userId) |
117
|
|
|
{ |
118
|
1 |
|
$clientIidIUserIdKeys = $this->getKeysByClientIdAndUserId($clientId, $userId); |
119
|
1 |
|
if (count($clientIidIUserIdKeys) > 0) { |
120
|
1 |
|
foreach ($clientIidIUserIdKeys as $key => $v) { |
121
|
1 |
|
$token = $this->client->get($v); |
122
|
1 |
|
if ($token !== $currentToken) { |
123
|
1 |
|
$this->deleteByToken($token); |
124
|
1 |
|
$this->client->del($v); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
} |
128
|
1 |
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* 生成关联键值对 |
132
|
|
|
* @param $token |
133
|
|
|
* @param array $arr ['access_token'=>'','client_id' => 'client1', 'expires'=>1, 'union_id' => 'union_id', 'user_id' => '1', 'scope'=>1]; |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
6 |
|
private function buildExtraKey($token, array $arr) |
137
|
|
|
{ |
138
|
6 |
|
$clientIdUserIdKey = "client_id" . "_" . "{$arr['client_id']}" . "-" . "user_id" . "_" . "{$arr['user_id']}" . "-" . "access_token" . "_" . $token . "-" . "union_id" . "_" . "{$arr['union_id']}"; |
139
|
6 |
|
return $this->getPrefix() . $clientIdUserIdKey; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* 客户端id和用户id 模糊匹配出所有键 |
144
|
|
|
* @param $clientId |
145
|
|
|
* @param $userId |
146
|
|
|
* @return array |
147
|
|
|
*/ |
148
|
2 |
|
private function getKeysByClientIdAndUserId($clientId, $userId) |
149
|
|
|
{ |
150
|
2 |
|
$clientIdUserIdKeys = $this->getPrefix() . "client_id" . "_" . "{$clientId}" . "-" . "user_id" . "_" . "{$userId}" . "-*"; |
151
|
2 |
|
return $this->client->keys($clientIdUserIdKeys); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* 通过clientId取出clientId对应的与主记录关联的键数组 |
156
|
|
|
* @param $clientId |
157
|
|
|
* @return array |
158
|
|
|
*/ |
159
|
1 |
|
private function getKeysByClientId($clientId) |
160
|
|
|
{ |
161
|
1 |
|
$clientIdKey = $this->getPrefix() . "client_id" . "_" . "{$clientId}" . "-*"; |
162
|
1 |
|
return $this->client->keys($clientIdKey); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* 通过用户ID取出用户对应的与主记录关联的键数组 |
167
|
|
|
* @param $userId |
168
|
|
|
* @return array |
169
|
|
|
*/ |
170
|
1 |
|
private function getKeysByUserId($userId) |
171
|
|
|
{ |
172
|
1 |
|
$userIdKey = $this->getPrefix() . "*-user_id" . "_" . "{$userId}" . "-*"; |
173
|
1 |
|
return $this->client->keys($userIdKey); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* 删除与key有关的所有记录 |
178
|
|
|
* @param $keys |
179
|
|
|
*/ |
180
|
3 |
|
private function delKeys($keys) |
181
|
|
|
{ |
182
|
3 |
|
if (count($keys) > 0) { |
183
|
3 |
|
foreach ($keys as $key => $v) { |
184
|
3 |
|
$token = $this->client->get($v); |
185
|
3 |
|
$this->deleteByToken($token); |
186
|
3 |
|
$this->client->del($v); |
187
|
|
|
} |
188
|
|
|
} |
189
|
3 |
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* 所有记录的key值前缀 |
193
|
|
|
* @return string |
194
|
|
|
*/ |
195
|
6 |
|
private function getPrefix() |
196
|
|
|
{ |
197
|
6 |
|
return ""; |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|