Skip to content

Commit 41dbfee

Browse files
committed
Make sure the flush command and the session handler supports tags
1 parent 0b3856b commit 41dbfee

File tree

2 files changed

+47
-17
lines changed

2 files changed

+47
-17
lines changed

src/Command/CacheFlushCommand.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Cache\CacheBundle\Command;
1313

14+
use Cache\Taggable\TaggablePoolInterface;
1415
use Psr\Cache\CacheItemPoolInterface;
1516
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
1617
use Symfony\Component\Console\Input\InputInterface;
@@ -32,18 +33,28 @@ protected function configure()
3233
{
3334
$this->setName('cache:flush');
3435
$this->setDescription('Flushes the given cache');
35-
$this->addArgument('instance', InputArgument::REQUIRED, 'Which instance do you want to clean?');
36+
$this->addArgument('type', InputArgument::REQUIRED, 'Which type of cache do you want to clear?');
3637
}
3738

3839
/**
3940
* {@inheritdoc}
4041
*/
4142
protected function execute(InputInterface $input, OutputInterface $output)
4243
{
43-
$serviceName = sprintf('cache.instance.%s', $input->getArgument('instance'));
44+
$validTypes = ['session', 'routing', 'doctrine'];
45+
$type = $input->getArgument('type');
46+
if (!in_array($type, $validTypes)) {
47+
$output->writeln(sprintf('Type "%s" does not exist. Valid type are: %s', $type, implode(',', $validTypes)));
48+
}
49+
50+
$serviceId = $this->getContainer()->getParameter(sprintf('cache.%s%.service_id', $type));
4451

4552
/** @var CacheItemPoolInterface $service */
46-
$service = $this->getContainer()->get($serviceName);
47-
$service->clear();
53+
$service = $this->getContainer()->get($serviceId);
54+
if ($service instanceof TaggablePoolInterface) {
55+
$service->clear([$type]);
56+
} else {
57+
$service->clear();
58+
}
4859
}
4960
}

src/Session/SessionHandler.php

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111

1212
namespace Cache\CacheBundle\Session;
1313

14+
use Cache\Taggable\TaggablePoolInterface;
1415
use Psr\Cache\CacheItemPoolInterface;
1516

1617
/**
17-
* Class SessionHandler
18+
* Class SessionHandler.
1819
*
1920
* @author Aaron Scherer <[email protected]>
2021
*/
@@ -26,7 +27,7 @@ class SessionHandler implements \SessionHandlerInterface
2627
private $cache;
2728

2829
/**
29-
* @var integer Time to live in seconds
30+
* @var int Time to live in seconds
3031
*/
3132
private $ttl;
3233

@@ -43,40 +44,40 @@ class SessionHandler implements \SessionHandlerInterface
4344
* * expiretime: The time to live in seconds
4445
*
4546
* @param CacheItemPoolInterface $cache A Cache instance
46-
* @param array $options An associative array of cache options
47+
* @param array $options An associative array of cache options
4748
*
4849
* @throws \InvalidArgumentException When unsupported options are passed
4950
*/
5051
public function __construct(CacheItemPoolInterface $cache, array $options = array())
5152
{
5253
$this->cache = $cache;
5354

54-
$this->ttl = isset($options['ttl']) ? (int) $options['ttl'] : 86400;
55+
$this->ttl = isset($options['ttl']) ? (int) $options['ttl'] : 86400;
5556
$this->prefix = isset($options['prefix']) ? $options['prefix'] : 'sf2ses_';
5657
}
5758

5859
/**
59-
* {@inheritDoc}
60+
* {@inheritdoc}
6061
*/
6162
public function open($savePath, $sessionName)
6263
{
6364
return true;
6465
}
6566

6667
/**
67-
* {@inheritDoc}
68+
* {@inheritdoc}
6869
*/
6970
public function close()
7071
{
7172
return true;
7273
}
7374

7475
/**
75-
* {@inheritDoc}
76+
* {@inheritdoc}
7677
*/
7778
public function read($sessionId)
7879
{
79-
$item = $this->cache->getItem($this->prefix.$sessionId);
80+
$item = $this->getCacheItem($sessionId);
8081
if ($item->isHit()) {
8182
return $item->get();
8283
}
@@ -85,31 +86,49 @@ public function read($sessionId)
8586
}
8687

8788
/**
88-
* {@inheritDoc}
89+
* {@inheritdoc}
8990
*/
9091
public function write($sessionId, $data)
9192
{
92-
$item = $this->cache->getItem($this->prefix . $sessionId);
93+
$item = $this->getCacheItem($sessionId);
9394
$item->set($data)
9495
->expiresAfter($this->ttl);
9596

9697
return $this->cache->save($item);
9798
}
9899

99100
/**
100-
* {@inheritDoc}
101+
* {@inheritdoc}
101102
*/
102103
public function destroy($sessionId)
103104
{
104-
return $this->cache->deleteItem($this->prefix . $sessionId);
105+
if ($this->cache instanceof TaggablePoolInterface) {
106+
return $this->cache->deleteItem($this->prefix.$sessionId, ['session']);
107+
}
108+
109+
return $this->cache->deleteItem($this->prefix.$sessionId);
105110
}
106111

107112
/**
108-
* {@inheritDoc}
113+
* {@inheritdoc}
109114
*/
110115
public function gc($lifetime)
111116
{
112117
// not required here because cache will auto expire the records anyhow.
113118
return true;
114119
}
120+
121+
/**
122+
* @param $sessionId
123+
*
124+
* @return \Psr\Cache\CacheItemInterface
125+
*/
126+
private function getCacheItem($sessionId)
127+
{
128+
if ($this->cache instanceof TaggablePoolInterface) {
129+
return $this->cache->getItem($this->prefix.$sessionId, ['session']);
130+
}
131+
132+
return $this->cache->getItem($this->prefix.$sessionId);
133+
}
115134
}

0 commit comments

Comments
 (0)