Skip to content

Commit 18cb24e

Browse files
committed
feat: major improvements and comprehensive test coverage
- Refactor to eliminate code duplication - Add missing KeyAwareFileStore methods - Optimize Redis/file/database key retrieval - Add error handling, logging, and validation - Add comprehensive test suite (integration, edge cases, unit tests) - Fix test infrastructure and linting issues - Update documentation and remove Memcached support
1 parent df3b2c4 commit 18cb24e

15 files changed

+1623
-346
lines changed

README.md

Lines changed: 134 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ use Abr4xas\CacheUiLaravel\KeyAwareFileStore;
8989
use Illuminate\Support\Facades\Cache;
9090
use Illuminate\Support\ServiceProvider;
9191
use Illuminate\Foundation\Application;
92+
use Illuminate\Filesystem\Filesystem;
9293

9394
class AppServiceProvider extends ServiceProvider
9495
{
@@ -107,7 +108,7 @@ class AppServiceProvider extends ServiceProvider
107108
{
108109
// Register the custom file cache driver
109110
Cache::extend('key-aware-file', fn (Application $app, array $config) => Cache::repository(new KeyAwareFileStore(
110-
$app['files'],
111+
$app->make(Filesystem::class),
111112
$config['path'],
112113
$config['file_permission'] ?? null
113114
)));
@@ -187,7 +188,31 @@ Are you sure you want to delete this cache key? › No / Yes
187188
🗑️ The key 'user_1_profile' has been successfully deleted
188189
```
189190
190-
### Programmatic Usage (optional)
191+
### Advanced Command Options
192+
193+
The command supports several useful options:
194+
195+
```bash
196+
# Show cache value before deletion
197+
php artisan cache:list --show-value
198+
199+
# Export keys list to a file
200+
php artisan cache:list --export=keys.txt
201+
202+
# Filter keys by regex pattern
203+
php artisan cache:list --filter="/^user_/"
204+
205+
# Show additional information (size, type, expiration)
206+
php artisan cache:list --info
207+
208+
# Limit number of keys displayed
209+
php artisan cache:list --limit=50
210+
211+
# Combine multiple options
212+
php artisan cache:list --store=redis --show-value --info --limit=100
213+
```
214+
215+
### Programmatic Usage
191216
192217
You can also use the `CacheUiLaravel` class directly in your code:
193218
@@ -200,13 +225,69 @@ $keys = CacheUiLaravel::getAllKeys();
200225
// Get all cache keys from a specific store
201226
$redisKeys = CacheUiLaravel::getAllKeys('redis');
202227

228+
// Get limited number of keys (useful for large caches)
229+
$limitedKeys = CacheUiLaravel::getAllKeys('redis', 100);
230+
203231
// Delete a specific key from default store
204232
$deleted = CacheUiLaravel::forgetKey('user_1_profile');
205233

206234
// Delete a key from a specific store
207235
$deleted = CacheUiLaravel::forgetKey('session_data', 'redis');
208236
```
209237
238+
### Advanced Use Cases
239+
240+
#### Batch Operations
241+
242+
```php
243+
use Abr4xas\CacheUiLaravel\Facades\CacheUiLaravel;
244+
245+
// Get all keys matching a pattern
246+
$allKeys = CacheUiLaravel::getAllKeys('redis');
247+
$userKeys = array_filter($allKeys, fn($key) => str_starts_with($key, 'user_'));
248+
249+
// Delete multiple keys
250+
foreach ($userKeys as $key) {
251+
CacheUiLaravel::forgetKey($key, 'redis');
252+
}
253+
```
254+
255+
#### Monitoring Cache Size
256+
257+
```php
258+
use Abr4xas\CacheUiLaravel\Facades\CacheUiLaravel;
259+
use Illuminate\Support\Facades\Cache;
260+
261+
$keys = CacheUiLaravel::getAllKeys('redis', 1000); // Limit to 1000 for performance
262+
$totalSize = 0;
263+
264+
foreach ($keys as $key) {
265+
$value = Cache::get($key);
266+
if ($value !== null) {
267+
$totalSize += strlen(serialize($value));
268+
}
269+
}
270+
271+
echo "Total cache size: " . number_format($totalSize / 1024 / 1024, 2) . " MB";
272+
```
273+
274+
#### Cache Key Analysis
275+
276+
```php
277+
use Abr4xas\CacheUiLaravel\Facades\CacheUiLaravel;
278+
279+
$keys = CacheUiLaravel::getAllKeys('redis');
280+
$patterns = [];
281+
282+
foreach ($keys as $key) {
283+
$prefix = explode('_', $key)[0] ?? 'unknown';
284+
$patterns[$prefix] = ($patterns[$prefix] ?? 0) + 1;
285+
}
286+
287+
arsort($patterns);
288+
print_r($patterns); // Shows key distribution by prefix
289+
```
290+
210291
## Testing
211292
212293
```bash
@@ -215,25 +296,28 @@ composer test:unit
215296
216297
## TODO
217298
218-
The following tests need to be implemented to fully validate the new `KeyAwareFileStore` functionality:
299+
The following tests and improvements are planned or in progress:
219300
220301
### Unit Tests for KeyAwareFileStore
221-
- [ ] Test `put()` method with various data types (string, integer, array, boolean, null)
222-
- [ ] Test `get()` method with wrapped and unwrapped data formats
223-
- [ ] Test `add()` method behavior and return values
224-
- [ ] Test `forever()` method with zero expiration
225-
- [ ] Test `increment()` method with numeric values
226-
- [ ] Test backward compatibility with legacy cache files
227-
- [ ] Test error handling for corrupted cache files
228-
- [ ] Test file permissions and directory creation
302+
- [x] Test `put()` method with various data types (string, integer, array, boolean, null)
303+
- [x] Test `get()` method with wrapped and unwrapped data formats
304+
- [x] Test `add()` method behavior and return values
305+
- [x] Test `forever()` method with zero expiration
306+
- [x] Test `increment()` method with numeric values
307+
- [x] Test `decrement()` method with numeric values
308+
- [x] Test backward compatibility with legacy cache files
309+
- [x] Test error handling for corrupted cache files
310+
- [x] Test file permissions and directory creation
311+
- [x] Test `remember()` and `rememberForever()` methods
312+
- [x] Test `pull()`, `has()`, and `flush()` methods
229313
230314
### Integration Tests
231-
- [ ] Test complete cache workflow (store → retrieve → delete)
232-
- [ ] Test multiple keys with different expiration times
233-
- [ ] Test cache key listing with `getAllKeys()` method
234-
- [ ] Test cache key deletion with `forgetKey()` method
235-
- [ ] Test mixed wrapped and legacy data scenarios
236-
- [ ] Test performance with large numbers of cache keys
315+
- [x] Test complete cache workflow (store → retrieve → delete)
316+
- [x] Test multiple keys with different expiration times
317+
- [x] Test cache key listing with `getAllKeys()` method
318+
- [x] Test cache key deletion with `forgetKey()` method
319+
- [x] Test mixed wrapped and legacy data scenarios
320+
- [x] Test performance with large numbers of cache keys
237321
238322
### Driver Registration Tests
239323
- [ ] Test custom driver registration in `AppServiceProvider`
@@ -243,17 +327,43 @@ The following tests need to be implemented to fully validate the new `KeyAwareFi
243327
- [ ] Test error handling for invalid paths and permissions
244328
245329
### CacheUiLaravel Integration Tests
246-
- [ ] Test `getAllKeys()` method with `key-aware-file` driver
247-
- [ ] Test `forgetKey()` method with `key-aware-file` driver
248-
- [ ] Test mixed driver scenarios (Redis + File + Database)
249-
- [ ] Test error handling and graceful degradation
330+
- [x] Test `getAllKeys()` method with `key-aware-file` driver
331+
- [x] Test `forgetKey()` method with `key-aware-file` driver
332+
- [x] Test mixed driver scenarios (Redis + File + Database)
333+
- [x] Test error handling and graceful degradation
250334
251335
### Edge Cases and Error Handling
336+
- [x] Test with invalid serialized data
337+
- [x] Test with very large cache values
338+
- [x] Test with special characters in cache keys
252339
- [ ] Test with read-only file systems
253340
- [ ] Test with insufficient disk space
254-
- [ ] Test with invalid serialized data
255-
- [ ] Test with very large cache values
256-
- [ ] Test with special characters in cache keys
341+
342+
### Performance Tests
343+
- [ ] Test benchmark for operations with many keys
344+
- [ ] Test load scenarios to validate optimizations
345+
346+
## Configuration Options
347+
348+
The package provides several configuration options in `config/cache-ui-laravel.php`:
349+
350+
- `default_store`: Default cache store to use
351+
- `preview_limit`: Maximum characters to display in value preview (default: 100)
352+
- `search_scroll`: Number of visible items in search menu (default: 15)
353+
- `keys_limit`: Maximum number of keys to retrieve (null = unlimited)
354+
- `enable_logging`: Enable error logging for cache operations (default: false)
355+
- `operation_timeout`: Timeout in seconds for cache operations (0 = no timeout)
356+
357+
You can also configure these via environment variables:
358+
359+
```env
360+
CACHE_UI_DEFAULT_STORE=redis
361+
CACHE_UI_PREVIEW_LIMIT=150
362+
CACHE_UI_SEARCH_SCROLL=20
363+
CACHE_UI_KEYS_LIMIT=1000
364+
CACHE_UI_ENABLE_LOGGING=true
365+
CACHE_UI_OPERATION_TIMEOUT=30
366+
```
257367
258368
## Changelog
259369

config/cache-ui-laravel.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,38 @@
5555

5656
'search_scroll' => env('CACHE_UI_SEARCH_SCROLL', 15),
5757

58+
/*
59+
|--------------------------------------------------------------------------
60+
| Keys Limit
61+
|--------------------------------------------------------------------------
62+
|
63+
| Maximum number of keys to retrieve when listing cache keys.
64+
| Set to null for unlimited (not recommended for large caches).
65+
|
66+
*/
67+
68+
'keys_limit' => env('CACHE_UI_KEYS_LIMIT', null),
69+
70+
/*
71+
|--------------------------------------------------------------------------
72+
| Enable Logging
73+
|--------------------------------------------------------------------------
74+
|
75+
| Enable error logging for cache operations.
76+
|
77+
*/
78+
79+
'enable_logging' => env('CACHE_UI_ENABLE_LOGGING', false),
80+
81+
/*
82+
|--------------------------------------------------------------------------
83+
| Operation Timeout
84+
|--------------------------------------------------------------------------
85+
|
86+
| Timeout in seconds for cache operations (0 = no timeout).
87+
|
88+
*/
89+
90+
'operation_timeout' => env('CACHE_UI_OPERATION_TIMEOUT', 0),
91+
5892
];

phpunit.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<testsuites>
88
<testsuite name="Unit">
99
<directory>tests/Unit</directory>
10+
<directory>tests/Integration</directory>
1011
</testsuite>
1112
</testsuites>
1213
<source>

rector.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,5 @@
1919
typeDeclarations: true,
2020
privatization: true,
2121
earlyReturn: true,
22-
strictBooleans: true,
2322
)
2423
->withPhpSets();

0 commit comments

Comments
 (0)