Skip to content

Commit

Permalink
Add Inmemory backend
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreyErmilov committed Jul 12, 2023
1 parent c4d0a08 commit 9411c4b
Show file tree
Hide file tree
Showing 12 changed files with 187 additions and 142 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ members = [
"hitbox",
"hitbox-backend",
"hitbox-redis",
"hitbox-inmemory",
"hitbox-tower",
"examples",
]
3 changes: 2 additions & 1 deletion examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ name = "hitbox-examples"
version = "0.0.0"
publish = false
edition = "2021"

[features]
default = []

Expand All @@ -11,7 +12,7 @@ hitbox = { path = "../hitbox", features = ["metrics"] }
hitbox-backend = { path = "../hitbox-backend" }
hitbox-tower = { path = "../hitbox-tower" }
hitbox-redis = { path = "../hitbox-redis" }
stretto = { version = "0.8", features = ["async"] }
hitbox-inmemory = { path = "../hitbox-inmemory" }
actix = "0.13"
axum = "0.6"
log = "0.4"
Expand Down
4 changes: 1 addition & 3 deletions examples/examples/async_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,4 @@ async fn main() -> Result<(), CacheError> {
Ok(())
}*/

fn main() {

}
fn main() {}
4 changes: 1 addition & 3 deletions examples/examples/cacheable_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,4 @@ async fn main() -> Result<(), CacheError> {
Ok(())
}*/

fn main() {

}
fn main() {}
4 changes: 1 addition & 3 deletions examples/examples/sync_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,4 @@ async fn main() -> Result<(), CacheError> {
Ok(())
}*/

fn main() {

}
fn main() {}
57 changes: 1 addition & 56 deletions examples/examples/tower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use hitbox_backend::{
serializer::{JsonSerializer, Serializer},
BackendError, BackendResult, CacheBackend, CacheableResponse, CachedValue, DeleteStatus,
};
use hitbox_inmemory::InMemoryBackend;
use hitbox_tower::Cache;
use hyper::{Body, Server};
use std::{convert::Infallible, net::SocketAddr};
use stretto::AsyncCache;

use http::{Request, Response};
use tower::make::Shared;
Expand All @@ -15,61 +15,6 @@ async fn handle(_: Request<Body>) -> Result<Response<Body>, Infallible> {
Ok(Response::new("Hello, World!".into()))
}

#[derive(Clone)]
struct InMemoryBackend {
cache: AsyncCache<String, Vec<u8>>,
}

impl InMemoryBackend {
fn new() -> Self {
Self {
cache: AsyncCache::new(12960, 1e6 as i64, tokio::spawn).unwrap(),
}
}
}

#[async_trait]
impl CacheBackend for InMemoryBackend {
async fn get<T>(&self, key: String) -> BackendResult<Option<CachedValue<T::Cached>>>
where
T: CacheableResponse,
<T as CacheableResponse>::Cached: serde::de::DeserializeOwned,
{
match self.cache.get(&key).await {
Some(cached) => Ok(Some(
JsonSerializer::<Vec<u8>>::deserialize(cached.value().to_owned())
.map_err(BackendError::from)
.unwrap(),
)),
None => Ok(None),
}
}

async fn set<T>(
&self,
key: String,
value: CachedValue<T::Cached>,
ttl: Option<u32>,
) -> BackendResult<()>
where
T: CacheableResponse + Send,
<T as CacheableResponse>::Cached: serde::Serialize + Send,
{
let serialized =
JsonSerializer::<Vec<u8>>::serialize(&value).map_err(BackendError::from)?;
self.cache.insert(key, serialized, 42).await;
Ok(())
}

async fn delete(&self, key: String) -> BackendResult<DeleteStatus> {
unimplemented!()
}

async fn start(&self) -> BackendResult<()> {
Ok(())
}
}

#[tokio::main]
async fn main() {
let inmemory = InMemoryBackend::new();
Expand Down
1 change: 0 additions & 1 deletion hitbox-http/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
mod cache_policy;
mod predicates;

26 changes: 26 additions & 0 deletions hitbox-inmemory/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[package]
name = "hitbox-inmemory"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
hitbox = { path = "../hitbox", features = ["metrics"] }
hitbox-backend = { path = "../hitbox-backend" }
hitbox-tower = { path = "../hitbox-tower" }
hitbox-redis = { path = "../hitbox-redis" }
stretto = { version = "0.8", features = ["async"] }
actix = "0.13"
axum = "0.6"
log = "0.4"
actix-rt = "2"
serde = { version = "1", features = ["derive"] }
env_logger = "0.10"
serde_json = "1"
hyper = { version = "0.14", features = ["full"] }
tokio = { version = "1", features = ["full"] }
tower = { version = "0.4", features = ["full"] }
tower-http = { version = "0.4", features = ["full"] }
http = "0.2"
lazy_static = "1"
72 changes: 72 additions & 0 deletions hitbox-inmemory/src/backend.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
use axum::async_trait;
use hitbox_backend::{
serializer::{JsonSerializer, Serializer},
BackendError, BackendResult, CacheBackend, CacheableResponse, CachedValue, DeleteStatus,
};
use std::time::Duration;
use stretto::AsyncCache;

const COST: i64 = 0;

#[derive(Clone)]
pub struct InMemoryBackend {
cache: AsyncCache<String, Vec<u8>>,
}

impl InMemoryBackend {
pub fn new() -> Self {
Self {
cache: AsyncCache::new(12960, 1e6 as i64, tokio::spawn).unwrap(),
}
}
}

#[async_trait]
impl CacheBackend for InMemoryBackend {
async fn get<T>(&self, key: String) -> BackendResult<Option<CachedValue<T::Cached>>>
where
T: CacheableResponse,
<T as CacheableResponse>::Cached: serde::de::DeserializeOwned,
{
match self.cache.get(&key).await {
Some(cached) => Ok(Some(
JsonSerializer::<Vec<u8>>::deserialize(cached.value().to_owned())
.map_err(BackendError::from)
.unwrap(),
)),
None => Ok(None),
}
}

async fn set<T>(
&self,
key: String,
value: &CachedValue<T::Cached>,
ttl: Option<u32>,
) -> BackendResult<()>
where
T: CacheableResponse + Send,
T::Cached: serde::Serialize + Send + Sync,
{
let serialized =
JsonSerializer::<Vec<u8>>::serialize(&value).map_err(BackendError::from)?;
let _ = match ttl {
Some(ttl) => {
self.cache
.insert_with_ttl(key, serialized, COST, Duration::from_secs(ttl as u64))
.await
}
None => self.cache.insert(key, serialized, COST).await,
};
Ok(())
}

async fn delete(&self, key: String) -> BackendResult<DeleteStatus> {
self.cache.remove(&key).await;
Ok(DeleteStatus::Deleted(0))
}

async fn start(&self) -> BackendResult<()> {
Ok(())
}
}
4 changes: 4 additions & 0 deletions hitbox-inmemory/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub mod backend;

#[doc(inline)]
pub use crate::backend::InMemoryBackend;
Loading

0 comments on commit 9411c4b

Please sign in to comment.