data_manager.lua 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 db_key = '%s' LIMIT 1", msg_body.key)
  30. local isok,res;
  31. res = mysqldbx.query(sql)
  32. skynet.error("sql:",sql)
  33. if #res > 0 then
  34. sql = string.format("UPDATE data_manager SET db_value = '%s' WHERE db_key = '%s' ",cjson.encode(msg_body.value),msg_body.key)
  35. else
  36. sql = string.format("INSERT INTO `data_manager` (db_key,db_value) VALUES ('%s','%s')",msg_body.key,cjson.encode(msg_body.value))
  37. end
  38. skynet.error("sql:",sql)
  39. mysqldbx.query(sql)
  40. return true, {}
  41. end
  42. function M.db_get(msg_body)
  43. local isok ,key = tools.checkData({"key"},msg_body)
  44. if not isok then
  45. return false,string.format("缺少字段: %s.", key)
  46. end
  47. local sql = string.format("SELECT * FROM data_manager WHERE db_key = '%s' LIMIT 1", msg_body.key)
  48. local isok,res;
  49. res = mysqldbx.query(sql)
  50. if #res > 0 then
  51. res = res[1]
  52. end
  53. return true, res
  54. end
  55. return M