Bearsampp 2026.7.11
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 179 of file class.cachemanager.php.

179 : int
180 {
181 if (!self::$enabled || !self::$cacheDir) {
182 return 0;
183 }
184
185 // Clear in-memory caches of related classes
186 if (class_exists('Module')) {
188 }
189
190 // Clear general cache if it exists
191 if (class_exists('Cache')) {
192 $cache = new Cache();
193 $cache->clear();
194 }
195
196 $files = @glob(self::$cacheDir . '/*.cache');
197 $deleted = 0;
198
199 if (is_array($files)) {
200 foreach ($files as $file) {
201 if (@unlink($file)) {
202 $deleted++;
203 }
204 }
205 }
206
207 return $deleted;
208 }
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 147 of file class.cachemanager.php.

147 : string
148 {
149 return self::$cacheDir . '/' . $cacheKey . '.cache';
150 }

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 216 of file class.cachemanager.php.

216 : array
217 {
218 $files = @glob(self::$cacheDir . '/*.cache');
219 $totalSize = 0;
220
221 if (is_array($files)) {
222 foreach ($files as $file) {
223 $totalSize += filesize($file);
224 }
225 }
226
227 return [
228 'enabled' => self::$enabled,
229 'cacheDir' => self::$cacheDir,
230 'filesCount' => count($files ?? []),
231 'totalSize' => $totalSize,
232 'cacheHits' => self::$stats['hits'],
233 'cacheMisses' => self::$stats['misses'],
234 'cachewrites' => self::$stats['writes'],
235 'hitRate' => (self::$stats['hits'] + self::$stats['misses']) > 0
236 ? round((self::$stats['hits'] / (self::$stats['hits'] + self::$stats['misses'])) * 100, 2)
237 : 0
238 ];
239 }

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 36 of file class.cachemanager.php.

36 : void
37 {
38 self::$cacheDir = $cacheDir;
39
40 // Create cache directory if needed
41 if (!is_dir(self::$cacheDir)) {
42 @mkdir(self::$cacheDir, 0755, true);
43 }
44
45 // Check if cache directory is writable
46 if (!is_writable(self::$cacheDir)) {
47 Log::warning('Cache directory not writable, caching disabled: ' . self::$cacheDir);
48 self::$enabled = false;
49 }
50 }
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
bool Success status

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

159 : void
160 {
161 if (!self::$enabled || !self::$cacheDir) {
162 return;
163 }
164
165 $cacheKey = md5($sourcePath);
166 $cacheFile = self::getCacheFile($cacheKey);
167
168 if (file_exists($cacheFile)) {
169 @unlink($cacheFile);
170 }
171 }
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 103 of file class.cachemanager.php.

106 : bool {
107 if (!file_exists($cacheFile) || !file_exists($sourcePath)) {
108 return false;
109 }
110
111 // Cache valid only if newer than source
112 return filemtime($cacheFile) > filemtime($sourcePath);
113 }

References if.

◆ isEnabled()

isEnabled ( )
static

Check if caching is enabled

Returns
bool True if caching is enabled

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

258 : bool
259 {
260 return self::$enabled;
261 }

◆ 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 61 of file class.cachemanager.php.

65 {
66 if (!self::$enabled || !self::$cacheDir) {
67 return call_user_func($parser, $sourcePath);
68 }
69
70 if ($cacheKey === null) {
71 $cacheKey = md5($sourcePath);
72 }
73
74 $cacheFile = self::getCacheFile($cacheKey);
75
76 // Try cache first (warm start optimization)
77 if (self::isCacheValid($sourcePath, $cacheFile)) {
78 $cached = @json_decode(file_get_contents($cacheFile), true);
79 if (is_array($cached)) {
80 self::$stats['hits']++;
81 return $cached;
82 }
83 }
84
85 // Cache miss or invalid - parse and save
86 self::$stats['misses']++;
87 $data = call_user_func($parser, $sourcePath);
88
89 if (is_array($data)) {
90 self::writeCache($cacheFile, $data);
91 }
92
93 return $data;
94 }
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 248 of file class.cachemanager.php.

248 : void
249 {
250 self::$enabled = $enable;
251 }

◆ 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 123 of file class.cachemanager.php.

123 : bool
124 {
125 if (!self::$enabled) {
126 return false;
127 }
128
129 $json = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
130 $written = @file_put_contents($cacheFile, $json);
131
132 if ($written !== false) {
133 self::$stats['writes']++;
134 return true;
135 }
136
137 Log::warning('Failed to write cache: ' . $cacheFile);
138 return false;
139 }

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 19 of file class.cachemanager.php.

Referenced by init().

◆ $enabled

$enabled = true
staticprivate

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

◆ $stats

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

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

◆ CACHE_VERSION

const CACHE_VERSION = '1.0'

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


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