Skip to content

Commit 4232839

Browse files
authored
Merge pull request #31 from moufmouf/lock_cache_rebuild
Locks are placed before cache check
2 parents ae2238c + b2046c5 commit 4232839

File tree

2 files changed

+45
-13
lines changed

2 files changed

+45
-13
lines changed

src/GlobControllerQueryProvider.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Mouf\Composer\ClassNameMapper;
77
use Psr\Container\ContainerInterface;
88
use Psr\SimpleCache\CacheInterface;
9-
use Symfony\Component\Lock\Store\SemaphoreStore;
9+
use Symfony\Component\Lock\Lock;
1010
use TheCodingMachine\ClassExplorer\Glob\GlobClassExplorer;
1111
use TheCodingMachine\GraphQLite\Mappers\RecursiveTypeMapperInterface;
1212
use Symfony\Component\Lock\Factory as LockFactory;
@@ -100,16 +100,24 @@ private function getInstancesList(): array
100100
$key = 'globQueryProvider_'.str_replace('\\', '_', $this->namespace);
101101
$this->instancesList = $this->cache->get($key);
102102
if ($this->instancesList === null) {
103-
$this->instancesList = $this->lockAndBuildInstanceList();
103+
$lock = $this->lockFactory->createLock('buildInstanceList_'.$this->namespace, 5);
104+
if ($lock->isAcquired()) {
105+
// Lock is being held right now. Generation is happening.
106+
// Let's wait and fetch the result from the cache.
107+
$lock->acquire(true);
108+
$lock->release();
109+
return $this->getInstancesList();
110+
}
111+
112+
$this->instancesList = $this->lockAndBuildInstanceList($lock);
104113
$this->cache->set($key, $this->instancesList, $this->cacheTtl);
105114
}
106115
}
107116
return $this->instancesList;
108117
}
109118

110-
private function lockAndBuildInstanceList(): array
119+
private function lockAndBuildInstanceList(Lock $lock): array
111120
{
112-
$lock = $this->lockFactory->createLock('buildInstanceList_'.$this->namespace, 5);
113121
$lock->acquire(true);
114122
try {
115123
return $this->buildInstancesList();

src/Mappers/GlobTypeMapper.php

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use ReflectionClass;
1414
use ReflectionMethod;
1515
use Symfony\Component\Lock\Factory as LockFactory;
16+
use Symfony\Component\Lock\Lock;
1617
use Symfony\Component\Lock\Store\SemaphoreStore;
1718
use TheCodingMachine\ClassExplorer\Glob\GlobClassExplorer;
1819
use TheCodingMachine\GraphQLite\AnnotationReader;
@@ -167,7 +168,15 @@ private function getMaps(): array
167168
$this->mapClassToFactory === null ||
168169
$this->mapInputNameToFactory
169170
) {
170-
$this->lockAndBuildMap();
171+
$lock = $this->lockFactory->createLock('buildmap_'.$this->namespace, 5);
172+
if ($lock->isAcquired()) {
173+
// Lock is being held right now. Generation is happening.
174+
// Let's wait and fetch the result from the cache.
175+
$lock->acquire(true);
176+
$lock->release();
177+
return $this->getMaps();
178+
}
179+
$this->lockAndBuildMap($lock);
171180
// This is a very short lived cache. Useful to avoid overloading a server in case of heavy load.
172181
// Defaults to 2 seconds.
173182
$this->cache->set($keyClassCache, $this->mapClassToTypeArray, $this->globTtl);
@@ -212,7 +221,16 @@ private function getMapClassToExtendTypeArray(): array
212221
$keyExtendClassCache = 'globTypeMapperExtend_'.$namespace;
213222
$this->mapClassToExtendTypeArray = $this->cache->get($keyExtendClassCache);
214223
if ($this->mapClassToExtendTypeArray === null) {
215-
$this->buildMapClassToExtendTypeArray();
224+
$lock = $this->lockFactory->createLock('buildmapclassextend_'.$this->namespace, 5);
225+
if ($lock->isAcquired()) {
226+
// Lock is being held right now. Generation is happening.
227+
// Let's wait and fetch the result from the cache.
228+
$lock->acquire(true);
229+
$lock->release();
230+
return $this->getMapClassToExtendTypeArray();
231+
}
232+
233+
$this->buildMapClassToExtendTypeArray($lock);
216234
// This is a very short lived cache. Useful to avoid overloading a server in case of heavy load.
217235
// Defaults to 2 seconds.
218236
$this->cache->set($keyExtendClassCache, $this->mapClassToExtendTypeArray, $this->globTtl);
@@ -229,7 +247,16 @@ private function getMapNameToExtendType(RecursiveTypeMapperInterface $recursiveT
229247
$keyExtendNameCache = 'globTypeMapperExtend_names_'.$namespace;
230248
$this->mapNameToExtendType = $this->cache->get($keyExtendNameCache);
231249
if ($this->mapNameToExtendType === null) {
232-
$this->buildMapNameToExtendTypeArray($recursiveTypeMapper);
250+
$lock = $this->lockFactory->createLock('buildmapnameextend_'.$this->namespace, 5);
251+
if ($lock->isAcquired()) {
252+
// Lock is being held right now. Generation is happening.
253+
// Let's wait and fetch the result from the cache.
254+
$lock->acquire(true);
255+
$lock->release();
256+
return $this->getMapNameToExtendType($recursiveTypeMapper);
257+
}
258+
259+
$this->buildMapNameToExtendTypeArray($lock, $recursiveTypeMapper);
233260
// This is a very short lived cache. Useful to avoid overloading a server in case of heavy load.
234261
// Defaults to 2 seconds.
235262
$this->cache->set($keyExtendNameCache, $this->mapNameToExtendType, $this->globTtl);
@@ -265,9 +292,8 @@ private function getClassList(): array
265292
return $this->classes;
266293
}
267294

268-
private function lockAndBuildMap(): void
295+
private function lockAndBuildMap(Lock $lock): void
269296
{
270-
$lock = $this->lockFactory->createLock('buildmap_'.$this->namespace, 5);
271297
$lock->acquire(true);
272298
try {
273299
$this->buildMap();
@@ -319,9 +345,8 @@ private function buildMap(): void
319345
}
320346
}
321347

322-
private function buildMapClassToExtendTypeArray(): void
348+
private function buildMapClassToExtendTypeArray(Lock $lock): void
323349
{
324-
$lock = $this->lockFactory->createLock('buildmapclassextend_'.$this->namespace, 5);
325350
$lock->acquire(true);
326351
try {
327352
$this->mapClassToExtendTypeArray = [];
@@ -338,9 +363,8 @@ private function buildMapClassToExtendTypeArray(): void
338363
}
339364
}
340365

341-
private function buildMapNameToExtendTypeArray(RecursiveTypeMapperInterface $recursiveTypeMapper): void
366+
private function buildMapNameToExtendTypeArray(Lock $lock, RecursiveTypeMapperInterface $recursiveTypeMapper): void
342367
{
343-
$lock = $this->lockFactory->createLock('buildmapnameextend_'.$this->namespace, 5);
344368
$lock->acquire(true);
345369
try {
346370
$this->mapNameToExtendType = [];

0 commit comments

Comments
 (0)