game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
proton_cache.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// ==============================================================================
3// proton_cache.h
4// ==============================================================================
5// Cached Proton version scanning with automatic invalidation
6// ==============================================================================
7// This class provides a caching layer for Proton version detection to eliminate
8// expensive filesystem scans on every UI interaction. Features:
9// - Thread-safe caching with mutex protection
10// - Time-based cache invalidation (5 minutes by default)
11// - Manual cache invalidation when needed
12// - Transparent replacement for direct SteamLibraryScanner calls
13// ==============================================================================
14
15#pragma once
16
17#include <chrono>
18#include <filesystem>
19#include <mutex>
20#include <string>
21#include <vector>
22
23/**
24 * @class ProtonCache
25 * @brief Thread-safe cache for Proton version scanning results
26 *
27 * Caches the results of expensive Proton version scans to avoid repeated
28 * filesystem traversal. Cache automatically invalidates after a configurable
29 * time period or can be manually invalidated.
30 *
31 * ## Thread Safety
32 * All methods are thread-safe via mutex protection.
33 *
34 * ## Cache Invalidation
35 * Cache invalidates automatically after PROTON_CACHE_VALIDITY_SEC seconds.
36 * Can also be manually invalidated via invalidate().
37 *
38 * ## Usage Example
39 * ```cpp
40 * // Get cached Proton versions (scans filesystem only if cache expired)
41 * auto versions = ProtonCache::getVersions();
42 *
43 * // Manual invalidation (e.g., after installing new Proton)
44 * ProtonCache::invalidate();
45 * ```
46 */
48{
49 public:
50 // ==========================================================================
51 // Cache Access
52 // ==========================================================================
53
54 /**
55 * @brief Get cached Proton versions
56 * @return Vector of (name, path) pairs for all Proton versions
57 *
58 * Returns cached results if cache is valid, otherwise rescans filesystem
59 * and updates cache. Thread-safe.
60 */
61 static std::vector<std::pair<std::string, std::filesystem::path>> getVersions();
62
63 /**
64 * @brief Get sorted Proton version names only
65 * @return Vector of Proton version names sorted by priority
66 *
67 * Convenience method that returns just the names in sorted order:
68 * 1. CachyOS versions first
69 * 2. Experimental versions next
70 * 3. Others by version number
71 */
72 static std::vector<std::string> getSortedVersionNames();
73
74 // ==========================================================================
75 // Cache Management
76 // ==========================================================================
77
78 /**
79 * @brief Invalidate the cache
80 *
81 * Forces a rescan on next getVersions() call. Use this when:
82 * - New Proton version installed
83 * - Proton version removed
84 * - Steam library paths changed
85 */
86 static void invalidate();
87
88 /**
89 * @brief Check if cache is currently valid
90 * @return True if cache exists and hasn't expired
91 */
92 static bool isValid();
93
94 /**
95 * @brief Get cache age in seconds
96 * @return Number of seconds since cache was last updated
97 */
98 static int getCacheAge();
99
100 private:
101 // ==========================================================================
102 // Internal State
103 // ==========================================================================
104
105 /// Cached Proton versions (name, path pairs)
106 static std::vector<std::pair<std::string, std::filesystem::path>> cachedVersions_;
107
108 /// Time when cache was last updated
109 static std::chrono::system_clock::time_point cacheTime_;
110
111 /// Mutex for thread-safe cache access
112 static std::mutex cacheMutex_;
113
114 /// Flag indicating if cache has been initialized
115 static bool cacheInitialized_;
116
117 // ==========================================================================
118 // Internal Helpers
119 // ==========================================================================
120
121 /**
122 * @brief Update cache by scanning filesystem
123 *
124 * Called automatically when cache is invalid. Should only be called
125 * while holding cacheMutex_.
126 */
127 static void updateCache();
128
129 /**
130 * @brief Check if cache has expired
131 * @return True if cache needs refresh
132 *
133 * Should only be called while holding cacheMutex_.
134 */
135 static bool isCacheExpired();
136};
Thread-safe cache for Proton version scanning results.
static int getCacheAge()
Get cache age in seconds.
static std::vector< std::string > getSortedVersionNames()
Get sorted Proton version names only.
static std::vector< std::pair< std::string, std::filesystem::path > > getVersions()
Get cached Proton versions.
static void invalidate()
Invalidate the cache.
static bool isValid()
Check if cache is currently valid.