Process manager fixes (#71)
* fix: launching on linux * feat: #70 * feat: add dummy store page * feat: add store redir and refresh button to library * feat: cache first object fetching * feat: Remove let_chains feature and update to Rust 2024 Signed-off-by: quexeky <git@quexeky.dev> * feat: Check for if process was manually stopped Signed-off-by: quexeky <git@quexeky.dev> * fix: use bitcode instead of serde * chore: remove logs * fix: clippy * fix: clippy 2 * fix: swap to stop icon --------- Signed-off-by: quexeky <git@quexeky.dev> Co-authored-by: quexeky <git@quexeky.dev>
This commit is contained in:
@@ -1,11 +1,15 @@
|
||||
use std::{
|
||||
fmt::Display,
|
||||
time::{Duration, SystemTime},
|
||||
};
|
||||
|
||||
use crate::{
|
||||
database::{db::borrow_db_checked, models::data::Database},
|
||||
error::remote_access_error::RemoteAccessError,
|
||||
};
|
||||
use bitcode::{Decode, DecodeOwned, Encode};
|
||||
use cacache::Integrity;
|
||||
use http::{header::CONTENT_TYPE, response::Builder as ResponseBuilder, Response};
|
||||
use serde::{de::DeserializeOwned, Deserialize, Serialize};
|
||||
use serde_binary::binary_stream::Endian;
|
||||
use http::{Response, header::CONTENT_TYPE, response::Builder as ResponseBuilder};
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! offline {
|
||||
@@ -19,31 +23,48 @@ macro_rules! offline {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn cache_object<K: AsRef<str>, D: Serialize + DeserializeOwned>(
|
||||
pub fn cache_object<K: AsRef<str>, D: Encode>(
|
||||
key: K,
|
||||
data: &D,
|
||||
) -> Result<Integrity, RemoteAccessError> {
|
||||
let bytes = serde_binary::to_vec(data, Endian::Little).unwrap();
|
||||
let bytes = bitcode::encode(data);
|
||||
cacache::write_sync(&borrow_db_checked().cache_dir, key, bytes)
|
||||
.map_err(RemoteAccessError::Cache)
|
||||
}
|
||||
pub fn get_cached_object<K: AsRef<str>, D: Serialize + DeserializeOwned>(
|
||||
pub fn get_cached_object<K: AsRef<str> + Display, D: Encode + DecodeOwned>(
|
||||
key: K,
|
||||
) -> Result<D, RemoteAccessError> {
|
||||
get_cached_object_db::<K, D>(key, &borrow_db_checked())
|
||||
}
|
||||
pub fn get_cached_object_db<K: AsRef<str>, D: Serialize + DeserializeOwned>(
|
||||
pub fn get_cached_object_db<K: AsRef<str> + Display, D: DecodeOwned>(
|
||||
key: K,
|
||||
db: &Database,
|
||||
) -> Result<D, RemoteAccessError> {
|
||||
let bytes = cacache::read_sync(&db.cache_dir, key).map_err(RemoteAccessError::Cache)?;
|
||||
let data = serde_binary::from_slice::<D>(&bytes, Endian::Little).unwrap();
|
||||
let bytes = cacache::read_sync(&db.cache_dir, &key).map_err(RemoteAccessError::Cache)?;
|
||||
let data = bitcode::decode::<D>(&bytes).map_err(|_| {
|
||||
RemoteAccessError::Cache(cacache::Error::EntryNotFound(
|
||||
db.cache_dir.clone(),
|
||||
key.to_string(),
|
||||
))
|
||||
})?;
|
||||
Ok(data)
|
||||
}
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[derive(Encode, Decode)]
|
||||
pub struct ObjectCache {
|
||||
content_type: String,
|
||||
body: Vec<u8>,
|
||||
expiry: u128,
|
||||
}
|
||||
|
||||
impl ObjectCache {
|
||||
pub fn has_expired(&self) -> bool {
|
||||
let duration = Duration::from_millis(self.expiry.try_into().unwrap());
|
||||
SystemTime::UNIX_EPOCH
|
||||
.checked_add(duration)
|
||||
.unwrap()
|
||||
.elapsed()
|
||||
.is_err()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Response<Vec<u8>>> for ObjectCache {
|
||||
@@ -57,6 +78,12 @@ impl From<Response<Vec<u8>>> for ObjectCache {
|
||||
.unwrap()
|
||||
.to_owned(),
|
||||
body: value.body().clone(),
|
||||
expiry: SystemTime::now()
|
||||
.checked_add(Duration::from_days(1))
|
||||
.unwrap()
|
||||
.duration_since(SystemTime::UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_millis(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -66,3 +93,9 @@ impl From<ObjectCache> for Response<Vec<u8>> {
|
||||
resp_builder.body(value.body).unwrap()
|
||||
}
|
||||
}
|
||||
impl From<&ObjectCache> for Response<Vec<u8>> {
|
||||
fn from(value: &ObjectCache) -> Self {
|
||||
let resp_builder = ResponseBuilder::new().header(CONTENT_TYPE, value.content_type.clone());
|
||||
resp_builder.body(value.body.clone()).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user