117 lines
3.9 KiB
Lua
117 lines
3.9 KiB
Lua
|
|
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
|