Refactor to remove lazy_static for OnceLock+Mutex
This commit is contained in:
parent
e896ae2606
commit
5393ed3832
61
Cargo.lock
generated
61
Cargo.lock
generated
|
@ -1037,6 +1037,36 @@ dependencies = [
|
||||||
"slab",
|
"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]]
|
[[package]]
|
||||||
name = "generic-array"
|
name = "generic-array"
|
||||||
version = "0.14.7"
|
version = "0.14.7"
|
||||||
|
@ -2007,37 +2037,6 @@ dependencies = [
|
||||||
"prost",
|
"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]]
|
[[package]]
|
||||||
name = "quick-xml"
|
name = "quick-xml"
|
||||||
version = "0.30.0"
|
version = "0.30.0"
|
||||||
|
|
|
@ -16,7 +16,6 @@ hyper = { version = "0.14", features = ["full"] }
|
||||||
tower-http = { version = "0.4.1", features = ["fs", "add-extension", "auth", "compression-full", "trace"] }
|
tower-http = { version = "0.4.1", features = ["fs", "add-extension", "auth", "compression-full", "trace"] }
|
||||||
sha2 = "0.10.7"
|
sha2 = "0.10.7"
|
||||||
hex = "0.4"
|
hex = "0.4"
|
||||||
lazy_static = "1.4.0"
|
|
||||||
futures = "0.3.28"
|
futures = "0.3.28"
|
||||||
comrak = "0.1"
|
comrak = "0.1"
|
||||||
orgize = { git = "https://git.gmem.ca/arch/orgize.git", branch = "org-images" }
|
orgize = { git = "https://git.gmem.ca/arch/orgize.git", branch = "org-images" }
|
||||||
|
|
27
src/cache/mod.rs
vendored
27
src/cache/mod.rs
vendored
|
@ -1,12 +1,6 @@
|
||||||
use async_trait::async_trait;
|
|
||||||
use sqlx::FromRow;
|
use sqlx::FromRow;
|
||||||
use std::{
|
use std::sync::{OnceLock, Mutex};
|
||||||
fmt,
|
|
||||||
time::{SystemTime, UNIX_EPOCH},
|
|
||||||
};
|
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use tokio::sync::Mutex;
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, FromRow)]
|
#[derive(Clone, Debug, FromRow)]
|
||||||
pub struct CachedItem {
|
pub struct CachedItem {
|
||||||
|
@ -16,7 +10,7 @@ pub struct CachedItem {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Determine whether we should actually use the cached item or not.
|
/// 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()
|
// let current_time: i64 = SystemTime::now()
|
||||||
// .duration_since(UNIX_EPOCH)
|
// .duration_since(UNIX_EPOCH)
|
||||||
// .expect("SystemTime before UNIX EPOCH!")
|
// .expect("SystemTime before UNIX EPOCH!")
|
||||||
|
@ -28,12 +22,10 @@ fn should_use(item: &CachedItem) -> bool {
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
lazy_static! {
|
static CACHE: OnceLock<Mutex<HashMap<String, CachedItem>>> = OnceLock::new();
|
||||||
static ref CACHE: Mutex<HashMap<String, CachedItem>> = Mutex::new(HashMap::new());
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn get(key: &String) -> Option<CachedItem> {
|
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);
|
dbg!(&key);
|
||||||
match data.get(key) {
|
match data.get(key) {
|
||||||
Some(c) => {
|
Some(c) => {
|
||||||
|
@ -48,12 +40,13 @@ pub async fn get(key: &String) -> Option<CachedItem> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn rm(key: String) {
|
// async fn rm(key: String) {
|
||||||
let mut data = CACHE.lock().await;
|
// let mut data = CACHE.get().unwrap().clone();
|
||||||
data.remove(&key);
|
// data.remove(&key);
|
||||||
}
|
// let _ = CACHE.set(data);
|
||||||
|
// }
|
||||||
|
|
||||||
pub async fn set(key: String, item: CachedItem) {
|
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);
|
data.insert(key, item);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,3 @@
|
||||||
#[macro_use]
|
|
||||||
extern crate lazy_static;
|
|
||||||
|
|
||||||
// Include the `items` module, which is generated from items.proto.
|
// Include the `items` module, which is generated from items.proto.
|
||||||
pub mod items {
|
pub mod items {
|
||||||
include!(concat!(env!("OUT_DIR"), "/gabrielsimmerdotcom.gossip.rs"));
|
include!(concat!(env!("OUT_DIR"), "/gabrielsimmerdotcom.gossip.rs"));
|
||||||
|
|
Loading…
Reference in a new issue