123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- --[[
- Descripttion:基础模块类
- version:
- Author: Neo,Huang
- Date: 2022-07-04 19:07:54
- LastEditors: Neo,Huang
- LastEditTime: 2022-07-04 19:13:19
- --]]
- local lib_game_redis = require("lib_game_redis")
- local lib_game_mysql = require("lib_game_mysql")
- local redisUtil = require("utils.redisUtil")
- local sqlUtil = require("utils.sqlUtil")
- local root = class("base")
- function root:ctor(uid, mdlName, keyName, isSaveDB, isPersonal, secondaryKeys)
- self.uid = uid
- self.mdlName = mdlName
- self.keyName = keyName
- self.isSaveDB = isSaveDB
- self.isPersonal = false
- if isPersonal == nil then
- self.isPersonal = true
- end
- self.secondaryKeys = secondaryKeys
- end
- -- 模块sql属性
- function root:mysql_get_init_columns()
- return {}
- end
- -- 初始化模块表
- function root:mysql_get_table_info()
- return {
- mdlName = self.mdlName,
- keyName = self.keyName,
- isSaveDB = self.isSaveDB,
- secondaryKeys = self.secondaryKeys,
- columnNameOptions = self:mysql_get_init_columns()
- }
- end
- function root:do_login()
- end
- function root:do_logout()
- end
- function root:getKeyValue()
- return self[self.keyName]
- end
- -- 获取模块key
- function root:get_module_redis_key()
- local moduleKey = string.format("mdl:%s:%s", self.mdlName, tostring(self:getKeyValue()))
- return moduleKey
- end
- -- 模块数据库表名
- function root:get_table_name()
- return string.format("mdl_%s", tostring(self.mdlName))
- end
- -- 获取玩家模块数据
- function root:get_data_from_db()
- local moduleKey = self:get_module_redis_key()
- local res = redisUtil.hgetall(moduleKey)
- if not is_empty(res) then
- return res
- end
- if self.isSaveDB then
- local sql = sqlUtil.select_table_by_key(self:get_table_name(), self.keyName, self:getKeyValue())
- res = lib_game_mysql:query(sql)
- if res and res[1] then
- -- add to redis
- redisUtil.hmset(moduleKey, res[1])
- -- get again
- res = redisUtil.hgetall(moduleKey)
- if not is_empty(res) then
- return res
- end
- end
- return res
- end
- end
- -- 字段类型 - json
- function root:is_json(column)
- return string.match(string.upper(column), "JSON")
- end
- -- 字段类型 - int
- function root:is_number(column)
- return string.match(string.upper(column), "INT")
- end
- -- 字段值
- function root:get_column_db_value(column)
- if is_empty(column) then
- return
- end
- local key = self:get_module_redis_key()
- local val = lib_game_redis:hget(key, column)
- if self:is_json(column) then
- val = string.format("'%s'", cjson_encode(val))
- elseif self:is_number(column) then
- val = tonumber(val or 0)
- else
- val = string.format("'%s'", val)
- end
- return val
- end
- -- 备份模块数据
- function root:backup_to_db()
- local options = self:mysql_get_init_columns()
- if is_empty(options) then
- return false
- end
- local colums = {}
- local values = {}
- for k, v in pairs(options) do
- table.insert(colums, k)
- table.insert(values, self:get_column_db_value(k))
- end
- local tbName = self:get_table_name()
- local sql =
- string.format("replace into %s (%s) value (%s);", tbName, table.concat(colums, ","), table.concat(values, ","))
- local ret = lib_game_mysql:query(sql)
- return ret
- end
- -- 删除模块热数据
- function root:del_hot_data()
- local key = self:get_module_redis_key()
- local ret = lib_game_redis:del(key)
- log.info("del_hot_data key[%s] ret[%s]", tostring(key), tostring(ret))
- end
- ----------------------------------------
- -- 实时数据
- ----------------------------------------
- local moduleData = require("data.module")
- -- 获取数据类型
- function root:get_sub_key_type(subKey)
- if subKey == nil then
- return
- end
- local mpOptions = self:mysql_get_init_columns()
- for k, v in pairs(mpOptions) do
- if tostring(k) == tostring(subKey) then
- if self:is_number(v) then
- return "int"
- end
- if self:is_json(v) then
- return "json"
- end
- return "string"
- end
- end
- end
- -- 获取数据
- function root:redis_get_key_info(subKey, uid)
- if subKey == nil then
- return
- end
- uid = uid or self.uid
- local info = moduleData:hget(uid, self.mdlName, subKey)
- local tyKey = self:get_sub_key_type(subKey)
- if tyKey then
- if tyKey == "json" then
- if info then
- info = cjson_decode(info)
- end
- -- 主要针对C++中的NULL
- if is_empty(info) then
- info = {}
- end
- end
- if tyKey == "int" then
- info = tonumber(info or 0)
- end
- end
- return info
- end
- -- 设置数据
- function root:redis_update_key_info(subKey, info, uid)
- uid = uid or self.uid
- return moduleData:hset(uid, self.mdlName, subKey, info)
- end
- -- +1
- function root:redis_hincrby(subKey, uid)
- uid = uid or self.uid
- return moduleData:hincrby(uid, self.mdlName, subKey)
- end
- return root
|