key.rs 967 B

1234567891011121314151617181920212223242526272829303132333435
  1. use crate::{connection::Connection, error::Error, value::bytes_to_number, value::Value};
  2. use bytes::Bytes;
  3. use tokio::time::{Duration, Instant};
  4. pub fn del(conn: &Connection, args: &[Bytes]) -> Result<Value, Error> {
  5. Ok(conn.db().del(&args[1..]))
  6. }
  7. pub fn expire(conn: &Connection, args: &[Bytes]) -> Result<Value, Error> {
  8. let expires_at: i64 = bytes_to_number(&args[2])?;
  9. if expires_at <= 0 {
  10. return Ok(conn.db().del(&args[1..2]));
  11. }
  12. let expires_at = Duration::new(expires_at as u64, 0);
  13. Ok(conn
  14. .db()
  15. .expire(&args[1], expires_at))
  16. }
  17. pub fn persist(conn: &Connection, args: &[Bytes]) -> Result<Value, Error> {
  18. Ok(conn.db().persist(&args[1]))
  19. }
  20. pub fn ttl(conn: &Connection, args: &[Bytes]) -> Result<Value, Error> {
  21. let ttl = match conn.db().ttl(&args[1]) {
  22. Some(Some(ttl)) => (ttl - Instant::now()).as_secs() as i64,
  23. Some(None) => -1,
  24. None => -2,
  25. };
  26. Ok(ttl.into())
  27. }