31 lines
893 B
Rust
31 lines
893 B
Rust
use crate::config;
|
|
use crate::db;
|
|
use crate::model;
|
|
use moka::sync::Cache;
|
|
use std::sync::Arc;
|
|
use std::time::{Duration, Instant};
|
|
|
|
#[derive(Clone)]
|
|
pub struct AppState {
|
|
pub storage: db::UserStorage,
|
|
pub config: config::Config,
|
|
pub cache: Arc<Cache<String, (Vec<model::GetUserResponse>, Instant)>>,
|
|
}
|
|
|
|
impl AppState {
|
|
pub async fn new() -> anyhow::Result<Self> {
|
|
let config = config::Config::new().await?;
|
|
let user_storage = db::init_user_storage().await;
|
|
let cache = Cache::builder()
|
|
.max_capacity(10_000) // максимальное количество записей
|
|
.time_to_live(Duration::from_secs(300)) // TTL 5 минут
|
|
.build();
|
|
let state = AppState {
|
|
storage: user_storage,
|
|
config: config.clone(),
|
|
cache: Arc::new(cache),
|
|
};
|
|
Ok(state)
|
|
}
|
|
}
|