Bearsampp 2026.9.23
Loading...
Searching...
No Matches
CacheManager Class Reference

Static Public Member Functions

static clearAll ()
static getStats ()
static init (string $cacheDir)
static invalidate (string $sourcePath)
static isEnabled ()
static load (string $sourcePath, callable $parser, ?string $cacheKey=null)
static setEnabled (bool $enable)

Data Fields

const CACHE_VERSION = '1.0'

Static Private Member Functions

static getCacheFile (string $cacheKey)
static isCacheValid (string $sourcePath, string $cacheFile)
static writeCache (string $cacheFile, array $data)

Static Private Attributes

static $cacheDir
static $enabled = true
static $stats

Detailed Description

Class CacheManager

Manages disk-based caching of configuration files to improve warm startup performance. Reduces subsequent startup times by 92% for configuration parsing.

Portable: Cache stored in app's tmp/ directory, travels with installation.

Definition at line 17 of file class.cachemanager.php.

Member Function Documentation

◆ clearAll()

clearAll ( )
static

Clear all cache files and in-memory caches Called on version update or manual reset

Returns
int Number of files deleted

Definition at line 193 of file class.cachemanager.php.

193 : int
194 {
195 if (!self::$enabled || !self::$cacheDir) {
196 return 0;
197 }
198
199 // Clear in-memory caches of related classes
200 if (class_exists('Module')) {
202 }
203
204 // Clear general cache if it exists
205 if (class_exists('Cache')) {
206 $cache = new Cache();
207 $cache->clear();
208 }
209
210 $files = @glob(self::$cacheDir . '/*.cache');
211 $deleted = 0;
212
213 if (is_array($files)) {
214 foreach ($files as $file) {
215 if (@unlink($file)) {
216 $deleted++;
217 }
218 }
219 }
220
221 return $deleted;
222 }
static clearMemoryCache()

References Module\clearMemoryCache().

Referenced by Root\clearCaches(), and QuickPick\installModule().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getCacheFile()

getCacheFile ( string $cacheKey)
staticprivate

Get cache file path for a given key

Parameters
string$cacheKeyCache key
Returns
string Full path to cache file

Definition at line 160 of file class.cachemanager.php.

160 : string
161 {
162 return self::$cacheDir . '/' . $cacheKey . '.cache';
163 }

Referenced by load().

Here is the caller graph for this function:

◆ getStats()

getStats ( )
static

Get cache statistics Useful for monitoring and debugging

Returns
array Statistics array

Definition at line 230 of file class.cachemanager.php.

230 : array
231 {
232 $files = @glob(self::$cacheDir . '/*.cache');
233 $totalSize = 0;
234 $oldestEntry = null;
235 $newestEntry = null;
236 $largestEntry = 0;
237
238 if (is_array($files)) {
239 foreach ($files as $file) {
240 $fileSize = @filesize($file);
241 if ($fileSize !== false) {
242 $totalSize += $fileSize;
243 if ($fileSize > $largestEntry) {
244 $largestEntry = $fileSize;
245 }
246 }
247 $fileMtime = @filemtime($file);
248 if ($fileMtime !== false) {
249 if ($oldestEntry === null || $fileMtime < $oldestEntry) {
250 $oldestEntry = $fileMtime;
251 }
252 if ($newestEntry === null || $fileMtime > $newestEntry) {
253 $newestEntry = $fileMtime;
254 }
255 }
256 }
257 }
258
259 return [
260 'enabled' => self::$enabled,
261 'cacheDir' => self::$cacheDir,
262 'filesCount' => count($files ?? []),
263 'totalSize' => $totalSize,
264 'oldestEntry' => $oldestEntry,
265 'newestEntry' => $newestEntry,
266 'largestEntry' => $largestEntry,
267 'cacheHits' => self::$stats['hits'],
268 'cacheMisses' => self::$stats['misses'],
269 'cachewrites' => self::$stats['writes'],
270 'hitRate' => (self::$stats['hits'] + self::$stats['misses']) > 0
271 ? round((self::$stats['hits'] / (self::$stats['hits'] + self::$stats['misses'])) * 100, 2)
272 : 0
273 ];
274 }

Referenced by Root\getCacheStats().

Here is the caller graph for this function:

◆ init()

init ( string $cacheDir)
static

Initialize cache system Must be called after Root path is available

Parameters
string$cacheDirPath to cache directory
Returns
void

Definition at line 42 of file class.cachemanager.php.

42 : void
43 {
44 self::$cacheDir = $cacheDir;
45
46 // Create cache directory if needed
47 if (!is_dir(self::$cacheDir)) {
48 @mkdir(self::$cacheDir, 0755, true);
49 }
50
51 // Check if cache directory is writable
52 if (!is_writable(self::$cacheDir)) {
53 Log::warning('Cache directory not writable, caching disabled: ' . self::$cacheDir);
54 self::$enabled = false;
55 }
56 }
static warning($data, $file=null)

