Skip to content
This repository was archived by the owner on Jan 8, 2024. It is now read-only.

Commit 545f199

Browse files
committed
Added CLI CRUD-like commands
1 parent 2749e14 commit 545f199

11 files changed

+551
-19
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ Or in your template:
6262
{% endcache %}
6363
</div>
6464
```
65+
66+
#### :computer: CLI command interactions
67+
@todo :|
68+
6569
#### :bulb: Introducing Cacheable Responses (V3 only)
6670
As of the V3 there's a new, easier and cleaner way to setup HTTP cache to decrease your server bandwidth along with your CPU load: Cacheable Responses.
6771
And it's pretty easy to implement:
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
use Symfony\Component\Console\Output\OutputInterface;
2626
use Symfony\Component\Console\Style\SymfonyStyle;
2727

28-
class PhpfastcacheCommand extends Command
28+
class PhpfastcacheClearCommand extends Command
2929
{
3030
/**
3131
* @var \Phpfastcache\Bundle\Service\Phpfastcache
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
/**
4+
*
5+
* This file is part of phpFastCache.
6+
*
7+
* @license MIT License (MIT)
8+
*
9+
* For full copyright and license information, please see the docs/CREDITS.txt file.
10+
*
11+
* @author Georges.L (Geolim4) <contact@geolim4.com>
12+
* @author PastisD https://github.com/PastisD
13+
* @author Khoa Bui (khoaofgod) <khoaofgod@gmail.com> http://www.phpfastcache.com
14+
*
15+
*/
16+
declare(strict_types=1);
17+
18+
namespace Phpfastcache\Bundle\Command;
19+
20+
use Phpfastcache\Bundle\Service\Phpfastcache;
21+
use Phpfastcache\Exceptions\PhpfastcacheDriverCheckException;
22+
use Symfony\Component\Console\Command\Command;
23+
use Symfony\Component\Console\Input\InputArgument;
24+
use Symfony\Component\Console\Input\InputInterface;
25+
use Symfony\Component\Console\Output\OutputInterface;
26+
use Symfony\Component\Console\Style\SymfonyStyle;
27+
28+
class PhpfastcacheDelCommand extends Command
29+
{
30+
/**
31+
* @var \Phpfastcache\Bundle\Service\Phpfastcache
32+
*/
33+
protected $phpfastcache;
34+
35+
/**
36+
* @var
37+
*/
38+
protected $parameters;
39+
40+
/**
41+
* PhpfastcacheCommand constructor.
42+
* @param \Phpfastcache\Bundle\Service\Phpfastcache $phpfastcache
43+
* @param $parameters
44+
*/
45+
public function __construct(Phpfastcache $phpfastcache, $parameters)
46+
{
47+
$this->phpfastcache = $phpfastcache;
48+
$this->parameters = $parameters;
49+
50+
parent::__construct();
51+
}
52+
53+
protected function configure()
54+
{
55+
$this
56+
->setName('phpfastcache:del')
57+
->setAliases([ 'phpfastcache:delete', 'phpfastcache:rem', 'phpfastcache:remove'])
58+
->setDescription('Delete phpfastcache cache value')
59+
->addArgument(
60+
'driver',
61+
InputArgument::REQUIRED,
62+
'Cache name to clear'
63+
)
64+
->addArgument(
65+
'key',
66+
InputArgument::REQUIRED,
67+
'Cache key'
68+
);
69+
}
70+
71+
/**
72+
* @param \Symfony\Component\Console\Input\InputInterface $input
73+
* @param \Symfony\Component\Console\Output\OutputInterface $output
74+
* @return int|null|void
75+
*/
76+
protected function execute(InputInterface $input, OutputInterface $output)
77+
{
78+
$io = new SymfonyStyle($input, $output);
79+
$caches = $this->parameters;
80+
81+
$driver = $input->getArgument('driver');
82+
$cacheKey = $input->getArgument('key');
83+
84+
if (\array_key_exists($driver, $caches[ 'drivers' ])) {
85+
$io->section($driver);
86+
$driverInstance = $this->phpfastcache->get($driver);
87+
$cacheItem = $driverInstance->getItem($cacheKey);
88+
89+
if(!$cacheItem->isHit()){
90+
$io->note(\sprintf('Cache item "%s" does not exists in the cache', $cacheKey));
91+
}else{
92+
$driverInstance->deleteItem($cacheItem->getKey());
93+
$io->success(\sprintf('Cache item "%s" has been deleted from cache', $cacheKey));
94+
}
95+
} else {
96+
$io->error("Cache instance {$driver} does not exists");
97+
}
98+
}
99+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
/**
4+
*
5+
* This file is part of phpFastCache.
6+
*
7+
* @license MIT License (MIT)
8+
*
9+
* For full copyright and license information, please see the docs/CREDITS.txt file.
10+
*
11+
* @author Georges.L (Geolim4) <contact@geolim4.com>
12+
* @author PastisD https://github.com/PastisD
13+
* @author Khoa Bui (khoaofgod) <khoaofgod@gmail.com> http://www.phpfastcache.com
14+
*
15+
*/
16+
declare(strict_types=1);
17+
18+
namespace Phpfastcache\Bundle\Command;
19+
20+
use Phpfastcache\Bundle\Service\Phpfastcache;
21+
use Phpfastcache\Exceptions\PhpfastcacheDriverCheckException;
22+
use Symfony\Component\Console\Command\Command;
23+
use Symfony\Component\Console\Input\InputArgument;
24+
use Symfony\Component\Console\Input\InputInterface;
25+
use Symfony\Component\Console\Output\OutputInterface;
26+
use Symfony\Component\Console\Style\SymfonyStyle;
27+
28+
class PhpfastcacheGetCommand extends Command
29+
{
30+
/**
31+
* @var \Phpfastcache\Bundle\Service\Phpfastcache
32+
*/
33+
protected $phpfastcache;
34+
35+
/**
36+
* @var
37+
*/
38+
protected $parameters;
39+
40+
/**
41+
* PhpfastcacheCommand constructor.
42+
* @param \Phpfastcache\Bundle\Service\Phpfastcache $phpfastcache
43+
* @param $parameters
44+
*/
45+
public function __construct(Phpfastcache $phpfastcache, $parameters)
46+
{
47+
$this->phpfastcache = $phpfastcache;
48+
$this->parameters = $parameters;
49+
50+
parent::__construct();
51+
}
52+
53+
protected function configure()
54+
{
55+
$this
56+
->setName('phpfastcache:get')
57+
->setDescription('Get phpfastcache cache value')
58+
->addArgument(
59+
'driver',
60+
InputArgument::REQUIRED,
61+
'Cache name to clear'
62+
)
63+
->addArgument(
64+
'key',
65+
InputArgument::REQUIRED,
66+
'Cache key'
67+
);
68+
}
69+
70+
/**
71+
* @param \Symfony\Component\Console\Input\InputInterface $input
72+
* @param \Symfony\Component\Console\Output\OutputInterface $output
73+
* @return int|null|void
74+
*/
75+
protected function execute(InputInterface $input, OutputInterface $output)
76+
{
77+
$io = new SymfonyStyle($input, $output);
78+
$caches = $this->parameters;
79+
80+
$driver = $input->getArgument('driver');
81+
$cacheKey = $input->getArgument('key');
82+
83+
if (\array_key_exists($driver, $caches[ 'drivers' ])) {
84+
$io->section($driver);
85+
$cacheItem = $this->phpfastcache->get($driver)->getItem($cacheKey);
86+
87+
if($cacheItem->isHit()){
88+
$cacheItemValue = $cacheItem->get();
89+
if(!\is_object($cacheItemValue)){
90+
ob_start();
91+
var_dump($cacheItemValue);
92+
$content = ob_get_contents();
93+
ob_end_clean();
94+
$io->write('<bg=green;fg=black>[HIT]</> ' . $content);
95+
}else{
96+
$io->write('<bg=green;fg=black>[HIT]</> (object) ' . \get_class($cacheItemValue));
97+
}
98+
$io->write('This item will expires in <fg=green>' . $cacheItem->getTtl() .'</> second(s)');
99+
}else{
100+
$io->write('<bg=yellow;fg=red>[MISS]</> The cache item "' . $cacheKey . '" does not (yet) exists !');
101+
}
102+
} else {
103+
$io->error("Cache instance {$driver} does not exists");
104+
}
105+
}
106+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
3+
/**
4+
*
5+
* This file is part of phpFastCache.
6+
*
7+
* @license MIT License (MIT)
8+
*
9+
* For full copyright and license information, please see the docs/CREDITS.txt file.
10+
*
11+
* @author Georges.L (Geolim4) <contact@geolim4.com>
12+
* @author PastisD https://github.com/PastisD
13+
* @author Khoa Bui (khoaofgod) <khoaofgod@gmail.com> http://www.phpfastcache.com
14+
*
15+
*/
16+
declare(strict_types=1);
17+
18+
namespace Phpfastcache\Bundle\Command;
19+
20+
use Phpfastcache\Bundle\Service\Phpfastcache;
21+
use Phpfastcache\Exceptions\PhpfastcacheDriverCheckException;
22+
use Symfony\Component\Console\Command\Command;
23+
use Symfony\Component\Console\Input\InputArgument;
24+
use Symfony\Component\Console\Input\InputInterface;
25+
use Symfony\Component\Console\Output\OutputInterface;
26+
use Symfony\Component\Console\Style\SymfonyStyle;
27+
28+
class PhpfastcacheSetCommand extends Command
29+
{
30+
/**
31+
* @var \Phpfastcache\Bundle\Service\Phpfastcache
32+
*/
33+
protected $phpfastcache;
34+
35+
/**
36+
* @var
37+
*/
38+
protected $parameters;
39+
40+
/**
41+
* PhpfastcacheCommand constructor.
42+
* @param \Phpfastcache\Bundle\Service\Phpfastcache $phpfastcache
43+
* @param $parameters
44+
*/
45+
public function __construct(Phpfastcache $phpfastcache, $parameters)
46+
{
47+
$this->phpfastcache = $phpfastcache;
48+
$this->parameters = $parameters;
49+
50+
parent::__construct();
51+
}
52+
53+
protected function configure()
54+
{
55+
$this
56+
->setName('phpfastcache:set')
57+
->setDescription('Set phpfastcache cache value')
58+
->addArgument(
59+
'driver',
60+
InputArgument::REQUIRED,
61+
'Cache name to clear'
62+
)
63+
->addArgument(
64+
'key',
65+
InputArgument::REQUIRED,
66+
'Cache key'
67+
)
68+
->addArgument(
69+
'value',
70+
InputArgument::REQUIRED,
71+
'Cache value'
72+
)
73+
->addArgument(
74+
'ttl',
75+
InputArgument::OPTIONAL,
76+
'Cache ttl (in second)'
77+
);
78+
}
79+
80+
/**
81+
* @param \Symfony\Component\Console\Input\InputInterface $input
82+
* @param \Symfony\Component\Console\Output\OutputInterface $output
83+
* @return int|null|void
84+
*/
85+
protected function execute(InputInterface $input, OutputInterface $output)
86+
{
87+
$io = new SymfonyStyle($input, $output);
88+
$caches = $this->parameters;
89+
90+
$driver = $input->getArgument('driver');
91+
$cacheKey = $input->getArgument('key');
92+
$cacheValue = $input->getArgument('value');
93+
$cacheTtl = $input->getArgument('ttl');
94+
95+
if (\array_key_exists($driver, $caches[ 'drivers' ])) {
96+
$io->section($driver);
97+
$driverInstance = $this->phpfastcache->get($driver);
98+
$cacheItem = $driverInstance->getItem($cacheKey);
99+
$cacheItem->set($cacheValue);
100+
101+
if($cacheTtl){
102+
if(\is_numeric($cacheTtl)){
103+
$cacheItem->expiresAfter($cacheTtl);
104+
}else{
105+
$io->error(\sprintf('Invalid ttl value format "%s", must be a valid integer, aborting...', $cacheTtl));
106+
return;
107+
}
108+
}
109+
110+
$io->success(\sprintf('Cache item "%s" set to "%s" for %d seconds', $cacheKey, $cacheValue, $cacheItem->getTtl()));
111+
112+
$driverInstance->save($cacheItem);
113+
} else {
114+
$io->error("Cache instance {$driver} does not exists");
115+
}
116+
}
117+
}

src/Resources/config/services.yml

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ services:
55
arguments:
66
- '%phpfastcache%'
77
- '@?debug.stopwatch'
8-
Phpfastcache\Bundle\Command\PhpfastcacheCommand:
9-
arguments: ['@Phpfastcache\Bundle\Service\Phpfastcache', '%phpfastcache%']
10-
tags:
11-
- { name: console.command }
128
Phpfastcache\Bundle\Twig\HumanReadableExtension\Extension:
139
tags:
1410
- { name: twig.extension }
@@ -29,3 +25,20 @@ services:
2925
- '@Phpfastcache\Bundle\Twig\CacheExtension\CacheStrategy\LifetimeCacheStrategy'
3026
tags:
3127
- { name: twig.extension }
28+
# CLI CRUD-like commands
29+
Phpfastcache\Bundle\Command\PhpfastcacheClearCommand:
30+
arguments: ['@Phpfastcache\Bundle\Service\Phpfastcache', '%phpfastcache%']
31+
tags:
32+
- { name: console.command }
33+
Phpfastcache\Bundle\Command\PhpfastcacheGetCommand:
34+
arguments: ['@Phpfastcache\Bundle\Service\Phpfastcache', '%phpfastcache%']
35+
tags:
36+
- { name: console.command }
37+
Phpfastcache\Bundle\Command\PhpfastcacheSetCommand:
38+
arguments: ['@Phpfastcache\Bundle\Service\Phpfastcache', '%phpfastcache%']
39+
tags:
40+
- { name: console.command }
41+
Phpfastcache\Bundle\Command\PhpfastcacheDelCommand:
42+
arguments: ['@Phpfastcache\Bundle\Service\Phpfastcache', '%phpfastcache%']
43+
tags:
44+
- { name: console.command }

0 commit comments

Comments
 (0)