@@ -247,6 +247,161 @@ Event::listen(OptimizationApplied::class, fn($e) =>
247247
248248** Events:** CacheHit, CacheMissed, KeyWritten, KeyForgotten, OptimizationApplied
249249
250+ ### 🔐 Encryption Strategy
251+
252+ Encrypt sensitive cached data automatically:
253+
254+ ``` php
255+ // Enable in config
256+ config(['smart-cache.encryption.enabled' => true]);
257+ config(['smart-cache.encryption.keys' => ['user_*', 'payment_*']]);
258+
259+ // Sensitive data is automatically encrypted
260+ SmartCache::put('user_123_ssn', $sensitiveData, 3600);
261+ // Data encrypted at rest, decrypted on retrieval
262+ ```
263+
264+ ** Benefit:** Secure sensitive data in cache without code changes
265+
266+ ### 🏷️ Cache Namespacing
267+
268+ Group and manage cache keys by namespace:
269+
270+ ``` php
271+ // Set namespace for operations
272+ SmartCache::namespace('users')->put('profile', $data, 3600);
273+ SmartCache::namespace('users')->put('settings', $settings, 3600);
274+
275+ // Flush entire namespace
276+ SmartCache::flushNamespace('users'); // Clears all user:* keys
277+
278+ // Get all keys in namespace
279+ $keys = SmartCache::getNamespaceKeys('users');
280+ ```
281+
282+ ** Benefit:** Organize cache keys, easy bulk invalidation
283+
284+ ### ⏱️ TTL Jitter
285+
286+ Prevent thundering herd with randomized TTL:
287+
288+ ``` php
289+ // Add 10% jitter to TTL
290+ SmartCache::withJitter(0.1)->put('popular_data', $data, 3600);
291+ // Actual TTL: 3240-3960 seconds (±10%)
292+
293+ // Or use dedicated methods
294+ SmartCache::putWithJitter('key', $value, 3600, 0.15); // 15% jitter
295+ SmartCache::rememberWithJitter('key', 3600, 0.1, fn() => expensiveCall());
296+ ```
297+
298+ ** Benefit:** Prevents cache stampede when many keys expire simultaneously
299+
300+ ### 🔌 Circuit Breaker
301+
302+ Auto-fallback when cache backend fails:
303+
304+ ``` php
305+ // Check if cache is available
306+ if (SmartCache::isAvailable()) {
307+ $data = SmartCache::get('key');
308+ }
309+
310+ // Execute with automatic fallback
311+ $data = SmartCache::withFallback(
312+ fn() => SmartCache::get('key'), // Primary
313+ fn() => Database::query('SELECT ...') // Fallback
314+ );
315+
316+ // Get circuit breaker stats
317+ $stats = SmartCache::getCircuitBreakerStats();
318+ // Returns: state, failure_count, success_count, last_failure_at
319+ ```
320+
321+ ** States:** Closed (normal) → Open (failing) → Half-Open (testing)
322+
323+ ### 🚦 Rate Limiting & Stampede Protection
324+
325+ Prevent cache stampede with rate limiting:
326+
327+ ``` php
328+ // Throttle cache operations
329+ $result = SmartCache::throttle('api_call', 10, 60, function() {
330+ return expensiveApiCall();
331+ }); // Max 10 calls per 60 seconds
332+
333+ // Remember with stampede protection (XFetch algorithm)
334+ $data = SmartCache::rememberWithStampedeProtection('key', 3600, function() {
335+ return expensiveComputation();
336+ });
337+ ```
338+
339+ ** Benefit:** Prevents multiple processes from regenerating cache simultaneously
340+
341+ ### 🔥 Cache Warming
342+
343+ Pre-warm cache with artisan command:
344+
345+ ``` bash
346+ # Warm cache using registered warmers
347+ php artisan smart-cache:warm
348+
349+ # Warm specific warmer
350+ php artisan smart-cache:warm --warmer=ProductCacheWarmer
351+ ```
352+
353+ Register warmers in your service provider:
354+
355+ ``` php
356+ use SmartCache\Contracts\CacheWarmer;
357+
358+ class ProductCacheWarmer implements CacheWarmer
359+ {
360+ public function warm(): void
361+ {
362+ $products = Product::all();
363+ SmartCache::put('all_products', $products, 3600);
364+ }
365+
366+ public function getKey(): string
367+ {
368+ return 'products';
369+ }
370+ }
371+
372+ // Register in AppServiceProvider
373+ $this->app->tag([ProductCacheWarmer::class], 'smart-cache.warmers');
374+ ```
375+
376+ ### 🧹 Orphan Chunk Cleanup
377+
378+ Automatically clean up orphan chunks:
379+
380+ ``` bash
381+ # Clean up orphan chunks
382+ php artisan smart-cache:cleanup-chunks
383+
384+ # Dry run (show what would be cleaned)
385+ php artisan smart-cache:cleanup-chunks --dry-run
386+ ```
387+
388+ ### 📊 Cache Statistics Dashboard
389+
390+ View cache statistics via web interface:
391+
392+ ``` php
393+ // Enable in config
394+ config(['smart-cache.dashboard.enabled' => true]);
395+ config(['smart-cache.dashboard.prefix' => 'smart-cache']);
396+ config(['smart-cache.dashboard.middleware' => ['web', 'auth']]);
397+ ```
398+
399+ ** Routes:**
400+ - ` GET /smart-cache/dashboard ` - HTML dashboard
401+ - ` GET /smart-cache/statistics ` - JSON statistics
402+ - ` GET /smart-cache/health ` - Health check
403+ - ` GET /smart-cache/keys ` - Managed keys list
404+
250405## 🌊 Modern Patterns (Laravel 12+)
251406
252407### SWR (Stale-While-Revalidate)
@@ -375,6 +530,41 @@ return [
375530 'enabled' => true,
376531 'metrics_ttl' => 3600,
377532 ],
533+
534+ // Encryption for sensitive data
535+ 'encryption' => [
536+ 'enabled' => false,
537+ 'keys' => [], // Keys to encrypt: ['user_*', 'payment_*']
538+ 'patterns' => [], // Regex patterns: ['/secret_.*/']
539+ ],
540+
541+ // Circuit breaker for cache backend failures
542+ 'circuit_breaker' => [
543+ 'enabled' => true,
544+ 'failure_threshold' => 5, // Failures before opening
545+ 'success_threshold' => 2, // Successes to close
546+ 'timeout' => 30, // Seconds before half-open
547+ ],
548+
549+ // Rate limiting for cache operations
550+ 'rate_limiter' => [
551+ 'enabled' => true,
552+ 'default_limit' => 100, // Max operations per window
553+ 'window' => 60, // Window in seconds
554+ ],
555+
556+ // TTL jitter to prevent thundering herd
557+ 'jitter' => [
558+ 'enabled' => false,
559+ 'percentage' => 0.1, // 10% jitter by default
560+ ],
561+
562+ // Statistics dashboard
563+ 'dashboard' => [
564+ 'enabled' => false,
565+ 'prefix' => 'smart-cache',
566+ 'middleware' => ['web'],
567+ ],
378568];
379569```
380570
0 commit comments