hxp
2025-06-04 f4a514d5ac952110da846636ecbb9de951eaf3d2
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<?php
 
namespace RedisOper;
 
include_once "/Common/CommFunc.php";
include_once "/Common/Logging.php";
 
class RedisPool
{
    // 注:由于php特殊原因,单次请求结束后会释放所有内存及链接,window系统下暂时无法处理链接池,故暂时只处理单次请求仅创建一个链接
    private static $redis = null;
 
    public static function Get($pconnet = false)
    {
        if (self::$redis) {
            // \Logging\LogInfo("get static Redis ok! ");
            return self::$redis;
        }
 
        if (!\CommFunc\GetConfig("Redis", "Enable", $Enable) || !$Enable) {
            // \Logging\LogInfo("不启用redis!");
            return;
        }
 
        if (
            !\CommFunc\GetConfig("Redis", "Host", $Host) || !\CommFunc\GetConfig("Redis", "Port", $Port) ||
            !\CommFunc\GetConfig("Redis", "Psw", $Psw) || !\CommFunc\GetConfig("Redis", "Select", $Select)
        ) {
            return;
        }
 
        $rd = new \Redis();
        if ($pconnet) {
            $cntOK = $rd->pconnect($Host, $Port, 0);
        } else {
            $cntOK = $rd->connect($Host, $Port);
        }
        if (!$cntOK) {
            \Logging\LogError("Redis connect error! " . $Host . ":" . $Port);
            return;
        }
        if ($Psw != "") {
            $psw = \Commfunc\GetDecodePsw($Psw);
            $authOK = $rd->auth($psw);
            if (!$authOK) {
                \Logging\LogError("Redis auth error! "  . $Host . ":" . $Port);
                return;
            }
        }
        $rd->select($Select);
        \Logging\LogInfo("Redis connect ok: " . $Host . ":" . $Port);
        self::$redis = $rd;
        return self::$redis;
    }
 
    public static function Close($redis)
    {
        //归还,暂不处理
    }
}
 
/**
 * 获取DB数据对应rediskey
 */
function GetDBRedisKey($dbName, $collectionName, $find)
{
    $redisKey = $dbName . ":" . $collectionName;
    foreach ($find as $value) {
        $redisKey .= ":" . $value;
    }
    return $redisKey;
}
 
/**
 * 获取GM工具功能数据对应rediskey,传入不定数量的参数,自定义
 */
function GetFCRedisKey()
{
    if (!func_num_args()) {
        return "";
    }
    $redisKey = "FC";
    foreach (func_get_args() as $value) {
        $redisKey .= ":" . $value;
    }
    return $redisKey;
}
 
function Exists($redisKey)
{
    $redis = RedisPool::Get();
    if (!$redis) {
        return;
    }
    $ret = $redis->exists($redisKey);
    RedisPool::Close($redis);
    \Logging\LogInfo("Redis Exists redisKey=" . $redisKey . " ret:" . $ret);
    return $ret;
}
 
function DelKey($redisKey)
{
    $redis = RedisPool::Get();
    if (!$redis) {
        return;
    }
    $ret = $redis->delete($redisKey);
    RedisPool::Close($redis);
    \Logging\LogInfo("Redis DelKey redisKey=" . $redisKey . " ret:" . $ret);
    return true;
}
 
function GetHashObj($redisKey, &$retArray)
{
    $redis = RedisPool::Get();
    if (!$redis) {
        return;
    }
    $retArray = $redis->hGetAll($redisKey);
    foreach ($retArray as $key => $value) {
        $jd = json_decode($value, true);
        if (is_array($jd)) {
            $retArray[$key] = $jd;
        }
    }
    RedisPool::Close($redis);
    if (!$retArray) {
        \Logging\LogInfo("Redis GetHashObj not exist! redisKey=" . $redisKey);
        return;
    }
    return true;
}
 
