dispatcher.rs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. use crate::{cmd, connection::Connection, 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(_conn: &Connection, _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(_conn: &Connection, _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. client {
  27. cmd::client::client,
  28. ["random" "loading" "stale"],
  29. -2,
  30. },
  31. decr {
  32. cmd::string::decr,
  33. ["write" "denyoom" "fast"],
  34. 2,
  35. },
  36. echo {
  37. cmd::client::echo,
  38. ["random" "loading" "stale"],
  39. 2,
  40. },
  41. get {
  42. cmd::string::get,
  43. ["random" "loading" "stale"],
  44. 2,
  45. },
  46. incr {
  47. cmd::string::incr,
  48. ["write" "denyoom" "fast"],
  49. 2,
  50. },
  51. set {
  52. cmd::string::set,
  53. ["random" "loading" "stale"],
  54. -3,
  55. },
  56. getset {
  57. cmd::string::getset,
  58. ["random" "loading" "stale"],
  59. -3,
  60. },
  61. ping {
  62. cmd::client::ping,
  63. ["random" "loading" "stale"],
  64. -1,
  65. },
  66. time {
  67. do_time,
  68. ["random" "loading" "stale"],
  69. 1,
  70. },
  71. }