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 (!App\Mode::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();
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) {
118 if (empty($prefix) || strpos($key, $prefix) === 0) {
124 $this->out($count . ' keys found');
127 private function executeGet()
129 if (count($this->args) >= 2) {
130 $key = $this->getArgument(1);
131 $value = Core\Cache::get($key);
133 $this->out("{$key} => " . var_export($value, true));
135 throw new CommandArgsException('Too few arguments for get');
139 private function executeSet()
141 if (count($this->args) >= 3) {
142 $key = $this->getArgument(1);
143 $value = $this->getArgument(2);
144 $duration = intval($this->getArgument(3, Core\Cache::FIVE_MINUTES));
146 if (is_array(Core\Cache::get($key))) {
147 throw new RuntimeException("$key is an array and can't be set using this command.");
150 $result = Core\Cache::set($key, $value, $duration);
152 $this->out("{$key} <= " . Core\Cache::get($key));
154 $this->out("Unable to set {$key}");
157 throw new CommandArgsException('Too few arguments for set');
161 private function executeFlush()
163 $result = Core\Cache::clear();
165 $this->out('Cache successfully flushed');
167 $this->out('Unable to flush the cache');
171 private function executeClear()
173 $result = Core\Cache::clear(false);
175 $this->out('Cache successfully cleared');
177 $this->out('Unable to flush the cache');