Skip to content

Commit

Permalink
redis: update backend trait
Browse files Browse the repository at this point in the history
  • Loading branch information
topenkoff committed Jul 9, 2023
1 parent c2e2520 commit 2aa8a8d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
19 changes: 12 additions & 7 deletions examples/examples/axum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ use hitbox_backend::{
serializer::{JsonSerializer, Serializer},
BackendError, BackendResult, CacheBackend, CacheableResponse, CachedValue, DeleteStatus,
};
use hitbox_redis::RedisBackend;
//use hitbox_redis::RedisBackend;
use hitbox_tower::Cache;
use http::StatusCode;
use lazy_static::lazy_static;
use stretto::AsyncCache;
use tower::ServiceBuilder;

lazy_static! {
static ref BACKEND: Arc<RedisBackend> = Arc::new(RedisBackend::new().unwrap());
}
//lazy_static! {
// static ref BACKEND: Arc<RedisBackend> = Arc::new(RedisBackend::new().unwrap());
//}

#[derive(Clone)]
struct InMemoryBackend {
Expand Down Expand Up @@ -105,8 +105,13 @@ async fn main() {
.filter_level(log::LevelFilter::Debug)
.init();

let backend = RedisBackend::new().unwrap();
//let backend = RedisBackend::new().unwrap();
let inmemory = InMemoryBackend::new();
let redis = hitbox_redis::fred::Builder::standalone()
.with_serializer::<axum::body::Bytes>()
.build()
.unwrap();

// build our application with a single route
let app = Router::new()
.route("/greet/:name/", get(handler_result))
Expand All @@ -117,8 +122,8 @@ async fn main() {
)
.layer(
ServiceBuilder::new()
.layer(Cache::builder().backend(inmemory).build())
.layer(Cache::builder().backend(backend).build()),
//.layer(Cache::builder().backend(inmemory).build())
.layer(Cache::new(redis)),
);

// run it with hyper on localhost:3000
Expand Down
7 changes: 4 additions & 3 deletions hitbox-redis/src/fred/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::marker::PhantomData;
use fred::clients::RedisClient;
use fred::interfaces::{ClientLike, KeysInterface};
use fred::types::{Expiration, FromRedis, RedisKey, RedisValue};
use serde::Serialize;

use async_trait::async_trait;
use hitbox_backend::{
Expand Down Expand Up @@ -79,16 +80,16 @@ where
async fn set<T>(
&self,
key: String,
value: CachedValue<T::Cached>,
value: &CachedValue<T::Cached>,
ttl: Option<u32>,
) -> BackendResult<()>
where
T: CacheableResponse + Send,
<T as CacheableResponse>::Cached: serde::Serialize + Send,
<T as CacheableResponse>::Cached: Serialize + Send + Sync,
{
tracing::debug!("RedisBackend::set::{}", &key);
let key = RedisKey::from(key);
let ser_value = S::serialize(&value).map_err(BackendError::from)?;
let ser_value = S::serialize(value).map_err(BackendError::from)?;
self.execute(|client| async move {
let expire = ttl.map(|ttl| Expiration::EX(ttl as i64));
client
Expand Down

0 comments on commit 2aa8a8d

Please sign in to comment.