simpledb.lua 838 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. local skynet = require "skynet"
  2. require "skynet.manager" -- import skynet.register
  3. local db = {}
  4. local command = {}
  5. function command.GET(key)
  6. return db[key]
  7. end
  8. function command.SET(key, value)
  9. local last = db[key]
  10. db[key] = value
  11. return last
  12. end
  13. skynet.start(function()
  14. skynet.dispatch("lua", function(session, address, cmd, ...)
  15. cmd = cmd:upper()
  16. if cmd == "PING" then
  17. assert(session == 0)
  18. local str = (...)
  19. if #str > 20 then
  20. str = str:sub(1,20) .. "...(" .. #str .. ")"
  21. end
  22. skynet.error(string.format("%s ping %s", skynet.address(address), str))
  23. return
  24. end
  25. local f = command[cmd]
  26. if f then
  27. skynet.ret(skynet.pack(f(...)))
  28. else
  29. error(string.format("Unknown command %s", tostring(cmd)))
  30. end
  31. end)
  32. -- skynet.traceproto("lua", false) -- true off tracelog
  33. skynet.register "SIMPLEDB"
  34. end)