From 9cfa27b6bd17fa27d39792c794937ad389cfbc47 Mon Sep 17 00:00:00 2001 From: Matt Breden Date: Mon, 23 Nov 2020 15:22:44 -0800 Subject: [PATCH 1/2] Update SchemaFactory to allow the annotation cache dir to be configurable Addresses https://github.com/thecodingmachine/graphqlite/issues/309 --- src/SchemaFactory.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/SchemaFactory.php b/src/SchemaFactory.php index a697fe9fd7..e5871bc423 100644 --- a/src/SchemaFactory.php +++ b/src/SchemaFactory.php @@ -86,6 +86,8 @@ class SchemaFactory private $parameterMiddlewares = []; /** @var Reader */ private $doctrineAnnotationReader; + /** @var string */ + private $annotationCacheDir; /** @var AuthenticationServiceInterface|null */ private $authenticationService; /** @var AuthorizationServiceInterface|null */ @@ -203,6 +205,13 @@ public function setDoctrineAnnotationReader(Reader $annotationReader): self return $this; } + public function setAnnotationCacheDir(string $cacheDir): self + { + $this->annotationCacheDir = $cacheDir; + + return $this; + } + /** * Returns a cached Doctrine annotation reader. * Note: we cannot get the annotation reader service in the container as we are in a compiler pass. @@ -213,7 +222,8 @@ private function getDoctrineAnnotationReader(): Reader AnnotationRegistry::registerLoader('class_exists'); $doctrineAnnotationReader = new DoctrineAnnotationReader(); - $cache = function_exists('apcu_fetch') ? new ApcuCache() : new PhpFileCache(sys_get_temp_dir() . '/graphqlite.' . crc32(__DIR__)); + $cacheDir = $this->annotationCacheDir ?? sys_get_temp_dir(); + $cache = function_exists('apcu_fetch') ? new ApcuCache() : new PhpFileCache($cacheDir . '/graphqlite.' . crc32(__DIR__)); $cache->setNamespace($this->cacheNamespace); From a88a534db4c23bb99f65d0dac9a361c8f7eabb1e Mon Sep 17 00:00:00 2001 From: Matt Breden Date: Thu, 3 Dec 2020 15:34:32 -0800 Subject: [PATCH 2/2] Modify apcu check in getDoctrineAnnotationReader() to use 'apcu_enabled' function --- src/SchemaFactory.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/SchemaFactory.php b/src/SchemaFactory.php index e5871bc423..3b984bbaed 100644 --- a/src/SchemaFactory.php +++ b/src/SchemaFactory.php @@ -222,8 +222,12 @@ private function getDoctrineAnnotationReader(): Reader AnnotationRegistry::registerLoader('class_exists'); $doctrineAnnotationReader = new DoctrineAnnotationReader(); - $cacheDir = $this->annotationCacheDir ?? sys_get_temp_dir(); - $cache = function_exists('apcu_fetch') ? new ApcuCache() : new PhpFileCache($cacheDir . '/graphqlite.' . crc32(__DIR__)); + if (function_exists('apcu_enabled') && apcu_enabled()) { + $cache = new ApcuCache(); + } else { + $cacheDir = $this->annotationCacheDir ?? sys_get_temp_dir(); + $cache = new PhpFileCache($cacheDir . '/graphqlite.' . crc32(__DIR__)); + } $cache->setNamespace($this->cacheNamespace);