-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: utilizing integrationos cache wrapper on unified api (#51)
- Loading branch information
Showing
16 changed files
with
379 additions
and
145 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
74 changes: 58 additions & 16 deletions
74
integrationos-cache/src/local/connection_model_definition_cache.rs
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,56 +1,98 @@ | ||
use crate::LocalCacheExt; | ||
use futures::Future; | ||
use integrationos_domain::{ | ||
connection_model_definition::ConnectionModelDefinition, Id, IntegrationOSError, MongoStore, | ||
Unit, | ||
connection_model_definition::ConnectionModelDefinition, destination::Destination, Id, | ||
IntegrationOSError, MongoStore, Unit, | ||
}; | ||
use moka::future::Cache; | ||
use mongodb::bson::Document; | ||
use std::{sync::Arc, time::Duration}; | ||
use std::{fmt::Debug, hash::Hash, sync::Arc, time::Duration}; | ||
|
||
#[derive(Clone)] | ||
pub struct ConnectionModelDefinitionCache { | ||
inner: Arc<Cache<Id, ConnectionModelDefinition>>, | ||
pub struct ConnectionModelDefinitionCacheForKey< | ||
K: Clone + Send + Sync + Eq + Hash + Debug + 'static, | ||
> { | ||
inner: Arc<Cache<K, ConnectionModelDefinition>>, | ||
} | ||
|
||
impl ConnectionModelDefinitionCache { | ||
pub fn new(size: u64) -> Self { | ||
impl<K: Clone + Send + Sync + Eq + Hash + Debug + 'static> ConnectionModelDefinitionCacheForKey<K> { | ||
pub fn new(size: u64, ttl: u64) -> Self { | ||
Self { | ||
inner: Arc::new( | ||
Cache::builder() | ||
.max_capacity(size) | ||
.time_to_live(Duration::from_secs(120)) | ||
.time_to_live(Duration::from_secs(ttl)) | ||
.build(), | ||
), | ||
} | ||
} | ||
|
||
pub async fn get_or_insert_with_filter( | ||
&self, | ||
key: &Id, | ||
key: K, | ||
store: MongoStore<ConnectionModelDefinition>, | ||
filter: Document, | ||
) -> Result<ConnectionModelDefinition, IntegrationOSError> { | ||
self.inner | ||
.get_or_insert_with_filter(key, store, filter) | ||
.get_or_insert_with_filter(&key, store, filter) | ||
.await | ||
} | ||
|
||
pub async fn get_or_insert_with_fn<F, Fut>( | ||
&self, | ||
key: K, | ||
fa: F, | ||
) -> Result<ConnectionModelDefinition, IntegrationOSError> | ||
where | ||
F: FnOnce() -> Fut, | ||
Fut: Future<Output = Result<ConnectionModelDefinition, IntegrationOSError>>, | ||
{ | ||
let result = self.inner.get(&key).await; | ||
match result { | ||
Ok(Some(value)) => Ok(value), | ||
Ok(None) => { | ||
let value = fa().await?; | ||
self.inner.set(&key, &value).await?; | ||
Ok(value) | ||
} | ||
Err(e) => Err(e), | ||
} | ||
} | ||
|
||
pub async fn get( | ||
&self, | ||
key: &Id, | ||
key: K, | ||
) -> Result<Option<ConnectionModelDefinition>, IntegrationOSError> { | ||
self.inner.get(key).await | ||
self.inner.get(&key).await | ||
} | ||
|
||
pub async fn set( | ||
&self, | ||
key: &Id, | ||
key: K, | ||
value: &ConnectionModelDefinition, | ||
) -> Result<Unit, IntegrationOSError> { | ||
self.inner.set(key, value).await | ||
self.inner.set(&key, value).await | ||
} | ||
|
||
pub async fn remove(&self, key: &Id) -> Result<Unit, IntegrationOSError> { | ||
self.inner.remove(key).await | ||
pub async fn remove(&self, key: K) -> Result<Unit, IntegrationOSError> { | ||
self.inner.remove(&key).await | ||
} | ||
} | ||
|
||
#[derive(Clone)] | ||
pub struct ConnectionModelDefinitionCacheIdKey; | ||
|
||
impl ConnectionModelDefinitionCacheIdKey { | ||
pub fn create(size: u64, ttl: u64) -> ConnectionModelDefinitionCacheForKey<Id> { | ||
ConnectionModelDefinitionCacheForKey::new(size, ttl) | ||
} | ||
} | ||
|
||
pub type ConnectionModelDefinitionDestinationKey = | ||
ConnectionModelDefinitionCacheForKey<Destination>; | ||
|
||
impl ConnectionModelDefinitionDestinationKey { | ||
pub fn create(size: u64, ttl: u64) -> ConnectionModelDefinitionCacheForKey<Destination> { | ||
ConnectionModelDefinitionCacheForKey::new(size, ttl) | ||
} | ||
} |
Oops, something went wrong.