dispatcher.rs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. incrby {
  52. cmd::string::incr_by,
  53. ["write" "denyoom" "fast"],
  54. 3,
  55. },
  56. set {
  57. cmd::string::set,
  58. ["random" "loading" "stale"],
  59. -3,
  60. },
  61. getset {
  62. cmd::string::getset,
  63. ["random" "loading" "stale"],
  64. -3,
  65. },
  66. ping {
  67. cmd::client::ping,
  68. ["random" "loading" "stale"],
  69. -1,
  70. },
  71. time {
  72. do_time,
  73. ["random" "loading" "stale"],
  74. 1,
  75. },
  76. }