bvplan/bvplan/src/cache.rs
2023-02-27 11:09:16 +01:00

78 lines
2.2 KiB
Rust

use anyhow::Result;
use lazy_static::lazy_static;
use r2d2_redis::{r2d2, redis, RedisConnectionManager};
use crate::config;
use crate::db;
pub type RedisPool = r2d2::Pool<RedisConnectionManager>;
pub type ConnectionPool = r2d2::PooledConnection<RedisConnectionManager>;
pub type Connection = redis::Connection;
pub fn establish_connection() -> Result<RedisConnectionManager> {
Ok(RedisConnectionManager::new(
config::CONFIG.redis_url.as_str(),
)?)
}
pub fn pool() -> Result<RedisPool> {
Ok(r2d2::Pool::builder().build(establish_connection()?)?)
}
lazy_static! {
pub static ref POOL: RedisPool = pool().unwrap();
}
pub mod keys {
use super::*;
// pub const SUBSTITUTIONS: &str = "substs";
pub const SUBSTITUTIONS_HTML: &str = "substs_html";
pub mod substitutions {
use chrono::prelude::*;
use serde::{Deserialize, Serialize};
use super::*;
#[derive(Deserialize, Serialize, Debug)]
pub struct Substitution {
#[serde(rename = "tm")]
pub time: (usize, Option<usize>),
#[serde(rename = "cl")]
pub classes: Vec<String>,
#[serde(rename = "ps")]
pub prev_subject: String,
#[serde(rename = "s")]
pub subject: Option<String>,
#[serde(rename = "t")]
pub teachers: Vec<String>,
#[serde(rename = "pr")]
pub prev_room: Option<String>,
#[serde(rename = "r")]
pub room: Option<String>,
#[serde(rename = "tx")]
pub text: Option<String>,
}
#[derive(Deserialize, Serialize, Debug)]
pub struct SubstitutionQuery {
#[serde(rename = "d")]
pub date: NaiveDate,
#[serde(rename = "w")]
pub week_type: db::models::WeekType,
#[serde(rename = "q")]
pub queried_at: NaiveDateTime,
#[serde(rename = "i")]
pub last_import_time: NaiveDateTime,
#[serde(rename = "y")]
pub schoolyear: String,
#[serde(rename = "t")]
pub tenant: String,
#[serde(rename = "s")]
pub substitutions: Vec<Substitution>,
}
}
}