|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace SolutionForest\InspireCms\Commands; |
| 4 | + |
| 5 | +use Illuminate\Console\Command; |
| 6 | +use Illuminate\Support\Arr; |
| 7 | +use Symfony\Component\Console\Attribute\AsCommand; |
| 8 | +use Illuminate\Support\Facades\Cache; |
| 9 | +use SolutionForest\InspireCms\InspireCmsConfig; |
| 10 | + |
| 11 | +#[AsCommand( |
| 12 | + name: 'inspirecms:cache-stats', |
| 13 | + description: 'Display information about the InspireCMS plugin' |
| 14 | +)] |
| 15 | +class CacheStats extends Command |
| 16 | +{ |
| 17 | + public function handle() |
| 18 | + { |
| 19 | + $this->info('InspireCMS Cache Stats:'); |
| 20 | + $this->line('----------------------------------'); |
| 21 | + $this->line('Cache Driver: ' . config('cache.default')); |
| 22 | + $this->line('Cache Prefix: ' . config('cache.prefix')); |
| 23 | + $this->line('Cache Tags: ' . implode(', ', config('cache.tags', []))); |
| 24 | + |
| 25 | + // Check specific cache keys |
| 26 | + $this->line('----------------------------------'); |
| 27 | + $this->info('Specific Cache Keys:'); |
| 28 | + $cacheKeys = InspireCmsConfig::get('cache'); |
| 29 | + $tableData = []; |
| 30 | + $headers = ['Cache Key', 'Store', 'Exists', 'TTL (seconds)']; |
| 31 | + |
| 32 | + foreach ($cacheKeys as $key => $details) { |
| 33 | + |
| 34 | + if ($key == 'key_value') { |
| 35 | + continue; // Skip key_value cache as it's handled separately |
| 36 | + } |
| 37 | + |
| 38 | + $cacheKey = $details['key'] ?? $key; |
| 39 | + $cacheStore = $details['store'] ?? config('cache.default'); |
| 40 | + |
| 41 | + $exists = Cache::store($cacheStore)->has($cacheKey) ? 'Yes' : 'No'; |
| 42 | + $ttl = $details['ttl'] ?? 'N/A'; |
| 43 | + |
| 44 | + $tableData[] = [ |
| 45 | + $cacheKey, |
| 46 | + $cacheStore, |
| 47 | + $exists, |
| 48 | + $ttl, |
| 49 | + ]; |
| 50 | + } |
| 51 | + |
| 52 | + // Key-Value Cache |
| 53 | + $kvCacheConfig = InspireCmsConfig::get('cache.key_value', []); |
| 54 | + $kvStore = $kvCacheConfig['store'] ?? config('cache.default'); |
| 55 | + if (filled($kvCacheConfig['prefix'] ?? null)) { |
| 56 | + |
| 57 | + $kvCacheKeys = collect($this->getAllCacheKeys($kvStore, config('cache.stores.' . $kvStore, []))) |
| 58 | + ->where(fn ($key) => str_contains($key, $kvCacheConfig['prefix'])) |
| 59 | + ->values() |
| 60 | + ->all() ?? []; |
| 61 | + |
| 62 | + foreach ($kvCacheKeys as $key) { |
| 63 | + |
| 64 | + $formattedCacheKey = $kvCacheConfig['prefix'] . str($key)->explode($kvCacheConfig['prefix'])->skip(1)->implode(''); |
| 65 | + |
| 66 | + $tableData[] = [ |
| 67 | + $key, |
| 68 | + $kvStore, |
| 69 | + Cache::store($kvStore)->has($formattedCacheKey) ? 'Yes' : 'No', |
| 70 | + $kvCacheConfig['ttl'] ?? 'N/A', |
| 71 | + ]; |
| 72 | + } |
| 73 | + |
| 74 | + } |
| 75 | + |
| 76 | + $this->table($headers, $tableData); |
| 77 | + |
| 78 | + return Command::SUCCESS; |
| 79 | + } |
| 80 | + |
| 81 | + private function getAllCacheKeys(string $store, $storeConfig): array |
| 82 | + { |
| 83 | + try { |
| 84 | + |
| 85 | + $driver = $storeConfig['driver'] ?? null; |
| 86 | + |
| 87 | + if ( |
| 88 | + empty($store) |
| 89 | + || empty($driver) // Store driver is not set |
| 90 | + || !is_string($store) // Store name is not a string |
| 91 | + ) { |
| 92 | + return []; |
| 93 | + } |
| 94 | + |
| 95 | + $cacheStore = Cache::store($store)->getStore(); |
| 96 | + |
| 97 | + switch ($driver) { |
| 98 | + case 'redis': |
| 99 | + if (method_exists($cacheStore, 'getRedis')) { |
| 100 | + $redis = $cacheStore->getRedis(); |
| 101 | + $prefix = $cacheStore->getPrefix(); |
| 102 | + $keys = $redis->keys($prefix . '*'); |
| 103 | + return array_map(function ($key) use ($prefix) { |
| 104 | + return substr($key, strlen($prefix)); |
| 105 | + }, $keys); |
| 106 | + } |
| 107 | + break; |
| 108 | + |
| 109 | + case 'file': |
| 110 | + $storage = storage_path('framework/cache/data'); |
| 111 | + $pattern = $storage . '/*'; |
| 112 | + $files = glob($pattern); |
| 113 | + |
| 114 | + return array_map(function ($file) { |
| 115 | + return str_replace('.php', '', basename($file)); |
| 116 | + }, $files); |
| 117 | + |
| 118 | + case 'database': |
| 119 | + |
| 120 | + // Database store requires a connection and table name |
| 121 | + $connection = $cacheStore->getConnection(); |
| 122 | + $table = $storeConfig['table'] ?? 'cache'; |
| 123 | + $query = $connection->table($table)->pluck('key')->toArray(); |
| 124 | + |
| 125 | + return array_map(function ($key) { |
| 126 | + return str_replace('inspire_key_value_', '', $key); |
| 127 | + }, $query); |
| 128 | + |
| 129 | + case 'dynamodb': |
| 130 | + |
| 131 | + // DynamoDB store requires a connection and table name |
| 132 | + $connection = config('database.connections.' . $cacheStore->getConnectionName()); |
| 133 | + $table = $cacheStore->getTable(); |
| 134 | + $query = \Illuminate\Support\Facades\DB::connection($connection)->table($table)->pluck('key')->toArray(); |
| 135 | + |
| 136 | + return array_map(function ($key) { |
| 137 | + return str_replace('inspire_key_value_', '', $key); |
| 138 | + }, $query); |
| 139 | + |
| 140 | + case 'array': |
| 141 | + case 'octane': |
| 142 | + // Octane store does not persist keys, so no keys to retrieve |
| 143 | + return []; |
| 144 | + |
| 145 | + } |
| 146 | + } catch (\Throwable $th) { |
| 147 | + // |
| 148 | + } |
| 149 | + |
| 150 | + return []; |
| 151 | + } |
| 152 | +} |
0 commit comments