Skip to content

Commit f9bdb96

Browse files
committed
Merge pull request #8 from php-cache/command
Improved the cache flush command
2 parents 0b3856b + 890d08a commit f9bdb96

File tree

3 files changed

+70
-24
lines changed

3 files changed

+70
-24
lines changed

composer.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@
2424
}
2525
],
2626
"require": {
27-
"php": "^5.5|^7",
28-
"symfony/framework-bundle": "^2.6|^3.0",
29-
"psr/cache-implementation": "~1.0"
27+
"php": "^5.5|^7",
28+
"symfony/framework-bundle": "^2.6|^3.0",
29+
"cache/psr-6-doctrine-bridge": "^0.1",
30+
"cache/taggable-cache": "^0.1",
31+
"psr/cache-implementation": "~1.0"
3032
},
3133
"require-dev": {
3234
"phpunit/phpunit": "^5.1|^4.0",
33-
"cache/doctrine-adapter": "^0.1",
34-
"cache/taggable-cache": "^0.1"
35+
"cache/doctrine-adapter": "^0.1"
3536
},
3637
"suggest": {
3738
"cache/doctrine-adapter-bundle": "A bundle to register PSR-6 compliant services based on Doctrine cache"

src/Command/CacheFlushCommand.php

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,39 +11,65 @@
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;
1718
use Symfony\Component\Console\Output\OutputInterface;
1819
use Symfony\Component\Console\Input\InputArgument;
1920

2021
/**
21-
* Class CacheFlushCommand
22+
* Class CacheFlushCommand.
2223
*
2324
* @author Aaron Scherer <[email protected]>
2425
*/
2526
class CacheFlushCommand extends ContainerAwareCommand
2627
{
27-
2828
/**
2929
* {@inheritdoc}
3030
*/
3131
protected function configure()
3232
{
3333
$this->setName('cache:flush');
3434
$this->setDescription('Flushes the given cache');
35-
$this->addArgument('instance', InputArgument::REQUIRED, 'Which instance do you want to clean?');
35+
$this->addArgument('type', InputArgument::REQUIRED, 'Which type of cache do you want to clear?');
3636
}
3737

3838
/**
3939
* {@inheritdoc}
4040
*/
4141
protected function execute(InputInterface $input, OutputInterface $output)
4242
{
43-
$serviceName = sprintf('cache.instance.%s', $input->getArgument('instance'));
43+
$validTypes = ['session', 'routing', 'doctrine', 'all'];
44+
$type = $input->getArgument('type');
45+
if (!in_array($type, $validTypes)) {
46+
$output->writeln(sprintf('Type "%s" does not exist. Valid type are: %s', $type, implode(',', $validTypes)));
47+
}
48+
49+
if ($type === 'all') {
50+
foreach (['session', 'routing', 'doctrine'] as $type) {
51+
$this->clearCacheForType($type);
52+
}
53+
} else {
54+
$this->clearCacheForType($type);
55+
}
56+
}
57+
58+
/**
59+
* Clear the cache for a type.
60+
*
61+
* @param string $type
62+
*/
63+
private function clearCacheForType($type)
64+
{
65+
$serviceId = $this->getContainer()->getParameter(sprintf('cache.%s%.service_id', $type));
4466

4567
/** @var CacheItemPoolInterface $service */
46-
$service = $this->getContainer()->get($serviceName);
47-
$service->clear();
68+
$service = $this->getContainer()->get($serviceId);
69+
if ($service instanceof TaggablePoolInterface) {
70+
$service->clear([$type]);
71+
} else {
72+
$service->clear();
73+
}
4874
}
4975
}

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)