Add helper method for caching function results (#5220)
* Add helper method for caching function results * fmt
This commit is contained in:
parent
63ea99d38a
commit
2848c076af
9 changed files with 85 additions and 75 deletions
3
Cargo.lock
generated
3
Cargo.lock
generated
|
@ -2550,7 +2550,6 @@ dependencies = [
|
||||||
"lemmy_db_views",
|
"lemmy_db_views",
|
||||||
"lemmy_db_views_actor",
|
"lemmy_db_views_actor",
|
||||||
"lemmy_utils",
|
"lemmy_utils",
|
||||||
"moka",
|
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"serde_with",
|
"serde_with",
|
||||||
|
@ -2632,7 +2631,6 @@ dependencies = [
|
||||||
"futures-util",
|
"futures-util",
|
||||||
"i-love-jesus",
|
"i-love-jesus",
|
||||||
"lemmy_utils",
|
"lemmy_utils",
|
||||||
"moka",
|
|
||||||
"pretty_assertions",
|
"pretty_assertions",
|
||||||
"regex",
|
"regex",
|
||||||
"rustls 0.23.16",
|
"rustls 0.23.16",
|
||||||
|
@ -2819,6 +2817,7 @@ dependencies = [
|
||||||
"markdown-it-ruby",
|
"markdown-it-ruby",
|
||||||
"markdown-it-sub",
|
"markdown-it-sub",
|
||||||
"markdown-it-sup",
|
"markdown-it-sup",
|
||||||
|
"moka",
|
||||||
"pretty_assertions",
|
"pretty_assertions",
|
||||||
"regex",
|
"regex",
|
||||||
"reqwest-middleware",
|
"reqwest-middleware",
|
||||||
|
|
|
@ -60,6 +60,7 @@ use lemmy_utils::{
|
||||||
slurs::{build_slur_regex, remove_slurs},
|
slurs::{build_slur_regex, remove_slurs},
|
||||||
validation::clean_urls_in_text,
|
validation::clean_urls_in_text,
|
||||||
},
|
},
|
||||||
|
CacheLock,
|
||||||
CACHE_DURATION_FEDERATION,
|
CACHE_DURATION_FEDERATION,
|
||||||
};
|
};
|
||||||
use moka::future::Cache;
|
use moka::future::Cache;
|
||||||
|
@ -535,7 +536,7 @@ pub fn local_site_opt_to_slur_regex(local_site: &Option<LocalSite>) -> Option<Le
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_url_blocklist(context: &LemmyContext) -> LemmyResult<RegexSet> {
|
pub async fn get_url_blocklist(context: &LemmyContext) -> LemmyResult<RegexSet> {
|
||||||
static URL_BLOCKLIST: LazyLock<Cache<(), RegexSet>> = LazyLock::new(|| {
|
static URL_BLOCKLIST: CacheLock<RegexSet> = LazyLock::new(|| {
|
||||||
Cache::builder()
|
Cache::builder()
|
||||||
.max_capacity(1)
|
.max_capacity(1)
|
||||||
.time_to_live(CACHE_DURATION_FEDERATION)
|
.time_to_live(CACHE_DURATION_FEDERATION)
|
||||||
|
|
|
@ -25,7 +25,6 @@ tracing = { workspace = true }
|
||||||
url = { workspace = true }
|
url = { workspace = true }
|
||||||
futures.workspace = true
|
futures.workspace = true
|
||||||
uuid = { workspace = true }
|
uuid = { workspace = true }
|
||||||
moka.workspace = true
|
|
||||||
anyhow.workspace = true
|
anyhow.workspace = true
|
||||||
chrono.workspace = true
|
chrono.workspace = true
|
||||||
webmention = "0.6.0"
|
webmention = "0.6.0"
|
||||||
|
|
|
@ -16,11 +16,11 @@ use lemmy_db_schema::source::{
|
||||||
use lemmy_db_views::structs::{LocalUserView, SiteView};
|
use lemmy_db_views::structs::{LocalUserView, SiteView};
|
||||||
use lemmy_db_views_actor::structs::{CommunityFollowerView, CommunityModeratorView, PersonView};
|
use lemmy_db_views_actor::structs::{CommunityFollowerView, CommunityModeratorView, PersonView};
|
||||||
use lemmy_utils::{
|
use lemmy_utils::{
|
||||||
error::{LemmyError, LemmyErrorExt, LemmyErrorType, LemmyResult},
|
build_cache,
|
||||||
CACHE_DURATION_API,
|
error::{LemmyErrorExt, LemmyErrorType, LemmyResult},
|
||||||
|
CacheLock,
|
||||||
VERSION,
|
VERSION,
|
||||||
};
|
};
|
||||||
use moka::future::Cache;
|
|
||||||
use std::sync::LazyLock;
|
use std::sync::LazyLock;
|
||||||
|
|
||||||
#[tracing::instrument(skip(context))]
|
#[tracing::instrument(skip(context))]
|
||||||
|
@ -28,41 +28,10 @@ pub async fn get_site(
|
||||||
local_user_view: Option<LocalUserView>,
|
local_user_view: Option<LocalUserView>,
|
||||||
context: Data<LemmyContext>,
|
context: Data<LemmyContext>,
|
||||||
) -> LemmyResult<Json<GetSiteResponse>> {
|
) -> LemmyResult<Json<GetSiteResponse>> {
|
||||||
static CACHE: LazyLock<Cache<(), GetSiteResponse>> = LazyLock::new(|| {
|
|
||||||
Cache::builder()
|
|
||||||
.max_capacity(1)
|
|
||||||
.time_to_live(CACHE_DURATION_API)
|
|
||||||
.build()
|
|
||||||
});
|
|
||||||
|
|
||||||
// This data is independent from the user account so we can cache it across requests
|
// This data is independent from the user account so we can cache it across requests
|
||||||
|
static CACHE: CacheLock<GetSiteResponse> = LazyLock::new(build_cache);
|
||||||
let mut site_response = CACHE
|
let mut site_response = CACHE
|
||||||
.try_get_with::<_, LemmyError>((), async {
|
.try_get_with((), read_site(&context))
|
||||||
let site_view = SiteView::read_local(&mut context.pool()).await?;
|
|
||||||
let admins = PersonView::admins(&mut context.pool()).await?;
|
|
||||||
let all_languages = Language::read_all(&mut context.pool()).await?;
|
|
||||||
let discussion_languages = SiteLanguage::read_local_raw(&mut context.pool()).await?;
|
|
||||||
let blocked_urls = LocalSiteUrlBlocklist::get_all(&mut context.pool()).await?;
|
|
||||||
let tagline = Tagline::get_random(&mut context.pool()).await.ok();
|
|
||||||
let admin_oauth_providers = OAuthProvider::get_all(&mut context.pool()).await?;
|
|
||||||
let oauth_providers =
|
|
||||||
OAuthProvider::convert_providers_to_public(admin_oauth_providers.clone());
|
|
||||||
|
|
||||||
Ok(GetSiteResponse {
|
|
||||||
site_view,
|
|
||||||
admins,
|
|
||||||
version: VERSION.to_string(),
|
|
||||||
my_user: None,
|
|
||||||
all_languages,
|
|
||||||
discussion_languages,
|
|
||||||
blocked_urls,
|
|
||||||
tagline,
|
|
||||||
oauth_providers: Some(oauth_providers),
|
|
||||||
admin_oauth_providers: Some(admin_oauth_providers),
|
|
||||||
taglines: vec![],
|
|
||||||
custom_emojis: vec![],
|
|
||||||
})
|
|
||||||
})
|
|
||||||
.await
|
.await
|
||||||
.map_err(|e| anyhow::anyhow!("Failed to construct site response: {e}"))?;
|
.map_err(|e| anyhow::anyhow!("Failed to construct site response: {e}"))?;
|
||||||
|
|
||||||
|
@ -112,3 +81,29 @@ pub async fn get_site(
|
||||||
|
|
||||||
Ok(Json(site_response))
|
Ok(Json(site_response))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn read_site(context: &LemmyContext) -> LemmyResult<GetSiteResponse> {
|
||||||
|
let site_view = SiteView::read_local(&mut context.pool()).await?;
|
||||||
|
let admins = PersonView::admins(&mut context.pool()).await?;
|
||||||
|
let all_languages = Language::read_all(&mut context.pool()).await?;
|
||||||
|
let discussion_languages = SiteLanguage::read_local_raw(&mut context.pool()).await?;
|
||||||
|
let blocked_urls = LocalSiteUrlBlocklist::get_all(&mut context.pool()).await?;
|
||||||
|
let tagline = Tagline::get_random(&mut context.pool()).await.ok();
|
||||||
|
let admin_oauth_providers = OAuthProvider::get_all(&mut context.pool()).await?;
|
||||||
|
let oauth_providers = OAuthProvider::convert_providers_to_public(admin_oauth_providers.clone());
|
||||||
|
|
||||||
|
Ok(GetSiteResponse {
|
||||||
|
site_view,
|
||||||
|
admins,
|
||||||
|
version: VERSION.to_string(),
|
||||||
|
my_user: None,
|
||||||
|
all_languages,
|
||||||
|
discussion_languages,
|
||||||
|
blocked_urls,
|
||||||
|
tagline,
|
||||||
|
oauth_providers: Some(oauth_providers),
|
||||||
|
admin_oauth_providers: Some(admin_oauth_providers),
|
||||||
|
taglines: vec![],
|
||||||
|
custom_emojis: vec![],
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
|
@ -11,6 +11,7 @@ use lemmy_db_schema::{
|
||||||
};
|
};
|
||||||
use lemmy_utils::{
|
use lemmy_utils::{
|
||||||
error::{FederationError, LemmyError, LemmyErrorType, LemmyResult},
|
error::{FederationError, LemmyError, LemmyErrorType, LemmyResult},
|
||||||
|
CacheLock,
|
||||||
CACHE_DURATION_FEDERATION,
|
CACHE_DURATION_FEDERATION,
|
||||||
};
|
};
|
||||||
use moka::future::Cache;
|
use moka::future::Cache;
|
||||||
|
@ -139,7 +140,7 @@ pub(crate) async fn local_site_data_cached(
|
||||||
// multiple times. This causes a huge number of database reads if we hit the db directly. So we
|
// multiple times. This causes a huge number of database reads if we hit the db directly. So we
|
||||||
// cache these values for a short time, which will already make a huge difference and ensures that
|
// cache these values for a short time, which will already make a huge difference and ensures that
|
||||||
// changes take effect quickly.
|
// changes take effect quickly.
|
||||||
static CACHE: LazyLock<Cache<(), Arc<LocalSiteData>>> = LazyLock::new(|| {
|
static CACHE: CacheLock<Arc<LocalSiteData>> = LazyLock::new(|| {
|
||||||
Cache::builder()
|
Cache::builder()
|
||||||
.max_capacity(1)
|
.max_capacity(1)
|
||||||
.time_to_live(CACHE_DURATION_FEDERATION)
|
.time_to_live(CACHE_DURATION_FEDERATION)
|
||||||
|
|
|
@ -79,7 +79,6 @@ uuid = { workspace = true, features = ["v4"] }
|
||||||
i-love-jesus = { workspace = true, optional = true }
|
i-love-jesus = { workspace = true, optional = true }
|
||||||
anyhow = { workspace = true }
|
anyhow = { workspace = true }
|
||||||
diesel-bind-if-some = { workspace = true, optional = true }
|
diesel-bind-if-some = { workspace = true, optional = true }
|
||||||
moka.workspace = true
|
|
||||||
derive-new.workspace = true
|
derive-new.workspace = true
|
||||||
tuplex = { workspace = true, optional = true }
|
tuplex = { workspace = true, optional = true }
|
||||||
|
|
||||||
|
|
|
@ -5,8 +5,7 @@ use crate::{
|
||||||
};
|
};
|
||||||
use diesel::{dsl::insert_into, result::Error};
|
use diesel::{dsl::insert_into, result::Error};
|
||||||
use diesel_async::RunQueryDsl;
|
use diesel_async::RunQueryDsl;
|
||||||
use lemmy_utils::{error::LemmyResult, CACHE_DURATION_API};
|
use lemmy_utils::{build_cache, error::LemmyResult, CacheLock};
|
||||||
use moka::future::Cache;
|
|
||||||
use std::sync::LazyLock;
|
use std::sync::LazyLock;
|
||||||
|
|
||||||
impl LocalSite {
|
impl LocalSite {
|
||||||
|
@ -18,12 +17,7 @@ impl LocalSite {
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
pub async fn read(pool: &mut DbPool<'_>) -> LemmyResult<Self> {
|
pub async fn read(pool: &mut DbPool<'_>) -> LemmyResult<Self> {
|
||||||
static CACHE: LazyLock<Cache<(), LocalSite>> = LazyLock::new(|| {
|
static CACHE: CacheLock<LocalSite> = LazyLock::new(build_cache);
|
||||||
Cache::builder()
|
|
||||||
.max_capacity(1)
|
|
||||||
.time_to_live(CACHE_DURATION_API)
|
|
||||||
.build()
|
|
||||||
});
|
|
||||||
Ok(
|
Ok(
|
||||||
CACHE
|
CACHE
|
||||||
.try_get_with((), async {
|
.try_get_with((), async {
|
||||||
|
|
|
@ -23,30 +23,31 @@ workspace = true
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
full = [
|
full = [
|
||||||
"dep:ts-rs",
|
"ts-rs",
|
||||||
"dep:diesel",
|
"diesel",
|
||||||
"dep:rosetta-i18n",
|
"rosetta-i18n",
|
||||||
"dep:actix-web",
|
"actix-web",
|
||||||
"dep:reqwest-middleware",
|
"reqwest-middleware",
|
||||||
"dep:tracing",
|
"tracing",
|
||||||
"dep:actix-web",
|
"actix-web",
|
||||||
"dep:serde_json",
|
"serde_json",
|
||||||
"dep:anyhow",
|
"anyhow",
|
||||||
"dep:http",
|
"http",
|
||||||
"dep:deser-hjson",
|
"deser-hjson",
|
||||||
"dep:regex",
|
"regex",
|
||||||
"dep:urlencoding",
|
"urlencoding",
|
||||||
"dep:doku",
|
"doku",
|
||||||
"dep:url",
|
"url",
|
||||||
"dep:smart-default",
|
"smart-default",
|
||||||
"dep:enum-map",
|
"enum-map",
|
||||||
"dep:futures",
|
"futures",
|
||||||
"dep:tokio",
|
"tokio",
|
||||||
"dep:html2text",
|
"html2text",
|
||||||
"dep:lettre",
|
"lettre",
|
||||||
"dep:uuid",
|
"uuid",
|
||||||
"dep:itertools",
|
"itertools",
|
||||||
"dep:markdown-it",
|
"markdown-it",
|
||||||
|
"moka",
|
||||||
]
|
]
|
||||||
|
|
||||||
[package.metadata.cargo-shear]
|
[package.metadata.cargo-shear]
|
||||||
|
@ -89,6 +90,7 @@ markdown-it-block-spoiler = "1.0.0"
|
||||||
markdown-it-sub = "1.0.0"
|
markdown-it-sub = "1.0.0"
|
||||||
markdown-it-sup = "1.0.0"
|
markdown-it-sup = "1.0.0"
|
||||||
markdown-it-ruby = "1.0.0"
|
markdown-it-ruby = "1.0.0"
|
||||||
|
moka = { workspace = true, optional = true }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
pretty_assertions = { workspace = true }
|
pretty_assertions = { workspace = true }
|
||||||
|
|
|
@ -42,7 +42,10 @@ macro_rules! location_info {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "full")]
|
cfg_if! {
|
||||||
|
if #[cfg(feature = "full")] {
|
||||||
|
use moka::future::Cache;use std::fmt::Debug;use std::hash::Hash;
|
||||||
|
|
||||||
/// tokio::spawn, but accepts a future that may fail and also
|
/// tokio::spawn, but accepts a future that may fail and also
|
||||||
/// * logs errors
|
/// * logs errors
|
||||||
/// * attaches the spawned task to the tracing span of the caller for better logging
|
/// * attaches the spawned task to the tracing span of the caller for better logging
|
||||||
|
@ -60,3 +63,20 @@ pub fn spawn_try_task(
|
||||||
* spawn was called */
|
* spawn was called */
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn build_cache<K, V>() -> Cache<K, V>
|
||||||
|
where
|
||||||
|
K: Debug + Eq + Hash + Send + Sync + 'static,
|
||||||
|
V: Debug + Clone + Send + Sync + 'static,
|
||||||
|
{
|
||||||
|
Cache::<K, V>::builder()
|
||||||
|
.max_capacity(1)
|
||||||
|
.time_to_live(CACHE_DURATION_API)
|
||||||
|
.build()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "full")]
|
||||||
|
pub type CacheLock<T> = std::sync::LazyLock<Cache<(), T>>;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue