Skip to main content

lighty_modsloader/
instance_cache.rs

1// Copyright (c) 2025 Hamadi
2// Licensed under the MIT License
3
4//! `instance.clear_cache().await` — purges every cache entry tied to an
5//! instance in a single call.
6
7use lighty_loaders::types::{Loader, LoaderExtensions, VersionInfo};
8
9use crate::with_mods::WithMods;
10
11/// Cache invalidation surface for a launcher instance.
12pub trait InstanceCache {
13    /// Invalidates the loader manifest cache plus the Modrinth and
14    /// CurseForge mod caches scoped to `(minecraft_version, loader)`.
15    /// Idempotent.
16    fn clear_cache(&self) -> impl std::future::Future<Output = ()> + Send;
17}
18
19impl<T> InstanceCache for T
20where
21    T: VersionInfo<LoaderType = Loader> + LoaderExtensions + WithMods + Send + Sync,
22{
23    async fn clear_cache(&self) {
24        self.invalidate_cache().await;
25
26        #[cfg(feature = "modrinth")]
27        {
28            let mc = self.minecraft_version().to_string();
29            let loader = self.loader().clone();
30            crate::modrinth::MODRINTH_CACHE
31                .retain(move |k| !(k.mc == mc && k.loader == loader))
32                .await;
33        }
34        #[cfg(feature = "curseforge")]
35        {
36            let mc = self.minecraft_version().to_string();
37            let loader = self.loader().clone();
38            crate::curseforge::CURSEFORGE_CACHE
39                .retain(move |k| !(k.mc == mc && k.loader == loader))
40                .await;
41        }
42    }
43}