3 namespace Friendica\Core\Console;
5 use Asika\SimpleConsole\CommandArgsException;
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', '?'];
23 protected function getHelp()
26 console cache - Manage node cache
28 bin/console cache list [-h|--help|-?] [-v]
29 bin/console cache get <key> [-h|--help|-?] [-v]
30 bin/console cache set <key> <value> [-h|--help|-?] [-v]
31 bin/console cache flush [-h|--help|-?] [-v]
32 bin/console cache clear [-h|--help|-?] [-v]
35 bin/console cache list [<prefix>]
36 List all cache keys, optionally filtered by a prefix
38 bin/console cache get <key>
39 Shows the value of the provided cache key
41 bin/console cache set <key> <value> [<ttl>]
42 Sets the value of the provided cache key, optionally with the provided TTL (time to live) with a default of five minutes.
44 bin/console cache flush
45 Clears expired cache keys
47 bin/console cache clear
51 -h|--help|-? Show help information
52 -v Show more debug information.
57 protected function doExecute()
59 $a = \Friendica\BaseObject::getApp();
61 if ($this->getOption('v')) {
62 $this->out('Executable: ' . $this->executable);
63 $this->out('Class: ' . __CLASS__);
64 $this->out('Arguments: ' . var_export($this->args, true));
65 $this->out('Options: ' . var_export($this->options, true));
68 if ($a->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
69 $this->out('Database isn\'t ready or populated yet, database cache won\'t be available');
74 if ($this->getOption('v')) {
75 $this->out('Cache Driver Name: ' . Core\Cache::$driver_name);
76 $this->out('Cache Driver Class: ' . Core\Cache::$driver_class);
79 switch ($this->getArgument(0)) {
90 $this->executeFlush();
93 $this->executeClear();
97 if (count($this->args) == 0) {
98 $this->out($this->getHelp());
105 private function executeList()
107 $prefix = $this->getArgument(1);
108 $keys = Core\Cache::getAllKeys($prefix);
110 if (empty($prefix)) {
111 $this->out('Listing all cache keys:');
113 $this->out('Listing all cache keys starting with "' . $prefix . '":');
117 foreach ($keys as $key) {
122 $this->out($count . ' keys found');
125 private function executeGet()
127 if (count($this->args) >= 2) {
128 $key = $this->getArgument(1);
129 $value = Core\Cache::get($key);
131 $this->out("{$key} => " . var_export($value, true));
133 throw new CommandArgsException('Too few arguments for get');
137 private function executeSet()
139 if (count($this->args) >= 3) {
140 $key = $this->getArgument(1);
141 $value = $this->getArgument(2);
142 $duration = intval($this->getArgument(3, Core\Cache::FIVE_MINUTES));
144 if (is_array(Core\Cache::get($key))) {
145 throw new RuntimeException("$key is an array and can't be set using this command.");
148 $result = Core\Cache::set($key, $value, $duration);
150 $this->out("{$key} <= " . Core\Cache::get($key));
152 $this->out("Unable to set {$key}");
155 throw new CommandArgsException('Too few arguments for set');
159 private function executeFlush()
161 $result = Core\Cache::clear();
163 $this->out('Cache successfully flushed');
165 $this->out('Unable to flush the cache');
169 private function executeClear()
171 $result = Core\Cache::clear(false);
173 $this->out('Cache successfully cleared');
175 $this->out('Unable to flush the cache');