embedded_redis/commands/bgsave.rs
1//! Abstraction of BGSAVE command.
2//!
3//! For general information about this command, see the [Redis documentation](<https://redis.io/commands/bgsave/>).
4//!
5//! # Basic usage
6//! By default no `SCHEDULE` option is used.
7//! ```
8//! # async_std::task::block_on(async {
9//!# use core::str::FromStr;
10//!# use core::net::SocketAddr;
11//!# use std_embedded_nal_async::Stack;
12//!# use std_embedded_time::StandardClock;
13//!# use embedded_redis::commands::bgsave::BackgroundSaveCommand;
14//!# use embedded_redis::network::{CommandErrors, ConnectionHandler};
15//!#
16//! let stack = Stack::default();
17//! let clock = StandardClock::default();
18//!
19//! let mut connection_handler = ConnectionHandler::resp2(SocketAddr::from_str("127.0.0.1:6379").unwrap());
20//! let client = connection_handler.connect(&stack, Some(&clock)).await.unwrap();
21//!
22//! let command = BackgroundSaveCommand::default();
23//! let response = client.send(command).await;
24//!# if let Err(error) = response {
25//!# assert!(matches!(error, CommandErrors::ErrorResponse(message)
26//!# if message.contains("Background save already in progress")));
27//!# }
28//! # });
29//! ```
30//! # Schedule option
31//! Using `SCHEDULE` option by setting constructor flag to `true`.
32//! ```
33//! # async_std::task::block_on(async {
34//!# use core::str::FromStr;
35//!# use core::net::SocketAddr;
36//!# use std_embedded_nal_async::Stack;
37//!# use std_embedded_time::StandardClock;
38//!# use embedded_redis::commands::bgsave::BackgroundSaveCommand;
39//!# use embedded_redis::network::ConnectionHandler;
40//!#
41//!# let stack = Stack::default();
42//!# let clock = StandardClock::default();
43//!#
44//!# let mut connection_handler = ConnectionHandler::resp2(SocketAddr::from_str("127.0.0.1:6379").unwrap());
45//!# let client = connection_handler.connect(&stack, Some(&clock)).await.unwrap();
46//!#
47//! let command = BackgroundSaveCommand::new(true);
48//! # });
49//! ```
50//! # Shorthand
51//! [Client](Client#method.get) provides a shorthand method for this command.
52//! ```
53//! # async_std::task::block_on(async {
54//!# use core::str::FromStr;
55//!# use core::net::SocketAddr;
56//!# use std_embedded_nal_async::Stack;
57//!# use std_embedded_time::StandardClock;
58//!# use embedded_redis::network::{CommandErrors, ConnectionHandler};
59//!#
60//!# let stack = Stack::default();
61//!# let clock = StandardClock::default();
62//!#
63//!# let mut connection_handler = ConnectionHandler::resp2(SocketAddr::from_str("127.0.0.1:6379").unwrap());
64//!# let client = connection_handler.connect(&stack, Some(&clock)).await.unwrap();
65//!#
66//! let response = client.bgsave(false).await;
67//!# if let Err(error) = response {
68//!# assert!(matches!(error, CommandErrors::ErrorResponse(message)
69//!# if message.contains("Background save already in progress")));
70//!# }
71//! # });
72//! ```
73use crate::commands::auth::AuthCommand;
74use crate::commands::builder::CommandBuilder;
75use crate::commands::hello::HelloCommand;
76use crate::commands::{Command, ResponseTypeError};
77use crate::network::protocol::Protocol;
78use crate::network::{Client, CommandErrors};
79use bytes::Bytes;
80use embedded_io_async::{Read, Write};
81use embedded_time::Clock;
82
83static SCHEDULE_OPTION: Bytes = Bytes::from_static(b"SCHEDULE");
84
85/// Abstraction ob BGSAVE command
86#[derive(Default)]
87pub struct BackgroundSaveCommand {
88 /// If BGSAVE SCHEDULE is used, the command will immediately return OK when an AOF rewrite is
89 /// in progress and schedule the background save to run at the next opportunity.
90 schedule: bool,
91}
92
93impl BackgroundSaveCommand {
94 pub fn new(schedule: bool) -> Self {
95 Self { schedule }
96 }
97}
98
99impl<F: From<CommandBuilder>> Command<F> for BackgroundSaveCommand {
100 type Response = ();
101
102 fn encode(&self) -> F {
103 let builder = CommandBuilder::new("BGSAVE");
104
105 if self.schedule {
106 return builder.arg(&SCHEDULE_OPTION).into();
107 }
108
109 builder.into()
110 }
111
112 fn eval_response(&self, _: F) -> Result<Self::Response, ResponseTypeError> {
113 Ok(())
114 }
115}
116
117impl<'a, T: Read + Write, C: Clock, P: Protocol> Client<'a, T, C, P>
118where
119 AuthCommand: Command<<P as Protocol>::FrameType>,
120 HelloCommand: Command<<P as Protocol>::FrameType>,
121{
122 /// Shorthand for [BackgroundSaveCommand]
123 pub async fn bgsave(&self, schedule: bool) -> Result<(), CommandErrors>
124 where
125 <P as Protocol>::FrameType: From<CommandBuilder>,
126 {
127 self.send(BackgroundSaveCommand::new(schedule)).await
128 }
129}