Skip to content

Commit a862a75

Browse files
committed
verify file driver cache deletion and update README
1 parent df3b2c4 commit a862a75

File tree

3 files changed

+118
-3
lines changed

3 files changed

+118
-3
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ The following tests need to be implemented to fully validate the new `KeyAwareFi
231231
- [ ] Test complete cache workflow (store → retrieve → delete)
232232
- [ ] Test multiple keys with different expiration times
233233
- [ ] Test cache key listing with `getAllKeys()` method
234-
- [ ] Test cache key deletion with `forgetKey()` method
234+
- [x] Test cache key deletion with `forgetKey()` method
235235
- [ ] Test mixed wrapped and legacy data scenarios
236236
- [ ] Test performance with large numbers of cache keys
237237
@@ -244,7 +244,7 @@ The following tests need to be implemented to fully validate the new `KeyAwareFi
244244
245245
### CacheUiLaravel Integration Tests
246246
- [ ] Test `getAllKeys()` method with `key-aware-file` driver
247-
- [ ] Test `forgetKey()` method with `key-aware-file` driver
247+
- [x] Test `forgetKey()` method with `key-aware-file` driver
248248
- [ ] Test mixed driver scenarios (Redis + File + Database)
249249
- [ ] Test error handling and graceful degradation
250250

src/CacheUiLaravel.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,29 @@ public function getAllKeys(?string $store = null): array
3333
*/
3434
public function forgetKey(string $key, ?string $store = null): bool
3535
{
36+
$storeName = $store ?? config('cache.default');
3637
$cacheStore = $store !== null && $store !== '' && $store !== '0' ? Cache::store($store) : Cache::store();
3738

38-
return $cacheStore->forget($key);
39+
if ($cacheStore->forget($key)) {
40+
return true;
41+
}
42+
43+
// Handle file driver specific logic for hashed keys
44+
$driver = config("cache.stores.{$storeName}.driver");
45+
46+
if (in_array($driver, ['file', 'key-aware-file']) && preg_match('/^[a-f0-9]{40}$/', $key)) {
47+
// Use the path from the specific store configuration, fallback to default
48+
$cachePath = config("cache.stores.{$storeName}.path", config('cache.stores.file.path', storage_path('framework/cache/data')));
49+
50+
$parts = array_slice(str_split($key, 2), 0, 2);
51+
$path = $cachePath . '/' . implode('/', $parts) . '/' . $key;
52+
53+
if (File::exists($path)) {
54+
return File::delete($path);
55+
}
56+
}
57+
58+
return false;
3959
}
4060

4161
private function getRedisKeys(string $store): array
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Abr4xas\CacheUiLaravel\CacheUiLaravel;
6+
use Illuminate\Support\Facades\Cache;
7+
use Illuminate\Support\Facades\Config;
8+
use Illuminate\Support\Facades\File;
9+
10+
describe('CacheUiLaravel File Driver Deletion', function (): void {
11+
beforeEach(function (): void {
12+
$this->cacheUiLaravel = new CacheUiLaravel();
13+
});
14+
15+
it('deletes hashed file when standard forget fails for file driver', function (): void {
16+
// Mock Cache::forget to return false, simulating that it couldn't find the key by name
17+
Cache::shouldReceive('store')->withNoArgs()->andReturnSelf();
18+
Cache::shouldReceive('forget')->with('008cb7ea48f292dd8b03d361a4c9f66085f77090')->andReturn(false);
19+
20+
// Configure file driver
21+
Config::set('cache.default', 'file');
22+
Config::set('cache.stores.file.driver', 'file');
23+
$cachePath = storage_path('framework/cache/data');
24+
Config::set('cache.stores.file.path', $cachePath);
25+
26+
// The key is a SHA1 hash
27+
$key = '008cb7ea48f292dd8b03d361a4c9f66085f77090';
28+
29+
// Expected file path reconstruction
30+
// 00/8c/008cb7ea48f292dd8b03d361a4c9f66085f77090
31+
$expectedPath = $cachePath . '/00/8c/' . $key;
32+
33+
// Mock File existence and deletion
34+
File::shouldReceive('exists')->with($expectedPath)->andReturn(true);
35+
File::shouldReceive('delete')->with($expectedPath)->andReturn(true);
36+
37+
$result = $this->cacheUiLaravel->forgetKey($key);
38+
39+
expect($result)->toBeTrue();
40+
});
41+
42+
it('deletes hashed file when standard forget fails for key-aware-file driver', function (): void {
43+
// Mock Cache::forget to return false
44+
Cache::shouldReceive('store')->withNoArgs()->andReturnSelf();
45+
Cache::shouldReceive('forget')->with('008cb7ea48f292dd8b03d361a4c9f66085f77090')->andReturn(false);
46+
47+
// Configure key-aware-file driver
48+
Config::set('cache.default', 'file');
49+
Config::set('cache.stores.file.driver', 'key-aware-file');
50+
$cachePath = storage_path('framework/cache/data');
51+
Config::set('cache.stores.file.path', $cachePath);
52+
53+
$key = '008cb7ea48f292dd8b03d361a4c9f66085f77090';
54+
$expectedPath = $cachePath . '/00/8c/' . $key;
55+
56+
File::shouldReceive('exists')->with($expectedPath)->andReturn(true);
57+
File::shouldReceive('delete')->with($expectedPath)->andReturn(true);
58+
59+
$result = $this->cacheUiLaravel->forgetKey($key);
60+
61+
expect($result)->toBeTrue();
62+
});
63+
64+
it('does not attempt file deletion for non-hashed keys', function (): void {
65+
Cache::shouldReceive('store')->withNoArgs()->andReturnSelf();
66+
Cache::shouldReceive('forget')->with('not-a-hash')->andReturn(false);
67+
68+
Config::set('cache.default', 'file');
69+
Config::set('cache.stores.file.driver', 'file');
70+
71+
// File::exists/delete should NOT be called
72+
File::shouldReceive('exists')->never();
73+
File::shouldReceive('delete')->never();
74+
75+
$result = $this->cacheUiLaravel->forgetKey('not-a-hash');
76+
77+
expect($result)->toBeFalse();
78+
});
79+
80+
it('does not attempt file deletion for non-file drivers', function (): void {
81+
Cache::shouldReceive('store')->withNoArgs()->andReturnSelf();
82+
Cache::shouldReceive('forget')->with('008cb7ea48f292dd8b03d361a4c9f66085f77090')->andReturn(false);
83+
84+
Config::set('cache.default', 'redis');
85+
Config::set('cache.stores.redis.driver', 'redis');
86+
87+
// File::exists/delete should NOT be called
88+
File::shouldReceive('exists')->never();
89+
File::shouldReceive('delete')->never();
90+
91+
$result = $this->cacheUiLaravel->forgetKey('008cb7ea48f292dd8b03d361a4c9f66085f77090');
92+
93+
expect($result)->toBeFalse();
94+
});
95+
});

0 commit comments

Comments
 (0)