Skip to content

Commit 13e83a0

Browse files
committed
refactor: Improve cache key generation and file handling in cache functions
Signed-off-by: Michele Palazzi <sysdadmin@m1k.cloud>
1 parent dfcdd61 commit 13e83a0

1 file changed

Lines changed: 8 additions & 6 deletions

File tree

src/cache.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function getCacheKey(string $user, array $options = []): string
2424
// Normalize options
2525
ksort($options);
2626
$optionsString = json_encode($options);
27-
return md5($user . $optionsString);
27+
return hash('sha256', $user . $optionsString);
2828
}
2929

3030
/**
@@ -71,11 +71,13 @@ function getCachedStats(string $user, array $options = [], int $maxAge = CACHE_D
7171
$fileAge = time() - filemtime($filePath);
7272
if ($fileAge > $maxAge) {
7373
// Cache expired, delete the file
74-
@unlink($filePath);
74+
if (file_exists($filePath)) {
75+
unlink($filePath);
76+
}
7577
return null;
7678
}
7779

78-
$contents = @file_get_contents($filePath);
80+
$contents = file_get_contents($filePath);
7981
if ($contents === false) {
8082
return null;
8183
}
@@ -135,7 +137,7 @@ function clearExpiredCache(int $maxAge = CACHE_DURATION): int
135137
foreach ($files as $file) {
136138
$fileAge = time() - filemtime($file);
137139
if ($fileAge > $maxAge) {
138-
if (@unlink($file)) {
140+
if (file_exists($file) && unlink($file)) {
139141
$deleted++;
140142
}
141143
}
@@ -156,13 +158,13 @@ function clearUserCache(string $user): bool
156158
return true;
157159
}
158160

159-
// Since we use md5 hash, we need to check all files
161+
// Since we use a hash, we need to check all files
160162
// For simplicity, just clear the cache with empty options
161163
$key = getCacheKey($user, []);
162164
$filePath = getCacheFilePath($key);
163165

164166
if (file_exists($filePath)) {
165-
return @unlink($filePath);
167+
return unlink($filePath);
166168
}
167169

168170
return true;

0 commit comments

Comments
 (0)