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

ambee/giterated

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

Re-expose Operation State in generics.

This is the worst code I have ever written. I hate the way this changes everything. ugh.

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨90db3e2

⁨giterated-cache/src/cache_get.rs⁩ - ⁨1446⁩ 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 giterated_models::error::OperationError;
19 use giterated_stack::{AnyObject, AnyValue, GiteratedStack};
20 use tracing::trace;
21
22 use crate::{CacheKey, CacheSubstack};
23
24 pub async fn get_cached(
25 object: AnyObject,
26 value_kind: String,
27 cache: CacheSubstack,
28 stack: GiteratedStack,
29 ) -> Result<AnyValue, OperationError<anyhow::Error>> {
30 let object_meta = stack
31 .metadata
32 .objects
33 .get(object.kind())
34 .ok_or_else(|| OperationError::Unhandled)?;
35 let object_str = (object_meta.to_str)(object.clone());
36 let cache_key = CacheKey {
37 object: object_str,
38 value_name: value_kind.clone(),
39 };
40
41 trace!("Check cache for {}::{}", object.kind(), value_kind);
42
43 if let Some(cached_value) = cache.cache.get(&cache_key).await {
44 trace!("Found cached for {}::{}", object.kind(), value_kind);
45
46 Ok(cached_value.clone())
47 } else {
48 trace!("Nothing cached for {}::{}", object.kind(), value_kind);
49 Err(OperationError::Unhandled)
50 }
51 }
52