- Move 12 markdown files from root to docs/ subdirectories - Organize documentation by category: • docs/troubleshooting/ (1 file) - Technical troubleshooting guides • docs/deployment/ (4 files) - Deployment and security documentation • docs/guides/ (3 files) - Feature-specific guides • docs/planning/ (4 files) - Planning and improvement proposals Root directory cleanup: - Reduced from 16 to 4 markdown files in root - Only essential project files remain: • CLAUDE.md (AI instructions) • README.md (Main project readme) • CLEANUP_PLAN.md (Current cleanup plan) • SRC_STRUCTURE_IMPROVEMENTS.md (Structure improvements) This improves: ✅ Documentation discoverability ✅ Logical organization by purpose ✅ Clean root directory ✅ Better maintainability
223 lines
8.0 KiB
PHP
223 lines
8.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
require_once __DIR__ . '/../../vendor/autoload.php';
|
|
|
|
use App\Framework\Cache\SmartCache;
|
|
use App\Framework\Cache\CacheKey;
|
|
use App\Framework\Cache\CacheItem;
|
|
use App\Framework\Cache\CacheResult;
|
|
use App\Framework\Core\ValueObjects\Duration;
|
|
|
|
echo "=== Testing Default Strategies Auto-Activation ===\n\n";
|
|
|
|
// Create simple mock cache
|
|
$mockCache = new class implements \App\Framework\Cache\Cache {
|
|
private array $storage = [];
|
|
|
|
public function get(\App\Framework\Cache\CacheIdentifier ...$identifiers): \App\Framework\Cache\CacheResult
|
|
{
|
|
$items = [];
|
|
foreach ($identifiers as $identifier) {
|
|
$key = $identifier->toString();
|
|
if (isset($this->storage[$key])) {
|
|
$items[] = \App\Framework\Cache\CacheItem::hit($identifier, $this->storage[$key]['value']);
|
|
} else {
|
|
$items[] = \App\Framework\Cache\CacheItem::miss($identifier);
|
|
}
|
|
}
|
|
return \App\Framework\Cache\CacheResult::fromItems(...$items);
|
|
}
|
|
|
|
public function set(\App\Framework\Cache\CacheItem ...$items): bool
|
|
{
|
|
foreach ($items as $item) {
|
|
$this->storage[$item->key->toString()] = [
|
|
'value' => $item->value,
|
|
'ttl' => $item->ttl
|
|
];
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public function has(\App\Framework\Cache\CacheIdentifier ...$identifiers): array
|
|
{
|
|
$results = [];
|
|
foreach ($identifiers as $identifier) {
|
|
$results[$identifier->toString()] = isset($this->storage[$identifier->toString()]);
|
|
}
|
|
return $results;
|
|
}
|
|
|
|
public function forget(\App\Framework\Cache\CacheIdentifier ...$identifiers): bool
|
|
{
|
|
foreach ($identifiers as $identifier) {
|
|
unset($this->storage[$identifier->toString()]);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public function clear(): bool
|
|
{
|
|
$this->storage = [];
|
|
return true;
|
|
}
|
|
|
|
public function remember(\App\Framework\Cache\CacheKey $key, callable $callback, ?\App\Framework\Core\ValueObjects\Duration $ttl = null): \App\Framework\Cache\CacheItem
|
|
{
|
|
$result = $this->get($key);
|
|
$item = $result->getItem($key);
|
|
if ($item->isHit) {
|
|
return $item;
|
|
}
|
|
|
|
$value = $callback();
|
|
$this->set(\App\Framework\Cache\CacheItem::forSet($key, $value, $ttl));
|
|
return \App\Framework\Cache\CacheItem::hit($key, $value);
|
|
}
|
|
};
|
|
|
|
echo "1. Testing Default Constructor Behavior:\n\n";
|
|
|
|
try {
|
|
// Create SmartCache with default constructor (should auto-enable strategies)
|
|
$smartCache = new SmartCache($mockCache);
|
|
|
|
echo " ✅ SmartCache created with default constructor\n";
|
|
|
|
// Check if strategies are enabled
|
|
$stats = $smartCache->getStats();
|
|
$strategySupport = $stats['strategy_support'] ?? false;
|
|
$strategyManager = $smartCache->getStrategyManager();
|
|
|
|
echo " 📊 Strategy support: " . ($strategySupport ? 'YES' : 'NO') . "\n";
|
|
|
|
if ($strategyManager) {
|
|
$managerStats = $strategyManager->getStats();
|
|
echo " 📊 Total strategies: {$managerStats['strategy_manager']['total_strategies']}\n";
|
|
echo " 📊 Enabled strategies: {$managerStats['strategy_manager']['enabled_strategies']}\n";
|
|
echo " 📊 Strategy names: " . implode(', ', $managerStats['strategy_manager']['strategy_names']) . "\n";
|
|
}
|
|
|
|
echo "\n";
|
|
|
|
} catch (\Throwable $e) {
|
|
echo " ❌ Error testing default constructor: {$e->getMessage()}\n\n";
|
|
}
|
|
|
|
echo "2. Testing Strategy Auto-Activation with Cache Operations:\n\n";
|
|
|
|
try {
|
|
$smartCache = new SmartCache($mockCache);
|
|
$testKey = CacheKey::fromString('auto_strategy_test');
|
|
|
|
echo " 🔧 Testing cache operations with auto-enabled strategies:\n";
|
|
|
|
// Test cache set with TTL modification by strategies
|
|
$originalTtl = Duration::fromMinutes(30);
|
|
$smartCache->set(CacheItem::forSet($testKey, "test_value", $originalTtl));
|
|
|
|
echo " ✅ Cache SET operation completed\n";
|
|
|
|
// Test cache get with strategy access tracking
|
|
$result = $smartCache->get($testKey);
|
|
$item = $result->getItem($testKey);
|
|
|
|
echo " ✅ Cache GET operation completed: " . ($item->isHit ? 'HIT' : 'MISS') . "\n";
|
|
|
|
// Test remember operation with strategy integration
|
|
$rememberResult = $smartCache->remember(
|
|
CacheKey::fromString('remember_test'),
|
|
fn() => "remembered_value",
|
|
Duration::fromHours(1)
|
|
);
|
|
|
|
echo " ✅ Cache REMEMBER operation completed: " . ($rememberResult->isHit ? 'HIT' : 'MISS') . "\n";
|
|
|
|
// Check if strategies have recorded activity
|
|
$adaptiveStats = $smartCache->getStrategyStats('adaptive_ttl');
|
|
if ($adaptiveStats) {
|
|
echo " 📊 Adaptive TTL tracked keys: {$adaptiveStats['total_tracked_keys']}\n";
|
|
}
|
|
|
|
$heatMapStats = $smartCache->getStrategyStats('heat_map');
|
|
if ($heatMapStats) {
|
|
echo " 📊 Heat Map tracked keys: {$heatMapStats['total_tracked_keys']}\n";
|
|
}
|
|
|
|
echo "\n";
|
|
|
|
} catch (\Throwable $e) {
|
|
echo " ❌ Error testing strategy auto-activation: {$e->getMessage()}\n\n";
|
|
}
|
|
|
|
echo "3. Testing Explicit Strategy Control:\n\n";
|
|
|
|
try {
|
|
// Test withoutStrategies method
|
|
echo " 🚫 Testing explicit strategy disabling:\n";
|
|
$noStrategyCache = SmartCache::withoutStrategies($mockCache);
|
|
|
|
$noStrategyStats = $noStrategyCache->getStats();
|
|
$hasStrategies = $noStrategyStats['strategy_support'] ?? false;
|
|
|
|
echo " ✅ Cache without strategies created\n";
|
|
echo " 📊 Strategy support: " . ($hasStrategies ? 'YES' : 'NO') . "\n";
|
|
|
|
// Test withDefaultStrategies method (explicit activation)
|
|
echo "\n ✨ Testing explicit strategy enabling:\n";
|
|
$explicitStrategyCache = SmartCache::withDefaultStrategies($mockCache);
|
|
|
|
$explicitStats = $explicitStrategyCache->getStats();
|
|
$explicitHasStrategies = $explicitStats['strategy_support'] ?? false;
|
|
|
|
echo " ✅ Cache with explicit strategies created\n";
|
|
echo " 📊 Strategy support: " . ($explicitHasStrategies ? 'YES' : 'NO') . "\n";
|
|
|
|
echo "\n";
|
|
|
|
} catch (\Throwable $e) {
|
|
echo " ❌ Error testing explicit strategy control: {$e->getMessage()}\n\n";
|
|
}
|
|
|
|
echo "4. Testing Performance vs Intelligence Trade-off:\n\n";
|
|
|
|
try {
|
|
echo " ⚡ Comparing cache configurations:\n";
|
|
|
|
// Without strategies (maximum performance)
|
|
$performanceCache = SmartCache::withoutStrategies($mockCache);
|
|
|
|
// With default strategies (balanced intelligence)
|
|
$intelligentCache = new SmartCache($mockCache); // Uses default strategies
|
|
|
|
// With performance strategies (performance-focused intelligence)
|
|
$perfOptimizedCache = SmartCache::withPerformanceStrategies($mockCache);
|
|
|
|
echo " ✅ Performance Cache (no strategies): Strategy support = " .
|
|
($performanceCache->getStats()['strategy_support'] ? 'YES' : 'NO') . "\n";
|
|
|
|
echo " ✅ Intelligent Cache (default strategies): Strategy support = " .
|
|
($intelligentCache->getStats()['strategy_support'] ? 'YES' : 'NO') . "\n";
|
|
|
|
echo " ✅ Performance-Optimized Cache (perf strategies): Strategy support = " .
|
|
($perfOptimizedCache->getStats()['strategy_support'] ? 'YES' : 'NO') . "\n";
|
|
|
|
echo "\n";
|
|
|
|
} catch (\Throwable $e) {
|
|
echo " ❌ Error testing performance configurations: {$e->getMessage()}\n\n";
|
|
}
|
|
|
|
echo "=== Default Strategies Auto-Activation Test Completed ===\n";
|
|
echo "\n🎯 Summary:\n";
|
|
echo " • ✅ SmartCache constructor now auto-enables Default Strategies\n";
|
|
echo " • ✅ Strategy support is active by default for intelligence\n";
|
|
echo " • ✅ SmartCache::withoutStrategies() available for maximum performance\n";
|
|
echo " • ✅ Backward compatibility maintained with explicit methods\n";
|
|
echo " • ✅ Convention over Configuration philosophy implemented\n";
|
|
echo "\n💡 Usage:\n";
|
|
echo " new SmartCache(\$cache) → WITH strategies (default)\n";
|
|
echo " SmartCache::withDefaultStrategies(\$cache) → WITH strategies (explicit)\n";
|
|
echo " SmartCache::withoutStrategies(\$cache) → WITHOUT strategies (performance)\n"; |