Skip to content

Commit 70cc7c0

Browse files
committed
add more docs
1 parent 18cb24e commit 70cc7c0

File tree

1 file changed

+148
-11
lines changed

1 file changed

+148
-11
lines changed

README.md

Lines changed: 148 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@ return [
3939

4040
// Number of visible items in scroll
4141
'search_scroll' => env('CACHE_UI_SEARCH_SCROLL', 15),
42+
43+
// Maximum number of keys to retrieve (null = unlimited)
44+
'keys_limit' => env('CACHE_UI_KEYS_LIMIT', null),
45+
46+
// Enable error logging for cache operations
47+
'enable_logging' => env('CACHE_UI_ENABLE_LOGGING', false),
48+
49+
// Timeout in seconds for cache operations (0 = no timeout)
50+
'operation_timeout' => env('CACHE_UI_OPERATION_TIMEOUT', 0),
4251
];
4352
```
4453

@@ -48,6 +57,9 @@ You can also configure these values in your `.env` file:
4857
CACHE_UI_DEFAULT_STORE=redis
4958
CACHE_UI_PREVIEW_LIMIT=150
5059
CACHE_UI_SEARCH_SCROLL=20
60+
CACHE_UI_KEYS_LIMIT=1000
61+
CACHE_UI_ENABLE_LOGGING=true
62+
CACHE_UI_OPERATION_TIMEOUT=30
5163
```
5264

5365
### Custom File Cache Driver (Only for File Store)
@@ -56,7 +68,8 @@ If you are using the `file` cache driver (default in Laravel), you should use ou
5668

5769
**Why?** The standard Laravel `file` driver stores keys as hashes, making them unreadable. This custom driver wraps the value to store the real key, allowing you to see and search for them.
5870

59-
> **Important**: This is **NOT** needed for Redis or Database drivers, as they support listing keys natively.
71+
> [!IMPORTANT]
72+
> This is **NOT** needed for Redis or Database drivers, as they support listing keys natively.
6073
6174
#### Driver Configuration
6275

@@ -122,6 +135,7 @@ class AppServiceProvider extends ServiceProvider
122135
-**Full compatibility**: Works exactly like the standard `file` driver
123136
-**Better experience**: Enables more intuitive cache key search and management
124137
-**Backward compatibility**: Existing cache files continue to work
138+
-**Complete API**: Implements all Laravel cache methods (`put`, `get`, `add`, `forever`, `increment`, `decrement`, `remember`, `rememberForever`, `pull`, `has`, `flush`)
125139

126140
#### Migration from Standard File Driver
127141

@@ -157,6 +171,11 @@ php artisan cache:list --store=redis
157171
- 📋 **List all keys**: View all available keys in your cache
158172
- 🗑️ **Selective deletion**: Delete individual keys without affecting the rest of the cache
159173
- 🔌 **Multiple drivers**: Supports Redis, File and Database
174+
-**Performance optimized**: Uses SCAN for Redis (safe for production) and supports key limits
175+
- 📊 **Additional info**: View cache value, size, expiration, and type
176+
- 📤 **Export functionality**: Export key lists to files
177+
- 🔎 **Pattern filtering**: Filter keys using regex patterns
178+
- 🛡️ **Error handling**: Comprehensive error handling with optional logging
160179

161180
### Supported Drivers
162181

