Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
georgeboot committed Jan 27, 2025
1 parent 54bd158 commit 7abfbbe
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 44 deletions.
6 changes: 3 additions & 3 deletions src/cache/drivers/inmem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ use std::{
time::{Duration, Instant},
};

use super::CacheDriver;
use crate::cache::CacheResult;
use crate::config::InMemCacheConfig;
use async_trait::async_trait;
use moka::{sync::Cache, Expiry};
use serde::de::DeserializeOwned;
use serde::Serialize;
use super::CacheDriver;
use crate::cache::CacheResult;
use crate::config::InMemCacheConfig;

/// Creates a new instance of the in-memory cache driver, with a default Loco
/// configuration.
Expand Down
2 changes: 1 addition & 1 deletion src/cache/drivers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
//! This module defines traits and implementations for cache drivers.
use std::time::Duration;

use super::CacheResult;
use async_trait::async_trait;
use serde::de::DeserializeOwned;
use serde::Serialize;
use super::CacheResult;

#[cfg(feature = "cache_inmem")]
pub mod inmem;
Expand Down
39 changes: 12 additions & 27 deletions src/cache/drivers/redis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ use std::{
time::{Duration, Instant},
};

use super::CacheDriver;
use crate::cache::{CacheError, CacheResult};
use crate::config::RedisCacheConfig;
use async_trait::async_trait;
use bb8::Pool;
use bb8_redis::RedisConnectionManager;
use opendal::services::Redis as RedisService;
use opendal::Builder;
use redis::{cmd, AsyncCommands};
use serde::de::DeserializeOwned;
use serde::Serialize;
use super::CacheDriver;
use crate::cache::{CacheError, CacheResult};
use crate::config::RedisCacheConfig;

/// Creates a new instance of the in-memory cache driver, with a default Loco
/// configuration.
Expand Down Expand Up @@ -58,7 +60,6 @@ impl CacheDriver for Redis {
async fn contains_key(&self, key: &str) -> CacheResult<bool> {
let mut connection = self.redis.get().await?;
Ok(connection.exists(key).await?)

}

/// Retrieves a value from the cache based on the provided key.
Expand All @@ -72,8 +73,8 @@ impl CacheDriver for Redis {

match data {
Some(bytes) => {
let value = rmp_serde::from_slice(&bytes)
.map_err(|e| CacheError::Any(Box::new(e)))?;
let value =
rmp_serde::from_slice(&bytes).map_err(|e| CacheError::Any(Box::new(e)))?;
Ok(Some(value))
}
None => Ok(None),
Expand All @@ -87,8 +88,7 @@ impl CacheDriver for Redis {
/// Returns a `CacheError` if there is an error during the operation.
async fn insert<T: Serialize>(&self, key: &str, value: &T) -> CacheResult<()> {
let mut connection = self.redis.get().await?;
let encoded = rmp_serde::to_vec(value)
.map_err(|e| CacheError::Any(Box::new(e)))?;
let encoded = rmp_serde::to_vec(value).map_err(|e| CacheError::Any(Box::new(e)))?;
connection.set(key, encoded).await?;
Ok(())
}
Expand All @@ -107,9 +107,10 @@ impl CacheDriver for Redis {
duration: Duration,
) -> CacheResult<()> {
let mut connection = self.redis.get().await?;
let encoded = rmp_serde::to_vec(value)
.map_err(|e| CacheError::Any(Box::new(e)))?;
connection.set_ex(key, encoded, duration.as_secs() as usize).await?;
let encoded = rmp_serde::to_vec(value).map_err(|e| CacheError::Any(Box::new(e)))?;
connection
.set_ex(key, encoded, duration.as_secs() as usize)
.await?;
Ok(())
}

Expand Down Expand Up @@ -137,22 +138,6 @@ impl CacheDriver for Redis {
}
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Expiration {
Never,
AfterDuration(Duration),
}

impl Expiration {
#[must_use]
pub fn as_duration(&self) -> Option<Duration> {
match self {
Self::Never => None,
Self::AfterDuration(d) => Some(*d),
}
}
}

#[cfg(test)]
mod tests {

Expand Down
24 changes: 11 additions & 13 deletions src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
//! This module provides a generic cache interface for various cache drivers.
pub mod drivers;

use std::{future::Future, time::Duration};
use std::sync::Arc;
use serde::de::DeserializeOwned;
use serde::Serialize;
use self::drivers::CacheDriver;
use crate::bgworker::{pg, skq, sqlt, Queue};
use crate::cache::drivers::{inmem, redis};
use crate::config::Config;
use self::drivers::CacheDriver;
use crate::{config, Error, Result as LocoResult};
use crate::cache::drivers::{inmem, redis};
use serde::de::DeserializeOwned;
use serde::Serialize;
use std::sync::Arc;
use std::{future::Future, time::Duration};

/// Errors related to cache operations
#[derive(thiserror::Error, Debug)]
Expand All @@ -31,12 +31,8 @@ pub type CacheResult<T> = std::result::Result<T, CacheError>;
#[allow(clippy::missing_panics_doc)]
pub async fn create_cache_provider(config: &Config) -> crate::Result<Arc<Cache>> {
match &config.cache {
config::CacheConfig::Redis(config) => {
Ok(Arc::new(redis::new(config).await?))
}
config::CacheConfig::InMem(config) => {
Ok(Arc::new(inmem::new(config).await?))
}
config::CacheConfig::Redis(config) => Ok(Arc::new(redis::new(config).await?)),
config::CacheConfig::InMem(config) => Ok(Arc::new(inmem::new(config).await?)),

#[allow(unreachable_patterns)]
_ => Err(Error::string(
Expand Down Expand Up @@ -212,7 +208,9 @@ impl Cache {
Ok(value)
} else {
let value = f.await?;
self.driver.insert_with_expiry(key, &value, duration).await?;
self.driver
.insert_with_expiry(key, &value, duration)
.await?;
Ok(value)
}
}
Expand Down

0 comments on commit 7abfbbe

Please sign in to comment.