39 lines
1.3 KiB
Rust
39 lines
1.3 KiB
Rust
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);
|
||
// }
|