99use Illuminate \Contracts \Cache \Factory as CacheFactory ;
1010use Illuminate \Contracts \Cache \Repository as CacheRepository ;
1111use Illuminate \Filesystem \Filesystem ;
12+ use Nuwave \Lighthouse \Support \Utils ;
1213
1314class QueryCache
1415{
@@ -30,7 +31,7 @@ public function __construct(
3031
3132 $ this ->enable = (bool ) $ config ['enable ' ];
3233 $ this ->mode = $ config ['mode ' ] ?? 'store ' ;
33- $ this ->opcachePath = $ config ['opcache_path ' ] ?? base_path ('bootstrap/cache ' );
34+ $ this ->opcachePath = rtrim ( $ config ['opcache_path ' ] ?? base_path ('bootstrap/cache ' ), ' / ' );
3435 $ this ->store = $ config ['store ' ] ?? null ;
3536 $ this ->ttl = $ config ['ttl ' ] ?? null ;
3637 }
@@ -86,43 +87,43 @@ protected function fromStoreOrParse(string $hash, \Closure $parse): DocumentNode
8687 /** @param \Closure(): DocumentNode $parse */
8788 protected function fromOPcacheOrParse (string $ hash , \Closure $ parse ): DocumentNode
8889 {
89- $ filePath = $ this ->opcacheFilePath ($ hash );
90+ $ path = $ this ->opcacheFilePath ($ hash );
9091
91- if ($ this ->filesystem ->exists ($ filePath )) {
92- return $ this ->requireOPcacheFile ($ filePath );
92+ if ($ this ->filesystem ->exists ($ path )) {
93+ return $ this ->requireOPcacheFile ($ path );
9394 }
9495
9596 $ query = $ parse ();
9697
9798 $ contents = static ::opcacheFileContents ($ query );
98- $ this ->filesystem -> put ( path: $ filePath , contents: $ contents, lock: true );
99+ Utils:: atomicPut (filesystem: $ this ->filesystem , path: $ path , contents: $ contents );
99100
100101 return $ query ;
101102 }
102103
103104 /** @param \Closure(): DocumentNode $parse */
104105 protected function fromHybridOrParse (string $ hash , \Closure $ parse ): DocumentNode
105106 {
106- $ filePath = $ this ->opcacheFilePath ($ hash );
107+ $ path = $ this ->opcacheFilePath ($ hash );
107108
108- if ($ this ->filesystem ->exists ($ filePath )) {
109- return $ this ->requireOPcacheFile ($ filePath );
109+ if ($ this ->filesystem ->exists ($ path )) {
110+ return $ this ->requireOPcacheFile ($ path );
110111 }
111112
112113 $ store = $ this ->makeCacheStore ();
113114
114115 $ contents = $ store ->get (key: "lighthouse:query: {$ hash }" );
115116 if (is_string ($ contents )) {
116- $ this ->filesystem -> put ( path: $ filePath , contents: $ contents, lock: true );
117+ Utils:: atomicPut (filesystem: $ this ->filesystem , path: $ path , contents: $ contents );
117118
118- return $ this ->requireOPcacheFile ($ filePath );
119+ return $ this ->requireOPcacheFile ($ path );
119120 }
120121
121122 $ query = $ parse ();
122123
123124 $ contents = static ::opcacheFileContents ($ query );
124125 $ store ->put (key: "lighthouse:query: {$ hash }" , value: $ contents , ttl: $ this ->ttl );
125- $ this ->filesystem -> put ( path: $ filePath , contents: $ contents, lock: true );
126+ Utils:: atomicPut (filesystem: $ this ->filesystem , path: $ path , contents: $ contents );
126127
127128 return $ query ;
128129 }
@@ -144,10 +145,10 @@ public static function opcacheFileContents(DocumentNode $query): string
144145 return "<?php return {$ queryArrayString }; " ;
145146 }
146147
147- protected function requireOPcacheFile (string $ filePath ): DocumentNode
148+ protected function requireOPcacheFile (string $ path ): DocumentNode
148149 {
149- $ astArray = require $ filePath ;
150- assert (is_array ($ astArray ), ' The cache file is expected to return an array. ' );
150+ $ astArray = require $ path ;
151+ assert (is_array ($ astArray ), " The query cache file at { $ path } is expected to return an array." );
151152
152153 $ astInstance = AST ::fromArray ($ astArray );
153154 assert ($ astInstance instanceof DocumentNode, 'The AST array is expected to convert to a DocumentNode. ' );
0 commit comments