3 namespace Friendica\Console;
5 use Asika\SimpleConsole\CommandArgsException;
7 use Friendica\Core\Cache\ICache;
11 * @brief tool to access the cache from the CLI
13 * With this script you can access the cache of your node from the CLI.
14 * You can read current values stored in the cache and set new values
17 * @author Hypolite Petovan <hypolite@mrpetovan.com>
19 class Cache extends \Asika\SimpleConsole\Console
21 protected $helpOptions = ['h', 'help', '?'];
33 protected function getHelp()
36 console cache - Manage node cache
38 bin/console cache list [-h|--help|-?] [-v]
39 bin/console cache get <key> [-h|--help|-?] [-v]
40 bin/console cache set <key> <value> [-h|--help|-?] [-v]
41 bin/console cache flush [-h|--help|-?] [-v]
42 bin/console cache clear [-h|--help|-?] [-v]
45 bin/console cache list [<prefix>]
46 List all cache keys, optionally filtered by a prefix
48 bin/console cache get <key>
49 Shows the value of the provided cache key
51 bin/console cache set <key> <value> [<ttl>]
52 Sets the value of the provided cache key, optionally with the provided TTL (time to live) with a default of five minutes.
54 bin/console cache flush
55 Clears expired cache keys
57 bin/console cache clear
61 -h|--help|-? Show help information
62 -v Show more debug information.
67 public function __construct(App\Mode $appMode, ICache $cache, array $argv = null)
69 parent::__construct($argv);
71 $this->appMode = $appMode;
72 $this->cache = $cache;
75 protected function doExecute()
77 if ($this->getOption('v')) {
78 $this->out('Executable: ' . $this->executable);
79 $this->out('Class: ' . __CLASS__);
80 $this->out('Arguments: ' . var_export($this->args, true));
81 $this->out('Options: ' . var_export($this->options, true));
84 if (!$this->appMode->has(App\Mode::DBCONFIGAVAILABLE)) {
85 $this->out('Database isn\'t ready or populated yet, database cache won\'t be available');
88 if ($this->getOption('v')) {
89 $this->out('Cache Driver Name: ' . (string)$this->cache);
90 $this->out('Cache Driver Class: ' . get_class($this->cache));
93 switch ($this->getArgument(0)) {
104 $this->executeFlush();
107 $this->executeClear();
111 if (count($this->args) == 0) {
112 $this->out($this->getHelp());
119 private function executeList()
121 $prefix = $this->getArgument(1);
122 $keys = $this->cache->getAllKeys($prefix);
124 if (empty($prefix)) {
125 $this->out('Listing all cache keys:');
127 $this->out('Listing all cache keys starting with "' . $prefix . '":');
131 foreach ($keys as $key) {
136 $this->out($count . ' keys found');
139 private function executeGet()
141 if (count($this->args) >= 2) {
142 $key = $this->getArgument(1);
143 $value = $this->cache->get($key);
145 $this->out("{$key} => " . var_export($value, true));
147 throw new CommandArgsException('Too few arguments for get');
151 private function executeSet()
153 if (count($this->args) >= 3) {
154 $key = $this->getArgument(1);
155 $value = $this->getArgument(2);
156 $duration = intval($this->getArgument(3, ICache::FIVE_MINUTES));
158 if (is_array($this->cache->get($key))) {
159 throw new RuntimeException("$key is an array and can't be set using this command.");
162 $result = $this->cache->set($key, $value, $duration);
164 $this->out("{$key} <= " . $this->cache->get($key));
166 $this->out("Unable to set {$key}");
169 throw new CommandArgsException('Too few arguments for set');
173 private function executeFlush()
175 $result = $this->cache->clear();
177 $this->out('Cache successfully flushed');
179 $this->out('Unable to flush the cache');
183 private function executeClear()
185 $result = $this->cache->clear(false);
187 $this->out('Cache successfully cleared');
189 $this->out('Unable to flush the cache');