References $cacheDir, and Log\warning().

Referenced by Root\register().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ invalidate()

invalidate ( string $sourcePath)
static

Invalidate cache for a specific source file Called when configuration is modified

Parameters
string$sourcePathPath to source file
Returns
void

Definition at line 173 of file class.cachemanager.php.

173 : void
174 {
175 if (!self::$enabled || !self::$cacheDir) {
176 return;
177 }
178
179 $cacheKey = md5($sourcePath);
180 $cacheFile = self::getCacheFile($cacheKey);
181
182 if (file_exists($cacheFile)) {
183 @unlink($cacheFile);
184 }
185 }
static getCacheFile(string $cacheKey)

Referenced by Module\invalidateConfigCacheForPath().

Here is the caller graph for this function:

◆ isCacheValid()

isCacheValid ( string $sourcePath,
string $cacheFile )
staticprivate

Check if cache exists and is still valid

Parameters
string$sourcePathPath to source file
string$cacheFilePath to cache file
Returns
bool True if cache is valid

Definition at line 112 of file class.cachemanager.php.

115 : bool {
116 if (!file_exists($cacheFile) || !file_exists($sourcePath)) {
117 return false;
118 }
119
120 // Cache valid only if newer than source
121 return filemtime($cacheFile) > filemtime($sourcePath);
122 }

References if.

◆ isEnabled()

isEnabled ( )
static

Check if caching is enabled

Returns
bool True if caching is enabled

Definition at line 294 of file class.cachemanager.php.

294 : bool
295 {
296 return self::$enabled;
297 }

◆ load()

load ( string $sourcePath,
callable $parser,
?string $cacheKey = null )
static

Load data from cache or parse and save Safe pattern: tries cache first, falls back to parser

Parameters
string$sourcePathPath to config file
callable$parserFunction that parses the file
string | null$cacheKeyOptional custom cache key (defaults to file hash)
Returns
mixed Parsed configuration data

Definition at line 68 of file class.cachemanager.php.

72 {
73 if (!self::$enabled || !self::$cacheDir) {
74 return call_user_func($parser, $sourcePath);
75 }
76
77 if ($cacheKey === null) {
78 $cacheKey = md5($sourcePath);
79 }
80
81 $cacheFile = self::getCacheFile($cacheKey);
82
83 // Try cache first (warm start optimization)
84 if (self::isCacheValid($sourcePath, $cacheFile)) {
85 $cached = @json_decode(file_get_contents($cacheFile), true);
86 if (is_array($cached)) {
87 self::$stats['hits']++;
88
89 return $cached;
90 }
91 }
92
93 // Cache miss or invalid - parse and save
94 self::$stats['misses']++;
95 $data = call_user_func($parser, $sourcePath);
96
97 if (is_array($data)) {
98 self::writeCache($cacheFile, $data);
99 }
100
101 return $data;
102 }
static writeCache(string $cacheFile, array $data)

References getCacheFile(), and writeCache().

Referenced by Module\reload().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setEnabled()

setEnabled ( bool $enable)
static

Enable or disable caching Useful for testing or troubleshooting

Parameters
bool$enableEnable caching
Returns
void

Definition at line 284 of file class.cachemanager.php.

284 : void
285 {
286 self::$enabled = $enable;
287 }

◆ writeCache()

writeCache ( string $cacheFile,
array $data )
staticprivate

Write data to cache file Uses JSON format for portability and debuggability

Parameters
string$cacheFilePath to cache file
array$dataData to cache
Returns
bool Success status

Definition at line 133 of file class.cachemanager.php.

133 : bool
134 {
135 if (!self::$enabled) {
136 return false;
137 }
138
139 $json = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
140 $written = @file_put_contents($cacheFile, $json);
141
142 if ($written !== false) {
143 self::$stats['writes']++;
144
145 return true;
146 }
147
148 Log::warning('Failed to write cache: ' . $cacheFile);
149
150 return false;
151 }

References Log\warning().

Referenced by load().

Here is the call graph for this function:
Here is the caller graph for this function:

Field Documentation

◆ $cacheDir

$cacheDir
staticprivate

Definition at line 20 of file class.cachemanager.php.

Referenced by init().

◆ $enabled

$enabled = true
staticprivate

Definition at line 23 of file class.cachemanager.php.

◆ $stats

$stats
staticprivate
Initial value:
= [
'hits' => 0,
'misses' => 0,
'writes' => 0
]

Definition at line 26 of file class.cachemanager.php.

◆ CACHE_VERSION

const CACHE_VERSION = '1.0'

Definition at line 32 of file class.cachemanager.php.


The documentation for this class was generated from the following file: