8000 Allow setter injection for custom cache and lock implementations · FriendsOfSymfony/FOSHttpCache@16e4f22 · GitHub
[go: up one dir, main page]

Skip to content

Commit 16e4f22

Browse files
committed
Allow setter injection for custom cache and lock implementations
1 parent d5bf36d commit 16e4f22

File tree

1 file changed

+57
-8
lines changed

1 file changed

+57
-8
lines changed

src/SymfonyCache/TaggableStore.php

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Psr\Cache\InvalidArgumentException;
1515
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
1616
use Symfony\Component\Cache\Adapter\TagAwareAdapter;
17+
use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
1718
use Symfony\Component\HttpFoundation\HeaderBag;
1819
use Symfony\Component\HttpFoundation\Request;
1920
use Symfony\Component\HttpFoundation\Response;
@@ -39,7 +40,7 @@ class TaggableStore implements StoreInterface
3940
private $purgeTagsHeader;
4041

4142
/**
42-
* @var TagAwareAdapter
43+
* @var TagAwareAdapterInterface
4344
*/
4445
private $cache;
4546

@@ -63,15 +64,47 @@ public function __construct($cacheDir, $purgeTagsHeader = PurgeTagsListener::DEF
6364
}
6465

6566
$this->purgeTagsHeader = $purgeTagsHeader;
66-
$this->cache = new TagAwareAdapter(new FilesystemAdapter('fos-http-cache', 0, $cacheDir));
6767

68-
if (SemaphoreStore::isSupported(false)) {
69-
$store = new SemaphoreStore();
70-
} else {
71-
$store = new FlockStore($cacheDir);
72-
}
68+
$this->setCache(new TagAwareAdapter(new FilesystemAdapter('fos-http-cache', 0, $cacheDir)));
69+
$this->setLockFactory(new Factory($this->getBestLocalLockStore($cacheDir)));
70+
}
7371

74-
$this->lockFactory = new Factory($store);
72+
/**
73+
* Use this method if you want to use a different cache implementation than
74+
* the one created in the constructor. Note that there are very good reasons
75+
* that the local adapters are used by default and you do not inject them in
76+
* the constructor but via setter injection instead. This is to protect you
77+
* as a developer! Only use this method if you're really sure your cache
78+
* implementation meets the needs of Symfony's HttpCache.
79+
*
80+
* @param TagAwareAdapterInterface $cache
81+
*
82+
* @return TaggableStore
83+
*/
84+
public function setCache(TagAwareAdapterInterface $cache)
85+
{
86+
$this->cache = $cache;
87+
88+
return $this;
89+
}
90+
91+
/**
92+
* Use this method if you want to use a different lock implementation than
93+
* the one created in the constructor. Note that there are very good reasons
94+
* that the local adapters are used by default and you do not inject them in
95+
* the constructor but via setter injection instead. This is to protect you
96+
* as a developer! Only use this method if you're really sure your lock
97+
* implementation meets the needs of Symfony's HttpCache.
98+
*
99+
* @param Factory $lockFactory
100+
*
101+
* @return TaggableStore
102+
*/
103+
public function setLockFactory(Factory $lockFactory)
104+
{
105+
$this->lockFactory = $lockFactory;
106+
107+
return $this;
75108
}
76109

77110
/**
@@ -392,4 +425,20 @@ private function restoreResponse(array $cacheData)
392425
$cacheData['headers']
393426
);
394427
}
428+
429+
/**
430+
* @param string $cache 80C5 Dir
431+
*
432+
* @return \Symfony\Component\Lock\StoreInterface
433+
*/
434+
private function getBestLocalLockStore($cacheDir)
435+
{
436+
if (SemaphoreStore::isSupported(false)) {
437+
$store = new SemaphoreStore();
438+
} else {
439+
$store = new FlockStore($cacheDir);
440+
}
441+
442+
return $store;
443+
}
395444
}

0 commit comments

Comments
 (0)
0