-pub(crate) async fn fetch_local_site_data(
- pool: &DbPool,
-) -> Result<LocalSiteData, diesel::result::Error> {
- // LocalSite may be missing
- let local_site = LocalSite::read(pool).await.ok();
- let allowed_instances = Instance::allowlist(pool).await?;
- let blocked_instances = Instance::blocklist(pool).await?;
-
- Ok(LocalSiteData {
- local_site,
- allowed_instances,
- blocked_instances,
- })
+pub(crate) async fn local_site_data_cached(
+ pool: &mut DbPool<'_>,
+) -> LemmyResult<Arc<LocalSiteData>> {
+ static CACHE: Lazy<Cache<(), Arc<LocalSiteData>>> = Lazy::new(|| {
+ Cache::builder()
+ .max_capacity(1)
+ .time_to_live(BLOCKLIST_CACHE_DURATION)
+ .build()
+ });
+ Ok(
+ CACHE
+ .try_get_with((), async {
+ let (local_site, allowed_instances, blocked_instances) =
+ lemmy_db_schema::try_join_with_pool!(pool => (
+ // LocalSite may be missing
+ |pool| async {
+ Ok(LocalSite::read(pool).await.ok())
+ },
+ Instance::allowlist,
+ Instance::blocklist
+ ))?;
+
+ Ok::<_, diesel::result::Error>(Arc::new(LocalSiteData {
+ local_site,
+ allowed_instances,
+ blocked_instances,
+ }))
+ })
+ .await?,
+ )