DataCache.lua 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. -- 数据缓存文件
  2. local DB = require("common.DB")
  3. local LuaMongo = _G.lua_mongo
  4. local CommonDB = require("common.CommonDB")
  5. local Util = require("common.Util")
  6. -- 开服第10天以后才缓存玩家uid
  7. local CACHE_UUID_STARTDAY = 10
  8. local player_uuid_list = {}
  9. local function isCacheUuid()
  10. local openDay = CommonDB.getServerOpenDay()
  11. if CACHE_UUID_STARTDAY > openDay or _G.is_middle == true then
  12. return false
  13. end
  14. return true
  15. end
  16. -- 加载所有玩家的uid, 后续优化下
  17. local function load_playeruuid()
  18. _G.collectgarbage("step", 1000000)
  19. LuaMongo.find(DB.db_char, nil, {lv = 1})
  20. while true do
  21. local data = {}
  22. if not LuaMongo.next(data) then
  23. break
  24. end
  25. player_uuid_list[data._id] = data.lv
  26. end
  27. _G.collectgarbage("step", 1000000)
  28. end
  29. function Update_PlayerUuidList(playerList)
  30. if not isCacheUuid() then
  31. return
  32. end
  33. player_uuid_list = playerList
  34. end
  35. function Insert_PlayerUuidList(newUuid, lv)
  36. if not isCacheUuid() then
  37. return
  38. end
  39. player_uuid_list[newUuid] = lv
  40. end
  41. function Update_PlayerLv(playerUid, newLV)
  42. if not newLV then
  43. return
  44. end
  45. player_uuid_list[playerUid] = newLV
  46. end
  47. function Get_PlayerUuidList()
  48. if not isCacheUuid() then
  49. return
  50. end
  51. if not next(player_uuid_list) then
  52. load_playeruuid()
  53. end
  54. return player_uuid_list
  55. end