wip: broken state
This commit is contained in:
@ -16,12 +16,9 @@ use tracing_subscriber::{filter, layer::SubscriberExt, prelude::*, util::Subscri
|
||||
mod config;
|
||||
mod datasources;
|
||||
mod routes;
|
||||
mod state;
|
||||
mod utils;
|
||||
|
||||
pub struct AppState {
|
||||
db: PgPool,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
// setting up configuration
|
||||
|
73
backend/public/src/state.rs
Normal file
73
backend/public/src/state.rs
Normal file
@ -0,0 +1,73 @@
|
||||
use fred::interfaces::KeysInterface;
|
||||
use fred::{clients::Pool, prelude::*};
|
||||
use serde_json::Value;
|
||||
use sqlx::PgPool;
|
||||
|
||||
pub type AppState = std::sync::Arc<tokio::sync::Mutex<AppInternalState>>;
|
||||
|
||||
pub struct AppInternalState {
|
||||
pub database: sqlx::postgres::PgPool,
|
||||
pub cache: Cache,
|
||||
}
|
||||
|
||||
pub struct Cache {
|
||||
pub inmem: Pool,
|
||||
}
|
||||
|
||||
impl AppInternalState {
|
||||
pub fn new(database: PgPool, cache: Pool) -> Self {
|
||||
AppInternalState {
|
||||
database,
|
||||
cache: Cache { inmem: cache },
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Cache {
|
||||
pub async fn get<T>(&mut self, key: String) -> Result<Option<T>, Box<dyn std::error::Error>>
|
||||
where
|
||||
T: for<'de> serde::Deserialize<'de>,
|
||||
{
|
||||
if !self.inmem.is_connected() {
|
||||
return Err(Box::new("Are you connected to the cache?".into()));
|
||||
}
|
||||
|
||||
let value: Option<Value> = self.inmem.get(key).await?;
|
||||
|
||||
let result = match value {
|
||||
Some(x) => match serde_json::from_value(x) {
|
||||
Ok(x) => Some(x),
|
||||
Err(_) => None,
|
||||
},
|
||||
None => None,
|
||||
};
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
pub async fn set<T>(
|
||||
&mut self,
|
||||
key: String,
|
||||
contents: &T,
|
||||
expiration: Option<Expiration>,
|
||||
set_opts: Option<SetOptions>,
|
||||
get: bool,
|
||||
) -> Result<(), Box<dyn std::error::Error>>
|
||||
where
|
||||
T: for<'de> serde::Deserialize<'de>,
|
||||
{
|
||||
if !self.inmem.is_connected() {
|
||||
return Err(Box::new("Are you connected to the cache?".into()));
|
||||
}
|
||||
|
||||
let value: Value = serde_json::to_value(contents)?;
|
||||
self.inmem
|
||||
.set(key, value.to_string(), expiration, set_opts, get)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn del(&mut self, key: String) -> Result<(), Box<dyn std::error::Error>> {
|
||||
self.inmem.del(key).await?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user