Files
michaelschiemer/src/Framework/Cache/CacheInitializer.php
Michael Schiemer 5050c7d73a docs: consolidate documentation into organized structure
- 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
2025-10-05 11:05:04 +02:00

120 lines
4.2 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Framework\Cache;
use App\Framework\Async\AsyncService;
use App\Framework\Cache\Compression\GzipCompression;
use App\Framework\Cache\Driver\ApcuCache;
use App\Framework\Cache\Driver\FileCache;
use App\Framework\Cache\Driver\InMemoryCache;
use App\Framework\Cache\Driver\NullCache;
use App\Framework\Cache\Driver\RedisCache;
use App\Framework\Cache\Metrics\CacheMetrics;
use App\Framework\Cache\Metrics\CacheMetricsInterface;
use App\Framework\Cache\Metrics\MetricsDecoratedCache;
use App\Framework\DI\Container;
use App\Framework\DI\Initializer;
use App\Framework\Performance\Contracts\PerformanceCollectorInterface;
use App\Framework\Redis\RedisConfig;
use App\Framework\Redis\RedisConnection;
use App\Framework\Serializer\Json\JsonSerializer;
use App\Framework\Serializer\Php\PhpSerializer;
final readonly class CacheInitializer
{
public function __construct(
private PerformanceCollectorInterface $performanceCollector,
private Container $container,
private ?AsyncService $asyncService = null,
#private CacheMetricsInterface $cacheMetrics,
private string $redisHost = 'redis',
private int $redisPort = 6379,
private int $compressionLevel = -1,
private int $minCompressionLength = 1024,
private bool $enableAsync = true
) {
}
#[Initializer]
public function __invoke(): Cache
{
#$this->clear();
$serializer = new PhpSerializer();
#$serializer = new JsonSerializer();
$compression = new GzipCompression($this->compressionLevel, $this->minCompressionLength);
// L1 Cache: Fast cache with compression for larger values
if (function_exists('apcu_clear_cache')) {
$apcuCache = new GeneralCache(new APCuCache(), $serializer, $compression);
} else {
$apcuCache = new GeneralCache(new InMemoryCache(), $serializer, $compression);
}
// L2 Cache: Persistent cache with compression
try {
// Check if Redis extension is available
if (! extension_loaded('redis')) {
throw new \RuntimeException('Redis extension is not loaded. Please install php-redis extension or use alternative cache drivers.');
}
$redisConfig = new RedisConfig(
host: $this->redisHost,
port: $this->redisPort,
database: 1 // Use DB 1 for cache
);
$redisConnection = new RedisConnection($redisConfig, 'cache');
$redisCache = new GeneralCache(new RedisCache($redisConnection), $serializer, $compression);
} catch (\Throwable $e) {
// Fallback to file cache if Redis is not available
error_log("Redis not available, falling back to file cache: " . $e->getMessage());
$redisCache = new GeneralCache(new FileCache(), $serializer, $compression);
}
#$redisCache->clear();
$multiLevelCache = new MultiLevelCache($apcuCache, $redisCache);
#return $multiLevelCache;
#return new LoggingCacheDecorator($multiLevelCache);
#return new GeneralCache(new NullCache(), $serializer, $compression);
// Create cache metrics instance directly to avoid DI circular dependency
$cacheMetrics = new CacheMetrics();
// Bind it to container for other services that might need it
if (! $this->container->has(CacheMetricsInterface::class)) {
$this->container->bind(CacheMetricsInterface::class, $cacheMetrics);
}
// Add comprehensive cache metrics with integrated performance tracking
$metricsCache = new MetricsDecoratedCache(
$multiLevelCache,
$cacheMetrics,
'MultiLevel',
$this->performanceCollector,
true // Performance tracking enabled
);
// Wrap with SmartCache for intelligent async processing and pattern support
return new SmartCache(
$metricsCache,
$this->asyncService,
$this->enableAsync
);
#return new GeneralCache(new NullCache());
}
private function clear(): void
{
apcu_clear_cache();
}
}