123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- --[[
- Descripttion:
- version:
- Author: Neo,Huang
- Date: 2022-01-06 16:50:11
- LastEditors: Neo,Huang
- LastEditTime: 2022-01-25 16:30:38
- --]]
- local lib_game_redis = require("lib_game_redis")
- -- redis基础操作
- -- 本文件所有处理不会对module对应的数据库进行操作,玩家个人数据请使用 data.module
- local root = {}
- ----------------------------------------
- -- main key
- ----------------------------------------
- function root.get_int(key)
- local val = lib_game_redis:get(key)
- return val and tonumber(val) or 0
- end
- function root.get_json(key)
- local val = lib_game_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_game_redis:hset(tb, key, info)
- end
- function root.hget_int(tb, key)
- local val = lib_game_redis:hget(tb, key)
- return val and tonumber(val) or 0
- end
- function root.hget_json(tb, key)
- local val = lib_game_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_game_redis:hmset(key, table.unpack(res))
- end
- -- 获取模块所有数据
- function root.hgetall(key)
- local data = lib_game_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.hincrby(key, subKey, steps)
- if is_nil(key) or is_nil(subKey) then
- return
- end
- steps = steps or 1
- return lib_game_redis:hincrby(key, subKey, steps)
- end
- function root.do_eval(evalkey, keyCount, ...)
- local data = lib_game_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
|