--[[ Descripttion: version: Author: Neo,Huang Date: 2022-01-06 16:50:11 LastEditors: Neo,Huang LastEditTime: 2022-01-25 16:30:38 --]] local lib_battle_redis = require("lib_battle_redis") -- redis基础操作 -- 本文件所有处理不会对module对应的数据库进行操作,玩家个人数据请使用 data.module local root = {} ---------------------------------------- -- main key ---------------------------------------- function root.get_int(key) local val = lib_battle_redis:get(key) return val and tonumber(val) or 0 end function root.get_json(key) local val = lib_battle_redis:get(key) val = val and cjson_decode(val) or {} -- 主要针对C++中的NULL if is_empty(val) then val = {} end return val end ---------------------------------------- -- subKey ---------------------------------------- function root.hset(tb, key, val) if is_empty(tb) or is_empty(key) then return end if is_nil(val) then return root.hdel(tb, key) end local info = val if type(val) == "table" then info = cjson_encode(val) end return lib_battle_redis:hset(tb, key, info) end function root.hget_int(tb, key) local val = lib_battle_redis:hget(tb, key) return val and tonumber(val) or 0 end function root.hget_json(tb, key) local val = lib_battle_redis:hget(tb, key) val = val and cjson_decode(val) or {} -- 主要针对C++中的NULL if is_empty(val) then val = {} end return val end function root.hmset(key, mapData) if is_empty(key) or is_empty(mapData) then return false end local res = {} for k, v in pairs(mapData) do table.insert(res, k) table.insert(res, v) end return lib_battle_redis:hmset(key, table.unpack(res)) end -- 获取模块所有数据 function root.hgetall(key) local data = lib_battle_redis:hgetall(key) if not is_empty(data) then local ret = {} for i = 1, #data, 2 do ret[data[i]] = data[i + 1] end return ret end end function root.do_eval(evalkey, keyCount, ...) local data = lib_battle_redis:eval(root.evalStr[evalkey], keyCount, ...) return data end -- redis 执行 lua 脚本. root.evalStr = { ["aaa"] = [[redis.call('lpush', KEYS[1], ARGV[1]); redis.call('ltrim', KEYS[1], 0, ARGV[2]);]], ["bbb"] = [["redis.call('zincrby',KEYS[1], ARGV[1], ARGV[2]); local scores = redis.call('zscore', KEYS[1], ARGV[2]); local rank = redis.call('zrevrank', KEYS[1], ARGV[2]); return {rank, scores};]] } return root