Refactor to remove lazy_static for OnceLock+Mutex

This commit is contained in:
Gabriel Simmer 2023-10-23 20:11:16 +01:00
parent e896ae2606
commit 5393ed3832
Signed by: arch
SSH key fingerprint: SHA256:m3OEcdtrnBpMX+2BDGh/byv3hrCekCLzDYMdvGEKPPQ
4 changed files with 40 additions and 52 deletions

61
Cargo.lock generated
View file

@ -1037,6 +1037,36 @@ dependencies = [
"slab",
]
[[package]]
name = "gabrielsimmerdotcom"
version = "0.1.0"
dependencies = [
"async-trait",
"axum",
"clap 4.3.21",
"comrak",
"crossbeam",
"file-format",
"frontmatter",
"futures",
"hex",
"hyper",
"maud",
"orgize",
"prost",
"prost-build",
"rand",
"rss",
"serde",
"serde_dhall",
"sha2",
"sqlx",
"time",
"tokio",
"tower",
"tower-http",
]
[[package]]
name = "generic-array"
version = "0.14.7"
@ -2007,37 +2037,6 @@ dependencies = [
"prost",
]
[[package]]
name = "quick-start"
version = "0.1.0"
dependencies = [
"async-trait",
"axum",
"clap 4.3.21",
"comrak",
"crossbeam",
"file-format",
"frontmatter",
"futures",
"hex",
"hyper",
"lazy_static 1.4.0",
"maud",
"orgize",
"prost",
"prost-build",
"rand",
"rss",
"serde",
"serde_dhall",
"sha2",
"sqlx",
"time",
"tokio",
"tower",
"tower-http",
]
[[package]]
name = "quick-xml"
version = "0.30.0"

View file

@ -16,7 +16,6 @@ hyper = { version = "0.14", features = ["full"] }
tower-http = { version = "0.4.1", features = ["fs", "add-extension", "auth", "compression-full", "trace"] }
sha2 = "0.10.7"
hex = "0.4"
lazy_static = "1.4.0"
futures = "0.3.28"
comrak = "0.1"
orgize = { git = "https://git.gmem.ca/arch/orgize.git", branch = "org-images" }

27
src/cache/mod.rs vendored
View file

@ -1,12 +1,6 @@
use async_trait::async_trait;
use sqlx::FromRow;
use std::{
fmt,
time::{SystemTime, UNIX_EPOCH},
};
use std::sync::{OnceLock, Mutex};
use std::collections::HashMap;
use tokio::sync::Mutex;
#[derive(Clone, Debug, FromRow)]
pub struct CachedItem {
@ -16,7 +10,7 @@ pub struct CachedItem {
}
/// Determine whether we should actually use the cached item or not.
fn should_use(item: &CachedItem) -> bool {
fn should_use(_item: &CachedItem) -> bool {
// let current_time: i64 = SystemTime::now()
// .duration_since(UNIX_EPOCH)
// .expect("SystemTime before UNIX EPOCH!")
@ -28,12 +22,10 @@ fn should_use(item: &CachedItem) -> bool {
true
}
lazy_static! {
static ref CACHE: Mutex<HashMap<String, CachedItem>> = Mutex::new(HashMap::new());
}
static CACHE: OnceLock<Mutex<HashMap<String, CachedItem>>> = OnceLock::new();
pub async fn get(key: &String) -> Option<CachedItem> {
let data = CACHE.lock().await;
let data = CACHE.get_or_init(|| Mutex::new(HashMap::new())).lock().unwrap();
dbg!(&key);
match data.get(key) {
Some(c) => {
@ -48,12 +40,13 @@ pub async fn get(key: &String) -> Option<CachedItem> {
}
}
async fn rm(key: String) {
let mut data = CACHE.lock().await;
data.remove(&key);
}
// async fn rm(key: String) {
// let mut data = CACHE.get().unwrap().clone();
// data.remove(&key);
// let _ = CACHE.set(data);
// }
pub async fn set(key: String, item: CachedItem) {
let mut data = CACHE.lock().await;
let mut data = CACHE.get_or_init(|| Mutex::new(HashMap::new())).lock().unwrap();
data.insert(key, item);
}

View file

@ -1,6 +1,3 @@
#[macro_use]
extern crate lazy_static;
// Include the `items` module, which is generated from items.proto.
pub mod items {
include!(concat!(env!("OUT_DIR"), "/gabrielsimmerdotcom.gossip.rs"));