data_manager.lua 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. local M = {}
  2. local redisdbx = require "redisdbx"
  3. local tools = require "tools"
  4. local skynet = require "skynet"
  5. local mysqldbx = require "mysqldbx"
  6. local cjson = require "cjson"
  7. function M.set(msg_body)
  8. local isok ,key = tools.checkData({"key","value"},msg_body)
  9. if not isok then
  10. return false,string.format("缺少字段: %s.", key)
  11. end
  12. -- local current_time = os.date("%Y-%m-%d %H:%M:%S")
  13. redisdbx.exec("set",nil,"xs-"..msg_body.key,msg_body.value)
  14. return true, {}
  15. end
  16. function M.get(msg_body)
  17. local isok ,key = tools.checkData({"key"},msg_body)
  18. if not isok then
  19. return false,string.format("缺少字段: %s.", key)
  20. end
  21. local res = redisdbx.exec("get",nil,"xs-"..msg_body.key)
  22. return true, res
  23. end
  24. function M.db_set(msg_body)
  25. local isok ,key = tools.checkData({"key","value"},msg_body)
  26. if not isok then
  27. return false,string.format("缺少字段: %s.", key)
  28. end
  29. local sql = string.format("SELECT * FROM data_manager WHERE key = '%s' LIMIT 1", msg_body.key)
  30. local isok,res;
  31. res = mysqldbx.query(sql)
  32. if #res > 0 then
  33. sql = string.format("UPDATE data_manager SET db_value = '%s' WHERE db_key = '%s' ",msg_body.value,msg_body.key)
  34. else
  35. sql = string.format("INSERT INTO `data_manager` (db_key,db_value) VALUES ('%s','%s')",msg_body.key,cjson.encode(msg_body.value))
  36. end
  37. skynet.error("sql:",sql)
  38. mysqldbx.query(sql)
  39. return true, {}
  40. end
  41. function M.db_get(msg_body)
  42. local isok ,key = tools.checkData({"key"},msg_body)
  43. if not isok then
  44. return false,string.format("缺少字段: %s.", key)
  45. end
  46. local sql = string.format("SELECT * FROM data_manager WHERE db_key = '%s' LIMIT 1", msg_body.key)
  47. local isok,res;
  48. res = mysqldbx.query(sql)
  49. if #res > 0 then
  50. res = res[1]
  51. end
  52. return true, res
  53. end
  54. return M