@@ -256,6 +256,9 @@ public function clear(): bool
256256 {
257257 $ success = true ;
258258
259+ // Clean up expired keys first
260+ $ this ->cleanupExpiredManagedKeys ();
261+
259262 // Get all tracked keys
260263 $ keys = $ this ->getManagedKeys ();
261264
@@ -405,6 +408,62 @@ public function getManagedKeys(): array
405408 return $ this ->managedKeys ;
406409 }
407410
411+ /**
412+ * Clean up expired keys from managed keys tracking.
413+ *
414+ * @return int Number of expired keys removed
415+ */
416+ public function cleanupExpiredManagedKeys (): int
417+ {
418+ $ cleaned = 0 ;
419+ $ validKeys = [];
420+
421+ foreach ($ this ->managedKeys as $ key ) {
422+ if ($ this ->has ($ key )) {
423+ $ validKeys [] = $ key ;
424+ } else {
425+ $ cleaned ++;
426+ }
427+ }
428+
429+ if ($ cleaned > 0 ) {
430+ $ this ->managedKeys = $ validKeys ;
431+ $ this ->cache ->forever ('_sc_managed_keys ' , $ this ->managedKeys );
432+ }
433+
434+ return $ cleaned ;
435+ }
436+
437+ /**
438+ * Check if a specific feature is available.
439+ *
440+ * @param string $feature The feature name to check
441+ * @return bool
442+ */
443+ public function hasFeature (string $ feature ): bool
444+ {
445+ $ features = [
446+ 'tags ' => method_exists ($ this , 'tags ' ),
447+ 'flushTags ' => method_exists ($ this , 'flushTags ' ),
448+ 'dependsOn ' => method_exists ($ this , 'dependsOn ' ),
449+ 'invalidate ' => method_exists ($ this , 'invalidate ' ),
450+ 'flushPatterns ' => method_exists ($ this , 'flushPatterns ' ),
451+ 'invalidateModel ' => method_exists ($ this , 'invalidateModel ' ),
452+ 'swr ' => method_exists ($ this , 'swr ' ),
453+ 'stale ' => method_exists ($ this , 'stale ' ),
454+ 'refreshAhead ' => method_exists ($ this , 'refreshAhead ' ),
455+ 'flexible ' => method_exists ($ this , 'flexible ' ),
456+ 'getStatistics ' => method_exists ($ this , 'getStatistics ' ),
457+ 'healthCheck ' => method_exists ($ this , 'healthCheck ' ),
458+ 'getPerformanceMetrics ' => method_exists ($ this , 'getPerformanceMetrics ' ),
459+ 'analyzePerformance ' => method_exists ($ this , 'analyzePerformance ' ),
460+ 'getAvailableCommands ' => method_exists ($ this , 'getAvailableCommands ' ),
461+ 'executeCommand ' => method_exists ($ this , 'executeCommand ' ),
462+ ];
463+
464+ return $ features [$ feature ] ?? false ;
465+ }
466+
408467 /**
409468 * Determines the cache driver from the store instance.
410469 *
@@ -882,13 +941,30 @@ protected function executeClearCommand(array $parameters = []): array
882941 ];
883942 }
884943
944+ // Clean up expired keys first
945+ $ expiredCleaned = $ this ->cleanupExpiredManagedKeys ();
946+ $ keys = $ this ->getManagedKeys ();
947+ $ actualCount = count ($ keys );
948+
949+ if ($ actualCount === 0 ) {
950+ return [
951+ 'success ' => true ,
952+ 'message ' => "All {$ count } managed keys were expired and have been cleaned up. " ,
953+ 'cleared_count ' => $ count ,
954+ 'expired_cleaned ' => $ expiredCleaned ,
955+ 'total_managed_keys ' => $ count
956+ ];
957+ }
958+
885959 $ success = $ this ->clear ();
886960
887961 return [
888962 'success ' => $ success ,
889- 'message ' => $ success ? "Cleared {$ count } SmartCache managed keys. " : 'Some keys could not be cleared. ' ,
890- 'cleared_count ' => $ success ? $ count : 0 ,
891- 'total_managed_keys ' => $ count
963+ 'message ' => $ success ? "Cleared {$ actualCount } SmartCache managed keys. " : 'Some keys could not be cleared. ' ,
964+ 'cleared_count ' => $ success ? $ actualCount : 0 ,
965+ 'expired_cleaned ' => $ expiredCleaned ,
966+ 'total_managed_keys ' => $ count ,
967+ 'active_keys_cleared ' => $ actualCount
892968 ];
893969 }
894970 }
@@ -1091,34 +1167,42 @@ public function analyzePerformance(): array
10911167 $ efficiency = $ metrics ['cache_efficiency ' ];
10921168 $ optimization = $ metrics ['optimization_impact ' ];
10931169
1170+ // Get warning thresholds from config
1171+ $ hitRatioThreshold = $ this ->config ->get ('smart-cache.warnings.hit_ratio_threshold ' , 70 );
1172+ $ optimizationThreshold = $ this ->config ->get ('smart-cache.warnings.optimization_ratio_threshold ' , 20 );
1173+ $ slowWriteThreshold = $ this ->config ->get ('smart-cache.warnings.slow_write_threshold ' , 0.1 );
1174+
10941175 // Hit ratio recommendations
1095- if ($ efficiency ['hit_ratio ' ] < 70 ) {
1176+ if ($ efficiency ['hit_ratio ' ] < $ hitRatioThreshold ) {
10961177 $ recommendations [] = [
10971178 'type ' => 'low_hit_ratio ' ,
10981179 'severity ' => 'warning ' ,
1099- 'message ' => 'Cache hit ratio is below 70%. Consider increasing TTL values or reviewing cache key strategies. ' ,
1100- 'current_ratio ' => $ efficiency ['hit_ratio ' ]
1180+ 'message ' => "Cache hit ratio is below {$ hitRatioThreshold }%. Consider increasing TTL values or reviewing cache key strategies. " ,
1181+ 'current_ratio ' => $ efficiency ['hit_ratio ' ],
1182+ 'threshold ' => $ hitRatioThreshold
11011183 ];
11021184 }
11031185
11041186 // Optimization recommendations
1105- if ($ optimization ['optimization_ratio ' ] < 20 && $ optimization ['total_writes ' ] > 10 ) {
1187+ if ($ optimization ['optimization_ratio ' ] < $ optimizationThreshold && $ optimization ['total_writes ' ] > 10 ) {
11061188 $ recommendations [] = [
11071189 'type ' => 'low_optimization ' ,
11081190 'severity ' => 'info ' ,
1109- 'message ' => 'Few cache entries are being optimized. Consider adjusting compression/chunking thresholds. ' ,
1110- 'current_ratio ' => $ optimization ['optimization_ratio ' ]
1191+ 'message ' => "Few cache entries are being optimized. Consider adjusting compression/chunking thresholds. " ,
1192+ 'current_ratio ' => $ optimization ['optimization_ratio ' ],
1193+ 'threshold ' => $ optimizationThreshold
11111194 ];
11121195 }
11131196
11141197 // Performance issues
11151198 $ writes = $ metrics ['metrics ' ]['cache_write ' ] ?? [];
1116- if (isset ($ writes ['average_duration ' ]) && $ writes ['average_duration ' ] > 0.1 ) {
1199+ if (isset ($ writes ['average_duration ' ]) && $ writes ['average_duration ' ] > $ slowWriteThreshold ) {
11171200 $ recommendations [] = [
11181201 'type ' => 'slow_writes ' ,
11191202 'severity ' => 'warning ' ,
1120- 'message ' => 'Cache write operations are taking longer than 100ms on average. ' ,
1121- 'average_duration ' => round ($ writes ['average_duration ' ] * 1000 , 2 ) . 'ms '
1203+ 'message ' => "Cache write operations are taking longer than " . round ($ slowWriteThreshold * 1000 ) . "ms on average. " ,
1204+ 'average_duration ' => round ($ writes ['average_duration ' ] * 1000 , 2 ) . 'ms ' ,
1205+ 'threshold ' => round ($ slowWriteThreshold * 1000 ) . 'ms '
11221206 ];
11231207 }
11241208
0 commit comments