dispatcher.rs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. use crate::{cmd, db::Db, dispatcher, error::Error, value::Value};
  2. use bytes::Bytes;
  3. use std::convert::TryInto;
  4. use std::time::SystemTime;
  5. use std::time::UNIX_EPOCH;
  6. fn do_time(_db: &Db, _args: &[Bytes]) -> Result<Value, Error> {
  7. let now = SystemTime::now();
  8. let since_the_epoch = now.duration_since(UNIX_EPOCH).expect("Time went backwards");
  9. let seconds = format!("{}", since_the_epoch.as_secs());
  10. let millis = format!("{}", since_the_epoch.subsec_millis());
  11. Ok(vec![seconds.as_str(), millis.as_str()].into())
  12. }
  13. fn do_command(_db: &Db, _args: &[Bytes]) -> Result<Value, Error> {
  14. let now = SystemTime::now();
  15. let since_the_epoch = now.duration_since(UNIX_EPOCH).expect("Time went backwards");
  16. let in_ms: i128 =
  17. since_the_epoch.as_secs() as i128 * 1000 + since_the_epoch.subsec_millis() as i128;
  18. Ok(format!("{}", in_ms).as_str().into())
  19. }
  20. dispatcher! {
  21. command {
  22. do_command,
  23. ["random" "loading" "stale"],
  24. 1,
  25. },
  26. incr {
  27. cmd::string::incr,
  28. ["write" "denyoom" "fast"],
  29. 2,
  30. },
  31. decr {
  32. cmd::string::decr,
  33. ["write" "denyoom" "fast"],
  34. 2,
  35. },
  36. get {
  37. cmd::string::get,
  38. ["random" "loading" "stale"],
  39. 2,
  40. },
  41. set {
  42. cmd::string::set,
  43. ["random" "loading" "stale"],
  44. -3,
  45. },
  46. getset {
  47. cmd::string::getset,
  48. ["random" "loading" "stale"],
  49. -3,
  50. },
  51. time {
  52. do_time,
  53. ["random" "loading" "stale"],
  54. 1,
  55. },
  56. }