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; // }