|
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) <[email protected]> |
|
12
|
|
|
* @author PastisD https://github.com/PastisD |
|
13
|
|
|
* @author Khoa Bui (khoaofgod) <[email protected]> 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
|
|
|
} |