baseModule.lua 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. --[[
  2. Descripttion:基础模块类
  3. version:
  4. Author: Neo,Huang
  5. Date: 2022-07-04 19:07:54
  6. LastEditors: Neo,Huang
  7. LastEditTime: 2022-07-04 19:13:19
  8. --]]
  9. local lib_game_redis = require("lib_game_redis")
  10. local lib_game_mysql = require("lib_game_mysql")
  11. local redisUtil = require("utils.redisUtil")
  12. local root = class("base")
  13. function root:ctor(uid, mdlName, keyName, isSaveDB, isPersonal)
  14. self.uid = uid
  15. self.mdlName = mdlName
  16. self.keyName = keyName
  17. self.isSaveDB = isSaveDB
  18. self.isPersonal = isPersonal or true
  19. end
  20. -- 模块sql属性
  21. function root:mysql_get_init_columns()
  22. return {}
  23. end
  24. -- 初始化模块表
  25. function root:mysql_get_table_info()
  26. return {
  27. mdlName = self.mdlName,
  28. keyName = self.keyName,
  29. isSaveDB = self.isSaveDB,
  30. columnNameOptions = self:mysql_get_init_columns()
  31. }
  32. end
  33. function root:getKeyValue()
  34. return self[self.keyName]
  35. end
  36. -- 获取模块key
  37. function root:get_module_redis_key()
  38. local moduleKey = string.format("mdl:%s:%s", self.mdlName, tostring(self:getKeyValue()))
  39. return moduleKey
  40. end
  41. -- 获取玩家模块数据
  42. function root:get_data_from_db()
  43. local moduleKey = self:get_module_redis_key()
  44. local res = redisUtil.hgetall(moduleKey)
  45. if not is_empty(res) then
  46. return res
  47. end
  48. if self.isSaveDB then
  49. local sql = sqlUtil.select_table_by_key(self.mdlName, self.keyName, self:getKeyValue())
  50. res = lib_game_mysql:query(sql)
  51. if res and res[1] then
  52. -- add to redis
  53. redisUtil.hmset(moduleKey, res[1])
  54. -- get again
  55. res = redisUtil.hgetall(moduleKey)
  56. if not is_empty(res) then
  57. return res
  58. end
  59. end
  60. return res
  61. end
  62. end
  63. -- 字段类型 - json
  64. function root:is_json(column)
  65. return string.match(string.upper(column), "JSON")
  66. end
  67. -- 字段类型 - int
  68. function root:is_number(column)
  69. return string.match(string.upper(column), "INT")
  70. end
  71. -- 字段值
  72. function root:get_column_db_value(column)
  73. if is_empty(column) then
  74. return
  75. end
  76. local key = string.format("mdl:%s:%s", tostring(self.mdlName), tostring(self.uid))
  77. local val = lib_game_redis:hget(key, column)
  78. if self:is_json(column) then
  79. val = string.format("'%s'", cjson_encode(val))
  80. elseif self:is_number(column) then
  81. val = tonumber(val or 0)
  82. else
  83. val = string.format("'%s'", val)
  84. end
  85. return val
  86. end
  87. -- 备份模块数据
  88. function root:backup_to_db()
  89. local options = self:mysql_get_init_columns()
  90. if is_empty(options) then
  91. return false
  92. end
  93. local colums = {}
  94. local values = {}
  95. for k, v in ipairs(options) do
  96. table.insert(colums, k)
  97. table.include(values, self:get_column_db_value(k))
  98. end
  99. local tbName = string.format("mdl_%s", tostring(self.mdlName))
  100. local sql =
  101. string.format("replace into %s (%s) value (%s);", tbName, table.concat(colums, ","), table.concat(values, ","))
  102. lib_game_mysql:query(sql)
  103. end
  104. -- 删除模块热数据
  105. function root:del_hot_data()
  106. local key = self:get_module_redis_key()
  107. local ret = lib_game_redis:del(key)
  108. log.info("del_hot_data key[%s] ret[%s]", tostring(key), tostring(ret))
  109. end
  110. ----------------------------------------
  111. -- 实时数据
  112. ----------------------------------------
  113. local moduleData = require("data.module")
  114. -- 获取数据类型
  115. function root:get_sub_key_type(subKey)
  116. if subKey == nil then
  117. return
  118. end
  119. local mpOptions = self:mysql_get_init_columns()
  120. for k, v in pairs(mpOptions) do
  121. if tostring(k) == tostring(subKey) then
  122. if self:is_number(v) then
  123. return "int"
  124. end
  125. if self:is_json(v) then
  126. return "json"
  127. end
  128. return "string"
  129. end
  130. end
  131. end
  132. -- 获取数据
  133. function root:redis_get_key_info(subKey, uid)
  134. if subKey == nil then
  135. return
  136. end
  137. uid = uid or self.uid
  138. local info = moduleData:hget(uid, self.mdlName, subKey)
  139. local tyKey = self:get_sub_key_type(subKey)
  140. if tyKey then
  141. if tyKey == "json" then
  142. if info then
  143. info = cjson_decode(info)
  144. end
  145. -- 主要针对C++中的NULL
  146. if is_empty(info) then
  147. info = {}
  148. end
  149. end
  150. if tyKey == "int" then
  151. info = tonumber(info or 0)
  152. end
  153. end
  154. return info
  155. end
  156. -- 设置数据
  157. function root:redis_update_key_info(subKey, info, uid)
  158. uid = uid or self.uid
  159. return moduleData:hset(uid, self.mdlName, subKey, info)
  160. end
  161. -- +1
  162. function root:redis_hincrby(subKey, uid)
  163. uid = uid or self.uid
  164. return moduleData:hincrby(uid, self.mdlName, subKey)
  165. end
  166. return root