Skip to content

Commit 7ef8f25

Browse files
committed
Adding TTL to CacheUrlMatcher
1 parent 3172b9a commit 7ef8f25

File tree

4 files changed

+36
-30
lines changed

4 files changed

+36
-30
lines changed

src/DependencyInjection/Compiler/RouterCompilerPass.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\DependencyInjection\Reference;
1515

1616
/**
17+
* @author Aaron Scherer <[email protected]>
1718
* @author Tobias Nyholm <[email protected]>
1819
*/
1920
class RouterCompilerPass extends BaseCompilerPass
@@ -23,7 +24,7 @@ class RouterCompilerPass extends BaseCompilerPass
2324
*/
2425
protected function prepare()
2526
{
26-
$router = $this->container->getParameter($this->getAlias() . '.router');
27+
$router = $this->container->getParameter($this->getAlias().'.router');
2728

2829
if (!$router['enabled']) {
2930
return;

src/Routing/Matcher/CacheUrlMatcher.php

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111

1212
namespace Cache\CacheBundle\Routing\Matcher;
1313

14-
use Aequasi\Cache\CachePool;
1514
use Psr\Cache\CacheItemPoolInterface;
1615
use Symfony\Component\Routing\Matcher\UrlMatcher;
16+
use Symfony\Component\Routing\RequestContext;
17+
use Symfony\Component\Routing\RouteCollection;
1718

1819
/**
1920
* Class CacheUrlMatcher
@@ -22,12 +23,34 @@
2223
*/
2324
class CacheUrlMatcher extends UrlMatcher
2425
{
25-
const CACHE_LIFETIME = 604800; // a week
26-
2726
/**
2827
* @var CacheItemPoolInterface
2928
*/
30-
protected $cache;
29+
protected $cachePool;
30+
31+
/**
32+
* @type int
33+
*/
34+
protected $ttl;
35+
36+
/**
37+
* CacheUrlMatcher constructor.
38+
*
39+
* @param CacheItemPoolInterface $cachePool
40+
* @param int $ttl
41+
* @param RouteCollection $routes
42+
* @param RequestContext $context
43+
*/
44+
public function __construct(
45+
CacheItemPoolInterface $cachePool,
46+
$ttl,
47+
RouteCollection $routes,
48+
RequestContext $context
49+
) {
50+
$this->cachePool = $cachePool;
51+
$this->ttl = $ttl;
52+
parent::__construct($routes, $context);
53+
}
3154

3255
/**
3356
* @param string $pathInfo
@@ -38,33 +61,17 @@ public function match($pathInfo)
3861
{
3962
$host = strtr($this->context->getHost(), '.', '_');
4063
$method = strtolower($this->context->getMethod());
41-
$key = 'route_' . $method . '_' . $host . '_' . $pathInfo;
64+
$key = 'route_'.$method.'_'.$host.'_'.$pathInfo;
4265

43-
if ($this->cache->hasItem($key)) {
44-
return $this->cache->getItem($key)->get();
66+
if ($this->cachePool->hasItem($key)) {
67+
return $this->cachePool->getItem($key)->get();
4568
}
4669

4770
$match = parent::match($pathInfo);
48-
$item = $this->cache->getItem($key);
49-
$item->set($match)
50-
->expiresAfter(self::CACHE_LIFETIME);
71+
$item = $this->cachePool->getItem($key);
72+
$item->set($match)
73+
->expiresAfter($this->ttl);
5174

5275
return $match;
5376
}
54-
55-
/**
56-
* @param CacheItemPoolInterface $cache
57-
*/
58-
public function setCache(CacheItemPoolInterface $cache)
59-
{
60-
$this->cache = $cache;
61-
}
62-
63-
/**
64-
* @return CacheItemPoolInterface
65-
*/
66-
public function getCache()
67-
{
68-
return $this->cache;
69-
}
7077
}

src/Routing/Router.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ public function getMatcher()
4949
return $this->matcher;
5050
}
5151

52-
$matcher = new CacheUrlMatcher($this->getRouteCollection(), $this->context);
53-
$matcher->setCache($this->cache);
52+
$matcher = new CacheUrlMatcher($this->cache, $this->ttl, $this->getRouteCollection(), $this->context);
5453

5554
return $this->matcher = $matcher;
5655
}

src/Session/SessionHandler.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Cache\CacheBundle\Session;
1313

14-
use Doctrine\Common\Cache\Cache;
1514
use Psr\Cache\CacheItemPoolInterface;
1615

1716
/**

0 commit comments

Comments
 (0)