HomeServer/Server/Common/RedisCenter.lua
2024-11-20 15:41:37 +08:00

117 lines
3.9 KiB
Lua
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

local skynet = require "skynet"
local oo = require "Class"
local log = require "Log"
local pb = require "pb"
local errorInfo = require "ErrorInfo"
local dataType = require "DataType"
local redisKeyUrl = require "RedisKeyUrl"
local RedisCenter = oo.class()
--根据尾号获取账号redis连接索引
function RedisCenter:GetAccountRedisIndex( account )
local lenAccount = #account
local lastNum = tonumber( string.sub( account, lenAccount , lenAccount) ) --正式服一般是6028731887380604672号返回账号最后一位非数字结尾的号为nil
local redisIndex = 0 --玩家的redis索引 迁移redis相关
if not lastNum then
redisIndex = 0
elseif lastNum >= 0 and lastNum <= 4 then
redisIndex = 1
elseif lastNum >= 5 and lastNum <= 9 then
redisIndex = 2
end
return redisIndex
end
--根据尾字母大小写获取账号账号redis连接
function RedisCenter:GetAccountRedis( account )
if not skynet.server.gameConfig:IsMultiRedis() then --如果未开起多redis直接返回默认的redis
return skynet.server.redis
end
local redisIndex = self:GetAccountRedisIndex( account )
if 0 == redisIndex then
return skynet.server.redis
elseif 1 == redisIndex then
return skynet.server.redisUser1
elseif 2 == redisIndex then
return skynet.server.redisUser2
end
return skynet.server.redis
end
--设置Redis中的用户信息
function RedisCenter:SetRedisDBInfo( account , ... )
local redisUserListKey = string.format( redisKeyUrl.AccountServerUserList , account )
local curRedis = self:GetAccountRedis( account )
curRedis:hmset( redisUserListKey , ... )
end
--获取Redis中的用户信息
function RedisCenter:GetRedisDBInfo( account )
local redisUserListKey = string.format( redisKeyUrl.AccountServerUserList , account )
local curRedis = self:GetAccountRedis( account )
local dbInfo = curRedis:hgetall( redisUserListKey )
dbInfo = redisKeyUrl:CovertTable( dbInfo )
if not dbInfo or not dbInfo.DBIndex or not dbInfo.UserID then
dbInfo = nil
log.error("Common:GetRedisDBInfo 数据库信息为空" , account )
end
return dbInfo
end
--获取好友redis连接索引
function RedisCenter:GetPartnerIdRedisIndex( partnerId )
--迁移redis相关
local lenPartnerId = #partnerId
local lastChar = string.sub( partnerId, lenPartnerId , lenPartnerId)
local redisIndex = 0 --玩家的redis索引,根据partnerId最后一个字母来判断如果是小写就走索引1大写走索引2其他情况走索引0
if not lastChar or "" == lastChar then
redisIndex = 0
elseif lastChar >= "a" and lastChar <= "z" then
redisIndex = 1
elseif lastChar >= "A" and lastChar <= "Z" then
redisIndex = 2
end
return redisIndex
end
--获取账号redis连接
function RedisCenter:GetPartnerRedis( partnerId )
if not skynet.server.gameConfig:IsMultiRedis() then --如果未开起多redis直接返回默认的redis
return skynet.server.redis
end
local redisIndex = self:GetPartnerIdRedisIndex( partnerId )
if 0 == redisIndex then
return skynet.server.redis
elseif 1 == redisIndex then
return skynet.server.redisUser1
elseif 2 == redisIndex then
return skynet.server.redisUser2
end
return skynet.server.redis
end
--获取账号旧的redis连接
function RedisCenter:GetPartnerOldRedis( partnerId )
return skynet.server.redis
end
--获取公共Redis连接
function RedisCenter:GetCommonRedis( partnerId )
if nil == partnerId then
return skynet.server.redis
end
--暂时只有一个直接返回就是。后期会有多个根据partnerId来获取redis索引
return skynet.server.redis
end
skynet.server.redisCenter = RedisCenter
return RedisCenter