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, 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> + WithMods + Send + Sync,
22{
23    async fn clear_cache(&self) {
24        match self.loader() {
25            #[cfg(feature = "vanilla")]
26            Loader::Vanilla => {
27                lighty_loaders::vanilla::VANILLA
28                    .invalidate(self.name())
29                    .await
30            }
31            #[cfg(feature = "fabric")]
32            Loader::Fabric => {
33                lighty_loaders::fabric::FABRIC
34                    .invalidate(self.name())
35                    .await
36            }
37            #[cfg(feature = "quilt")]
38            Loader::Quilt => {
39                lighty_loaders::quilt::QUILT
40                    .invalidate(self.name())
41                    .await
42            }
43            #[cfg(feature = "forge")]
44            Loader::Forge => {
45                lighty_loaders::forge::FORGE
46                    .invalidate(self.name())
47                    .await
48            }
49            #[cfg(feature = "neoforge")]
50            Loader::NeoForge => {
51                lighty_loaders::neoforge::NEOFORGE
52                    .invalidate(self.name())
53                    .await
54            }
55            #[cfg(feature = "lighty_updater")]
56            Loader::LightyUpdater => {
57                lighty_loaders::lighty_updater::LIGHTY_UPDATER
58                    .invalidate(self.name())
59                    .await
60            }
61            _ => {}
62        }
63
64        #[cfg(feature = "modrinth")]
65        {
66            let mc = self.minecraft_version().to_string();
67            let loader = self.loader().clone();
68            crate::modrinth::MODRINTH_CACHE
69                .retain(move |k| !(k.mc == mc && k.loader == loader))
70                .await;
71        }
72        #[cfg(feature = "curseforge")]
73        {
74            let mc = self.minecraft_version().to_string();
75            let loader = self.loader().clone();
76            crate::curseforge::CURSEFORGE_CACHE
77                .retain(move |k| !(k.mc == mc && k.loader == loader))
78                .await;
79        }
80    }
81}