@@ -168,7 +187,8 @@ php artisan cache:list --store=redis
168187
| **Array** | ⚠️ No | Not supported (doesn't persist) |
169188
| **Memcached** | ⚠️ No | Not currently supported |
170189

171-
> **Note**: The `key-aware-file` driver is **only** needed if you use the `file` cache driver. If you use Redis or Database, you don't need to change your driver configuration.
190+
> [!WARNING]
191+
> The `key-aware-file` driver is **only** needed if you use the `file` cache driver. If you use Redis or Database, you don't need to change your driver configuration.
172192
173193
### Usage Example
174194

@@ -205,13 +225,22 @@ php artisan cache:list --filter="/^user_/"
205225
# Show additional information (size, type, expiration)
206226
php artisan cache:list --info
207227

208-
# Limit number of keys displayed
228+
# Limit number of keys displayed (useful for large caches)
209229
php artisan cache:list --limit=50
210230

211231
# Combine multiple options
212232
php artisan cache:list --store=redis --show-value --info --limit=100
213233
```
214234
235+
#### Option Details
236+
237+
- **`--store=`**: Specify which cache store to use (defaults to Laravel's default cache store)
238+
- **`--show-value`**: Display the cache value before confirming deletion
239+
- **`--export=`**: Export the list of keys to a file (one key per line)
240+
- **`--filter=`**: Filter keys using a regex pattern (e.g., `/^user_/` matches keys starting with "user_")
241+
- **`--info`**: Show additional information about each key (size, expiration time, data type)
242+
- **`--limit=`**: Limit the number of keys to retrieve and display (helps with performance on large caches)
243+
215244
### Programmatic Usage
216245
217246
You can also use the `CacheUiLaravel` class directly in your code:
@@ -233,6 +262,11 @@ $deleted = CacheUiLaravel::forgetKey('user_1_profile');
233262
234263
// Delete a key from a specific store
235264
$deleted = CacheUiLaravel::forgetKey('session_data', 'redis');
265+
266+
// The methods include validation:
267+
// - Empty or invalid store names will use the default store
268+
// - Empty keys will return false
269+
// - Non-existent stores will throw an exception (if logging is enabled, errors are logged)
236270
```
237271
238272
### Advanced Use Cases
@@ -258,7 +292,8 @@ foreach ($userKeys as $key) {
258292
use Abr4xas\CacheUiLaravel\Facades\CacheUiLaravel;
259293
use Illuminate\Support\Facades\Cache;
260294
261-
$keys = CacheUiLaravel::getAllKeys('redis', 1000); // Limit to 1000 for performance
295+
// Use limit parameter for better performance on large caches
296+
$keys = CacheUiLaravel::getAllKeys('redis', 1000);
262297
$totalSize = 0;
263298
264299
foreach ($keys as $key) {
@@ -271,6 +306,24 @@ foreach ($keys as $key) {
271306
echo "Total cache size: " . number_format($totalSize / 1024 / 1024, 2) . " MB";
272307
```
273308
309+
#### Error Handling and Logging
310+
311+
The package includes comprehensive error handling. You can enable logging to track cache operation errors:
312+
313+
```php
314+
// In config/cache-ui-laravel.php or .env
315+
'enable_logging' => true,
316+
317+
// Errors will be logged to Laravel's log file
318+
// Example log entry:
319+
// [2024-01-01 12:00:00] local.WARNING: Cache UI: Failed to retrieve keys from store 'redis': Connection timeout
320+
```
321+
322+
When logging is enabled, the following operations will log errors:
323+
- Key retrieval failures
324+
- Key deletion failures
325+
- Driver-specific errors (Redis connection, file system, database)
326+
274327
#### Cache Key Analysis
275328
276329
```php
@@ -288,12 +341,85 @@ arsort($patterns);
288341
print_r($patterns); // Shows key distribution by prefix
289342
```
290343
344+
#### Performance Optimization
345+
346+
For large caches, always use the `limit` parameter to improve performance:
347+
348+
```php
349+
use Abr4xas\CacheUiLaravel\Facades\CacheUiLaravel;
350+
351+
// Get first 100 keys (useful for pagination)
352+
$firstBatch = CacheUiLaravel::getAllKeys('redis', 100);
353+
354+
// Process in batches
355+
$offset = 0;
356+
$limit = 100;
357+
do {
358+
$keys = CacheUiLaravel::getAllKeys('redis', $limit);
359+
// Process keys...
360+
$offset += $limit;
361+
} while (count($keys) === $limit);
362+
```
363+
364+
**Note**: The package automatically uses `SCAN` for Redis instead of `KEYS *`, which is safer for production environments as it doesn't block the Redis server.
365+
291366
## Testing
292367
368+
Run the test suite:
369+
293370
```bash
371+
# Run all tests
294372
composer test:unit
373+
374+
# Run specific test file
375+
vendor/bin/pest tests/Unit/CacheUiLaravelMethodsTest.php
376+
377+
# Run with coverage
378+
vendor/bin/pest --coverage
295379
```
296380
381+
The package includes comprehensive test coverage:
382+
- **Unit tests**: Individual component testing
383+
- **Integration tests**: End-to-end workflow testing
384+
- **Edge case tests**: Error handling and boundary conditions
385+
386+
## Technical Details
387+
388+
### Performance Optimizations
389+
390+
The package includes several performance optimizations:
391+
392+
1. **Redis SCAN**: Uses `SCAN` instead of `KEYS *` to prevent blocking Redis in production
393+
2. **Key Limits**: Optional limit parameters for all drivers to avoid loading all keys at once
394+
3. **Early Termination**: File driver stops reading files once the limit is reached
395+
4. **Efficient Queries**: Database driver uses optimized queries with limits
396+
397+
### Error Handling
398+
399+
The package includes comprehensive error handling:
400+
401+
- **Validation**: Input validation for store names and cache keys
402+
- **Graceful Degradation**: Falls back to alternative methods when primary methods fail
403+
- **Logging**: Optional error logging via Laravel's Log facade
404+
- **Exception Handling**: Catches and handles driver-specific exceptions
405+
406+
### KeyAwareFileStore Methods
407+
408+
The `key-aware-file` driver implements all standard Laravel cache methods:
409+
410+
- `put($key, $value, $seconds)` - Store an item with expiration
411+
- `get($key, $default = null)` - Retrieve an item
412+
- `add($key, $value, $seconds)` - Store an item only if it doesn't exist
413+
- `forever($key, $value)` - Store an item permanently
414+
- `increment($key, $value = 1)` - Increment a numeric value
415+
- `decrement($key, $value = 1)` - Decrement a numeric value
416+
- `remember($key, $ttl, $callback)` - Get or store a value
417+
- `rememberForever($key, $callback)` - Get or store a value permanently
418+
- `pull($key, $default = null)` - Get and delete an item
419+
- `has($key)` - Check if an item exists
420+
- `forget($key)` - Delete an item
421+
- `flush()` - Clear all items
422+
297423
## TODO
298424
299425
The following tests and improvements are planned or in progress:
@@ -347,14 +473,25 @@ The following tests and improvements are planned or in progress:
347473
348474
The package provides several configuration options in `config/cache-ui-laravel.php`:
349475
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)
476+
| Option | Type | Default | Description |
477+
|--------|------|---------|-------------|
478+
| `default_store` | string\|null | `null` | Default cache store to use when running the command |
479+
| `preview_limit` | int | `100` | Maximum characters to display in value preview |
480+
| `search_scroll` | int | `15` | Number of visible items in search menu |
481+
| `keys_limit` | int\|null | `null` | Maximum number of keys to retrieve (null = unlimited) |
482+
| `enable_logging` | bool | `false` | Enable error logging for cache operations |
483+
| `operation_timeout` | int | `0` | Timeout in seconds for cache operations (0 = no timeout) |
484+
485+
### Performance Considerations
486+
487+
- **Redis**: The package uses `SCAN` instead of `KEYS *` for safer key retrieval in production environments
488+
- **File Driver**: Supports optional `limit` parameter to avoid reading all files in large cache directories
489+
- **Database Driver**: Supports optional `limit` parameter to avoid loading all cache records at once
490+
- **Recommended**: Always use the `--limit` option or `keys_limit` config for large caches to improve performance
491+
492+
### Environment Variables
356493
357-
You can also configure these via environment variables:
494+
You can configure these options via environment variables:
358495
359496
```env
360497
CACHE_UI_DEFAULT_STORE=redis

0 commit comments

Comments
 (0)