Skip to content

Commit ed22358

Browse files
committed
Melhora a estratégia de cache de arquivos com tratamento de erros e logs para operações de leitura e escrita
1 parent 98d6864 commit ed22358

File tree

1 file changed

+80
-15
lines changed

1 file changed

+80
-15
lines changed

src/Cache/FileCacheStrategy.php

Lines changed: 80 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace PivotPHP\Routing\Cache;
66

77
use PivotPHP\Routing\Contracts\FileCacheInterface;
8+
use RuntimeException;
89

910
/**
1011
* File-based Cache Strategy
@@ -72,9 +73,21 @@ public function get(string $key): mixed
7273
return null;
7374
}
7475

75-
$data = @include $filePath;
76+
if (!is_readable($filePath)) {
77+
error_log("Cache file not readable: {$filePath}");
78+
$this->stats['misses']++;
79+
return null;
80+
}
7681

77-
if ($data === false) {
82+
try {
83+
$data = include $filePath;
84+
if ($data === false) {
85+
error_log("Failed to load cache file: {$filePath}");
86+
$this->stats['misses']++;
87+
return null;
88+
}
89+
} catch (\Throwable $e) {
90+
error_log("Error loading cache file {$filePath}: " . $e->getMessage());
7891
$this->stats['misses']++;
7992
return null;
8093
}
@@ -92,7 +105,11 @@ public function set(string $key, mixed $value, ?int $ttl = null): void
92105
$data = var_export($value, true);
93106
$content = "<?php\n\nreturn {$data};\n";
94107

95-
file_put_contents($filePath, $content, LOCK_EX);
108+
$bytesWritten = @file_put_contents($filePath, $content, LOCK_EX);
109+
if ($bytesWritten === false) {
110+
error_log("Failed to write cache file: {$filePath}. Check directory permissions.");
111+
throw new RuntimeException("Failed to write cache file: {$filePath}");
112+
}
96113
$this->stats['writes']++;
97114
}
98115

@@ -112,7 +129,9 @@ public function delete(string $key): void
112129
$filePath = $this->getCacheFilePath($key);
113130

114131
if (file_exists($filePath)) {
115-
@unlink($filePath);
132+
if (!@unlink($filePath)) {
133+
error_log("Failed to delete cache file: {$filePath}");
134+
}
116135
}
117136
}
118137

@@ -121,14 +140,22 @@ public function delete(string $key): void
121140
*/
122141
public function clear(): void
123142
{
124-
$files = glob($this->cacheDirectory . '/*.php');
143+
$files = @glob($this->cacheDirectory . '/*.php');
125144

126145
if ($files === false) {
146+
error_log("Failed to list cache files in: {$this->cacheDirectory}");
127147
return;
128148
}
129149

150+
$failures = [];
130151
foreach ($files as $file) {
131-
@unlink($file);
152+
if (!@unlink($file)) {
153+
$failures[] = $file;
154+
}
155+
}
156+
157+
if (!empty($failures)) {
158+
error_log("Failed to delete cache files: " . implode(', ', $failures));
132159
}
133160

134161
$this->stats = ['hits' => 0, 'misses' => 0, 'writes' => 0];
@@ -211,7 +238,11 @@ public function getCacheTime(string $key): ?int
211238
}
212239

213240
$mtime = @filemtime($filePath);
214-
return $mtime !== false ? $mtime : null;
241+
if ($mtime === false) {
242+
error_log("Cannot get file modification time: {$filePath}");
243+
return null;
244+
}
245+
return $mtime;
215246
}
216247

217248
/**
@@ -231,7 +262,11 @@ public function writeRoutesCache(array $routes): void
231262
$data = var_export($routes, true);
232263
$content = "<?php\n\n// Generated route cache\n// " . date('Y-m-d H:i:s') . "\n\nreturn {$data};\n";
233264

234-
file_put_contents($filePath, $content, LOCK_EX);
265+
$bytesWritten = @file_put_contents($filePath, $content, LOCK_EX);
266+
if ($bytesWritten === false) {
267+
error_log("Failed to write routes cache file: {$filePath}. Check directory permissions.");
268+
throw new RuntimeException("Failed to write routes cache file: {$filePath}");
269+
}
235270
$this->stats['writes']++;
236271
}
237272

@@ -246,8 +281,18 @@ public function readRoutesCache(): ?array
246281
return null;
247282
}
248283

249-
$data = @include $filePath;
250-
return is_array($data) ? $data : null;
284+
if (!is_readable($filePath)) {
285+
error_log("Routes cache file not readable: {$filePath}");
286+
return null;
287+
}
288+
289+
try {
290+
$data = include $filePath;
291+
return is_array($data) ? $data : null;
292+
} catch (\Throwable $e) {
293+
error_log("Error loading routes cache {$filePath}: " . $e->getMessage());
294+
return null;
295+
}
251296
}
252297

253298
/**
@@ -273,10 +318,16 @@ public function setCacheDirectory(string $directory): void
273318
public function ensureCacheDirectoryExists(): bool
274319
{
275320
if (!is_dir($this->cacheDirectory)) {
276-
return @mkdir($this->cacheDirectory, 0755, true) !== false;
321+
if (!@mkdir($this->cacheDirectory, 0755, true) && !is_dir($this->cacheDirectory)) {
322+
throw new RuntimeException("Failed to create cache directory: {$this->cacheDirectory}. Check permissions and disk space.");
323+
}
277324
}
278325

279-
return is_writable($this->cacheDirectory);
326+
if (!is_writable($this->cacheDirectory)) {
327+
throw new RuntimeException("Cache directory is not writable: {$this->cacheDirectory}");
328+
}
329+
330+
return true;
280331
}
281332

282333
/**
@@ -295,8 +346,17 @@ private function loadPatternsCache(): array
295346
return [];
296347
}
297348

298-
$data = @include $filePath;
299-
return is_array($data) ? $data : [];
349+
if (!is_readable($filePath)) {
350+
return [];
351+
}
352+
353+
try {
354+
$data = include $filePath;
355+
return is_array($data) ? $data : [];
356+
} catch (\Throwable $e) {
357+
error_log("Error loading patterns cache {$filePath}: " . $e->getMessage());
358+
return [];
359+
}
300360
}
301361

302362
/**
@@ -311,6 +371,11 @@ private function savePatternsCache(array $patterns): void
311371
$data = var_export($patterns, true);
312372
$content = "<?php\n\n// Generated patterns cache\n// " . date('Y-m-d H:i:s') . "\n\nreturn {$data};\n";
313373

314-
file_put_contents($filePath, $content, LOCK_EX);
374+
$bytesWritten = @file_put_contents($filePath, $content, LOCK_EX);
375+
if ($bytesWritten === false) {
376+
error_log("Failed to write patterns cache file: {$filePath}. Check directory permissions.");
377+
throw new RuntimeException("Failed to write patterns cache file: {$filePath}");
378+
}
315379
}
316380
}
381+

0 commit comments

Comments
 (0)