3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Console;
24 use Asika\SimpleConsole\CommandArgsException;
26 use Friendica\Core\Cache\Duration;
27 use Friendica\Core\Cache\ICache;
31 * tool to access the cache from the CLI
33 * With this script you can access the cache of your node from the CLI.
34 * You can read current values stored in the cache and set new values
37 class Cache extends \Asika\SimpleConsole\Console
39 protected $helpOptions = ['h', 'help', '?'];
51 protected function getHelp()
54 console cache - Manage node cache
56 bin/console cache list [-h|--help|-?] [-v]
57 bin/console cache get <key> [-h|--help|-?] [-v]
58 bin/console cache set <key> <value> [-h|--help|-?] [-v]
59 bin/console cache flush [-h|--help|-?] [-v]
60 bin/console cache clear [-h|--help|-?] [-v]
63 bin/console cache list [<prefix>]
64 List all cache keys, optionally filtered by a prefix
66 bin/console cache get <key>
67 Shows the value of the provided cache key
69 bin/console cache set <key> <value> [<ttl>]
70 Sets the value of the provided cache key, optionally with the provided TTL (time to live) with a default of five minutes.
72 bin/console cache flush
73 Clears expired cache keys
75 bin/console cache clear
79 -h|--help|-? Show help information
80 -v Show more debug information.
85 public function __construct(App\Mode $appMode, ICache $cache, array $argv = null)
87 parent::__construct($argv);
89 $this->appMode = $appMode;
90 $this->cache = $cache;
93 protected function doExecute()
95 if ($this->getOption('v')) {
96 $this->out('Executable: ' . $this->executable);
97 $this->out('Class: ' . __CLASS__);
98 $this->out('Arguments: ' . var_export($this->args, true));
99 $this->out('Options: ' . var_export($this->options, true));
102 if (!$this->appMode->has(App\Mode::DBCONFIGAVAILABLE)) {
103 $this->out('Database isn\'t ready or populated yet, database cache won\'t be available');
106 if ($this->getOption('v')) {
107 $this->out('Cache Driver Name: ' . $this->cache->getName());
108 $this->out('Cache Driver Class: ' . get_class($this->cache));
111 switch ($this->getArgument(0)) {
113 $this->executeList();
122 $this->executeFlush();
125 $this->executeClear();
129 if (count($this->args) == 0) {
130 $this->out($this->getHelp());
137 private function executeList()
139 $prefix = $this->getArgument(1);
140 $keys = $this->cache->getAllKeys($prefix);
142 if (empty($prefix)) {
143 $this->out('Listing all cache keys:');
145 $this->out('Listing all cache keys starting with "' . $prefix . '":');
149 foreach ($keys as $key) {
154 $this->out($count . ' keys found');
157 private function executeGet()
159 if (count($this->args) >= 2) {
160 $key = $this->getArgument(1);
161 $value = $this->cache->get($key);
163 $this->out("{$key} => " . var_export($value, true));
165 throw new CommandArgsException('Too few arguments for get');
169 private function executeSet()
171 if (count($this->args) >= 3) {
172 $key = $this->getArgument(1);
173 $value = $this->getArgument(2);
174 $duration = intval($this->getArgument(3, Duration::FIVE_MINUTES));
176 if (is_array($this->cache->get($key))) {
177 throw new RuntimeException("$key is an array and can't be set using this command.");
180 $result = $this->cache->set($key, $value, $duration);
182 $this->out("{$key} <= " . $this->cache->get($key));
184 $this->out("Unable to set {$key}");
187 throw new CommandArgsException('Too few arguments for set');
191 private function executeFlush()
193 $result = $this->cache->clear();
195 $this->out('Cache successfully flushed');
197 $this->out('Unable to flush the cache');
201 private function executeClear()
203 $result = $this->cache->clear(false);
205 $this->out('Cache successfully cleared');
207 $this->out('Unable to flush the cache');