|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace Nuwave\Lighthouse\Cache; |
| 4 | + |
| 5 | +use GraphQL\Language\AST\DocumentNode; |
| 6 | +use GraphQL\Utils\AST; |
| 7 | +use Illuminate\Config\Repository as ConfigRepository; |
| 8 | +use Illuminate\Container\Container; |
| 9 | +use Illuminate\Contracts\Cache\Factory as CacheFactory; |
| 10 | +use Illuminate\Contracts\Cache\Repository as CacheRepository; |
| 11 | +use Illuminate\Filesystem\Filesystem; |
| 12 | + |
| 13 | +class QueryCache |
| 14 | +{ |
| 15 | + protected bool $enable; |
| 16 | + |
| 17 | + protected string $mode; |
| 18 | + |
| 19 | + protected string $opcachePath; |
| 20 | + |
| 21 | + protected ?string $store; |
| 22 | + |
| 23 | + protected ?int $ttl; |
| 24 | + |
| 25 | + public function __construct( |
| 26 | + protected ConfigRepository $configRepository, |
| 27 | + protected Filesystem $filesystem, |
| 28 | + ) { |
| 29 | + $config = $this->configRepository->get('lighthouse.query_cache'); |
| 30 | + |
| 31 | + $this->enable = (bool) $config['enable']; |
| 32 | + $this->mode = $config['mode'] ?? 'store'; |
| 33 | + $this->opcachePath = $config['opcache_path'] ?? base_path('bootstrap/cache'); |
| 34 | + $this->store = $config['store'] ?? null; |
| 35 | + $this->ttl = $config['ttl'] ?? null; |
| 36 | + } |
| 37 | + |
| 38 | + public function isEnabled(): bool |
| 39 | + { |
| 40 | + return $this->enable; |
| 41 | + } |
| 42 | + |
| 43 | + public function clear(?int $opcacheTTLHours, bool $opcacheOnly): void |
| 44 | + { |
| 45 | + if (in_array($this->mode, ['store', 'hybrid']) |
| 46 | + && ! $opcacheOnly |
| 47 | + ) { |
| 48 | + $store = $this->makeCacheStore(); |
| 49 | + $store->clear(); |
| 50 | + } |
| 51 | + |
| 52 | + if (in_array($this->mode, ['opcache', 'hybrid'])) { |
| 53 | + $files = $this->filesystem->glob($this->opcacheFilePath('*')); |
| 54 | + |
| 55 | + if (is_int($opcacheTTLHours)) { |
| 56 | + $threshold = now()->subHours($opcacheTTLHours)->timestamp; |
| 57 | + $files = array_filter( |
| 58 | + $files, |
| 59 | + fn (string $file): bool => $this->filesystem->lastModified($file) < $threshold, |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + $this->filesystem->delete($files); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + /** @param \Closure(): DocumentNode $parse */ |
| 68 | + public function fromCacheOrParse(string $hash, \Closure $parse): DocumentNode |
| 69 | + { |
| 70 | + return match ($this->mode) { |
| 71 | + 'store' => $this->fromStoreOrParse($hash, $parse), |
| 72 | + 'opcache' => $this->fromOPcacheOrParse($hash, $parse), |
| 73 | + 'hybrid' => $this->fromHybridOrParse($hash, $parse), |
| 74 | + default => throw new \InvalidArgumentException("Invalid query cache mode: {$this->mode}."), |
| 75 | + }; |
| 76 | + } |
| 77 | + |
| 78 | + /** @param \Closure(): DocumentNode $parse */ |
| 79 | + protected function fromStoreOrParse(string $hash, \Closure $parse): DocumentNode |
| 80 | + { |
| 81 | + $store = $this->makeCacheStore(); |
| 82 | + |
| 83 | + return $store->remember(key: "lighthouse:query:{$hash}", ttl: $this->ttl, callback: $parse); |
| 84 | + } |
| 85 | + |
| 86 | + /** @param \Closure(): DocumentNode $parse */ |
| 87 | + protected function fromOPcacheOrParse(string $hash, \Closure $parse): DocumentNode |
| 88 | + { |
| 89 | + $filePath = $this->opcacheFilePath($hash); |
| 90 | + |
| 91 | + if ($this->filesystem->exists($filePath)) { |
| 92 | + return $this->requireOPcacheFile($filePath); |
| 93 | + } |
| 94 | + |
| 95 | + $query = $parse(); |
| 96 | + |
| 97 | + $contents = static::opcacheFileContents($query); |
| 98 | + $this->filesystem->put(path: $filePath, contents: $contents, lock: true); |
| 99 | + |
| 100 | + return $query; |
| 101 | + } |
| 102 | + |
| 103 | + /** @param \Closure(): DocumentNode $parse */ |
| 104 | + protected function fromHybridOrParse(string $hash, \Closure $parse): DocumentNode |
| 105 | + { |
| 106 | + $filePath = $this->opcacheFilePath($hash); |
| 107 | + |
| 108 | + if ($this->filesystem->exists($filePath)) { |
| 109 | + return $this->requireOPcacheFile($filePath); |
| 110 | + } |
| 111 | + |
| 112 | + $store = $this->makeCacheStore(); |
| 113 | + |
| 114 | + $contents = $store->get(key: "lighthouse:query:{$hash}"); |
| 115 | + if (is_string($contents)) { |
| 116 | + $this->filesystem->put(path: $filePath, contents: $contents, lock: true); |
| 117 | + |
| 118 | + return $this->requireOPcacheFile($filePath); |
| 119 | + } |
| 120 | + |
| 121 | + $query = $parse(); |
| 122 | + |
| 123 | + $contents = static::opcacheFileContents($query); |
| 124 | + $store->put(key: "lighthouse:query:{$hash}", value: $contents, ttl: $this->ttl); |
| 125 | + $this->filesystem->put(path: $filePath, contents: $contents, lock: true); |
| 126 | + |
| 127 | + return $query; |
| 128 | + } |
| 129 | + |
| 130 | + protected function makeCacheStore(): CacheRepository |
| 131 | + { |
| 132 | + $cacheFactory = Container::getInstance()->make(CacheFactory::class); |
| 133 | + |
| 134 | + return $cacheFactory->store($this->store); |
| 135 | + } |
| 136 | + |
| 137 | + public static function opcacheFileContents(DocumentNode $query): string |
| 138 | + { |
| 139 | + $queryArrayString = var_export( |
| 140 | + value: $query->toArray(), |
| 141 | + return: true, |
| 142 | + ); |
| 143 | + |
| 144 | + return "<?php return {$queryArrayString};"; |
| 145 | + } |
| 146 | + |
| 147 | + protected function requireOPcacheFile(string $filePath): DocumentNode |
| 148 | + { |
| 149 | + $astArray = require $filePath; |
| 150 | + assert(is_array($astArray), 'The cache file is expected to return an array.'); |
| 151 | + |
| 152 | + $astInstance = AST::fromArray($astArray); |
| 153 | + assert($astInstance instanceof DocumentNode, 'The AST array is expected to convert to a DocumentNode.'); |
| 154 | + |
| 155 | + return $astInstance; |
| 156 | + } |
| 157 | + |
| 158 | + protected function opcacheFilePath(string $hash): string |
| 159 | + { |
| 160 | + return "{$this->opcachePath}/lighthouse-query-{$hash}.php"; |
| 161 | + } |
| 162 | +} |
0 commit comments