chuanyue-service/library/src/core/local_cache.rs
2024-04-16 21:56:32 +08:00

39 lines
1.3 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

use std::collections::HashMap;
use moka::future::Cache;
// use std::any::{Any, TypeId};
// use std::collections::HashMap;
// use std::lazy::SyncLazy;
// use moka::sync::Cache;
//
// type CacheKey = String;
//
// // 全局缓存,键为 String值为 Box<dyn Any>
// static GLOBAL_CACHE: SyncLazy<HashMap<TypeId, Cache<CacheKey, Box<dyn Any>>>> = SyncLazy::new(|| {
// HashMap::new()
// });
//
// fn insert<T: 'static + Any>(key: CacheKey, value: T) {
// let cache_type_id = TypeId::of::<T>();
// let mut cache = GLOBAL_CACHE.entry(cache_type_id).or_insert_with(|| {
// let config = moka::Config::builder().max_capacity(1000).build();
// Cache::with_config(config)
// });
// cache.insert(key, Box::new(value));
// }
//
// fn get<T: 'static + Any>(key: CacheKey) -> Option<T> {
// let cache_type_id = TypeId::of::<T>();
// GLOBAL_CACHE.get(&cache_type_id)?.get(&key)?.downcast::<T>().ok()
// }
//
// fn main() {
// insert("int_key".into(), 42);
// let int_value: Option<i32> = get("int_key".into());
// println!("Integer value: {:?}", int_value);
//
// insert("string_key".into(), "hello".to_string());
// let string_value: Option<String> = get("string_key".into());
// println!("String value: {:?}", string_value);
// }