-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c4d0a08
commit 9411c4b
Showing
12 changed files
with
187 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ members = [ | |
"hitbox", | ||
"hitbox-backend", | ||
"hitbox-redis", | ||
"hitbox-inmemory", | ||
"hitbox-tower", | ||
"examples", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -96,6 +96,4 @@ async fn main() -> Result<(), CacheError> { | |
Ok(()) | ||
}*/ | ||
|
||
fn main() { | ||
|
||
} | ||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,6 +59,4 @@ async fn main() -> Result<(), CacheError> { | |
Ok(()) | ||
}*/ | ||
|
||
fn main() { | ||
|
||
} | ||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,6 +102,4 @@ async fn main() -> Result<(), CacheError> { | |
Ok(()) | ||
}*/ | ||
|
||
fn main() { | ||
|
||
} | ||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
mod cache_policy; | ||
mod predicates; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
pub mod backend; | ||
|
||
#[doc(inline)] | ||
pub use crate::backend::InMemoryBackend; |
Oops, something went wrong.