function SetHashObj($redisKey, $objArray, $expire = 0)
{
    $redis = RedisPool::Get();
    if (!$redis) {
        return;
    }
    foreach ($objArray as $key => $value) {
        if (is_array($value)) {
            $objArray[$key] = \CommFunc\MyJson_encode($value);
        }
    }
    $ret = $redis->hMSet($redisKey, $objArray);
    if ($expire > 0) {
        $redis->expireAt($redisKey, time() + $expire);
    }
    RedisPool::Close($redis);
    if (!$ret) {
        // 设置失败
        return;
    }
    return true;
}
 
function GetHashValues($redisKey, $keys, &$retArray)
{
    $redis = RedisPool::Get();
    if (!$redis) {
        return;
    }
    $retArray = $redis->hMget($redisKey, $keys);
    RedisPool::Close($redis);
    if (!$retArray) {
        \Logging\LogInfo("Redis GetHashValues not exist! redisKey=" . $redisKey);
        return;
    }
    return true;
}
 
function SetHashValues($redisKey, $setArray, $expire = 0)
{
    $redis = RedisPool::Get();
    if (!$redis) {
        return;
    }
    foreach ($setArray as $key => $value) {
        if (is_array($value)) {
            $setArray[$key] = \CommFunc\MyJson_encode($value);
        }
    }
    $ret = $redis->hMSet($redisKey, $setArray);
    if ($expire > 0) {
        $redis->expireAt($redisKey, time() + $expire);
    }
    RedisPool::Close($redis);
    if (!$ret) {
        // 设置失败
        return;
    }
    return true;
}
 
function ListPush($key, $LR, $value)
{
    $redis = RedisPool::Get();
    if (!$redis) {
        return 0;
    }
    if (is_array($value)) {
        $value = \CommFunc\MyJson_encode($value);
    }
    if ($LR == "R") {
        $ret = $redis->rPush($key, $value);
    } else {
        $ret = $redis->lPush($key, $value);
    }
    RedisPool::Close($redis);
    if (!$ret) {
        return 0;
    }
    return $ret;
}
 
function ListPop($key, &$ret, $LR = "R")
{
    $redis = RedisPool::Get();
    if (!$redis) {
        return;
    }
    if ($LR == "R") {
        $ret = $redis->rPop($key);
    } else {
        $ret = $redis->lPop($key);
    }
    $jd = json_decode($ret, true);
    if (is_array($jd)) {
        $ret = $jd;
    }
    RedisPool::Close($redis);
    if (!$ret) {
        return;
    }
    return true;
}
 
function ListRange($key, &$retArray, $start = 0, $stop = -1, $innerToArray = true)
{
    $redis = RedisPool::Get();
    if (!$redis) {
        return;
    }
    $retArray = $redis->lrange($key, $start, $stop);
    if ($innerToArray) {
        foreach ($retArray as $key => $value) {
            $jd = json_decode($value, true);
            if (is_array($jd)) {
                $retArray[$key] = $jd;
            }
        }
    }
    RedisPool::Close($redis);
    return true;
}
 
// function GetKeysByPattern($pattern, $count = 1000)
// {
//     $redis = RedisPool::Get();
//     if (!$redis) {
//         return;
//     }
 
//     $keysList = [];
//     $it = 0; // 第一次迭代使用 0 作为游标
//     while (true) {
//         $keys = $redis->scan($it, $pattern, $count);
//         if (isset($keys)) {
//             // 迭代中可能并发更新数据,所有需要合并覆盖
//             $keysList = array_merge($keysList, $keys);
//         }
//         if ($it === 0) { //返回了游标 0 , 这表示迭代已经结束
//             break;
//         }
//     }
//     // 以 0 作为游标开始一次新的迭代, 一直调用 SCAN 命令, 直到命令返回游标 0 , 我们称这个过程为一次完整遍历(full iteration)。
//     RedisPool::Close($redis);
//     // scan 的 key 可能重复,去重
//     $keysList = array_unique($keysList);
//     return $keysList;
// }