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//!# use core::str::FromStr;
9//!# use core::net::SocketAddr;
10//!# use std_embedded_nal::Stack;
11//!# use std_embedded_time::StandardClock;
12//!# use embedded_redis::commands::bgsave::BackgroundSaveCommand;
13//!# use embedded_redis::network::ConnectionHandler;
14//!#
15//! let mut stack = Stack::default();
16//! let clock = StandardClock::default();
17//!
18//! let mut connection_handler = ConnectionHandler::resp2(SocketAddr::from_str("127.0.0.1:6379").unwrap());
19//! let client = connection_handler.connect(&mut stack, Some(&clock)).unwrap();
20//!
21//! let command = BackgroundSaveCommand::default();
22//! let response = client.send(command).unwrap().wait().unwrap();
23//! ```
24//! # Schedule option
25//! Using `SCHEDULE` option by setting constructor flag to `true`.
26//! ```
27//!# use core::str::FromStr;
28//!# use core::net::SocketAddr;
29//!# use std_embedded_nal::Stack;
30//!# use std_embedded_time::StandardClock;
31//!# use embedded_redis::commands::bgsave::BackgroundSaveCommand;
32//!# use embedded_redis::network::ConnectionHandler;
33//!#
34//!# let mut stack = Stack::default();
35//!# let clock = StandardClock::default();
36//!#
37//!# let mut connection_handler = ConnectionHandler::resp2(SocketAddr::from_str("127.0.0.1:6379").unwrap());
38//!# let client = connection_handler.connect(&mut stack, Some(&clock)).unwrap();
39//!#
40//! let command = BackgroundSaveCommand::new(true);
41//! ```
42//! # Shorthand
43//! [Client](Client#method.get) provides a shorthand method for this command.
44//! ```no_run
45//!# use core::str::FromStr;
46//!# use core::net::SocketAddr;
47//!# use std_embedded_nal::Stack;
48//!# use std_embedded_time::StandardClock;
49//!# use embedded_redis::network::ConnectionHandler;
50//!#
51//!# let mut stack = Stack::default();
52//!# let clock = StandardClock::default();
53//!#
54//!# let mut connection_handler = ConnectionHandler::resp2(SocketAddr::from_str("127.0.0.1:6379").unwrap());
55//!# let client = connection_handler.connect(&mut stack, Some(&clock)).unwrap();
56//!#
57//! let response = client.bgsave(false).unwrap().wait().unwrap();
58//! ```
59use crate::commands::auth::AuthCommand;
60use crate::commands::builder::CommandBuilder;
61use crate::commands::hello::HelloCommand;
62use crate::commands::{Command, ResponseTypeError};
63use crate::network::protocol::Protocol;
64use crate::network::{Client, CommandErrors, Future};
65use bytes::Bytes;
66use embedded_nal::TcpClientStack;
67use embedded_time::Clock;
68
69static SCHEDULE_OPTION: Bytes = Bytes::from_static(b"SCHEDULE");
70
71/// Abstraction ob BGSAVE command
72#[derive(Default)]
73pub struct BackgroundSaveCommand {
74    /// If BGSAVE SCHEDULE is used, the command will immediately return OK when an AOF rewrite is
75    /// in progress and schedule the background save to run at the next opportunity.
76    schedule: bool,
77}
78
79impl BackgroundSaveCommand {
80    pub fn new(schedule: bool) -> Self {
81        Self { schedule }
82    }
83}
84
85impl<F: From<CommandBuilder>> Command<F> for BackgroundSaveCommand {
86    type Response = ();
87
88    fn encode(&self) -> F {
89        let builder = CommandBuilder::new("BGSAVE");
90
91        if self.schedule {
92            return builder.arg(&SCHEDULE_OPTION).into();
93        }
94
95        builder.into()
96    }
97
98    fn eval_response(&self, _: F) -> Result<Self::Response, ResponseTypeError> {
99        Ok(())
100    }
101}
102
103impl<'a, N: TcpClientStack, C: Clock, P: Protocol> Client<'a, N, C, P>
104where
105    AuthCommand: Command<<P as Protocol>::FrameType>,
106    HelloCommand: Command<<P as Protocol>::FrameType>,
107{
108    /// Shorthand for [BackgroundSaveCommand]
109    pub fn bgsave(
110        &'a self,
111        schedule: bool,
112    ) -> Result<Future<'a, N, C, P, BackgroundSaveCommand>, CommandErrors>
113    where
114        <P as Protocol>::FrameType: From<CommandBuilder>,
115    {
116        self.send(BackgroundSaveCommand::new(schedule))
117    }
118}