JavaScript is disabled, refresh for a better experience. ambee/giterated

ambee/giterated

Git repository hosting, collaboration, and discovery for the Fediverse.

`giterated_cache` initial implementation

# Giterated Stack - Added the ability for dynamic substack handlers to exist for operations relevant to caching. - Added type metadata to the dynamic types. # Giterated Cache - Created - Implemented caching and fetching from cache. Hell fucking yes!!!! It works so good. Are you snooping in the commit logs because you're curious about the history of giterated? Cool that it got so big... tell me I say hi :)

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨86afeef

⁨giterated-cache/src/cache_get.rs⁩ - ⁨1536⁩ bytes
Raw
1 // use std::sync::Arc;
2
3 // use giterated_models::error::OperationError;
4 // use giterated_stack::GiteratedStack;
5 // use serde_json::Value;
6
7 // use crate::{cache_update::AnyObject, CacheSubstack};
8
9 // pub async fn try_value_get(
10 // object: AnyObject<'_>,
11 // value_name: &str,
12 // cache: CacheSubstack,
13 // stack: Arc<GiteratedStack>,
14 // ) -> Result<Value, OperationError<anyhow::Error>> {
15 // todo!()
16 // }
17
18 use std::sync::Arc;
19
20 use giterated_models::error::OperationError;
21 use giterated_stack::{AnyObject, AnyValue, GiteratedStack, StackOperationState};
22 use tracing::trace;
23
24 use crate::{CacheKey, CacheSubstack};
25
26 pub async fn get_cached(
27 object: AnyObject,
28 value_kind: String,
29 cache: CacheSubstack,
30 _operation_state: StackOperationState,
31 stack: Arc<GiteratedStack>,
32 ) -> Result<AnyValue, OperationError<anyhow::Error>> {
33 let object_meta = stack
34 .metadata
35 .objects
36 .get(object.kind())
37 .ok_or_else(|| OperationError::Unhandled)?;
38 let object_str = (object_meta.to_str)(object.clone());
39 let cache_key = CacheKey {
40 object: object_str,
41 value_name: value_kind.clone(),
42 };
43
44 trace!("Check cache for {}::{}", object.kind(), value_kind);
45
46 if let Some(cached_value) = cache.cache.get(&cache_key).await {
47 trace!("Found cached for {}::{}", object.kind(), value_kind);
48
49 Ok(cached_value.clone())
50 } else {
51 trace!("Nothing cached for {}::{}", object.kind(), value_kind);
52 Err(OperationError::Unhandled)
53 }
54 }
55