3 namespace Friendica\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', '?'];
28 protected function getHelp()
31 console cache - Manage node cache
33 bin/console cache list [-h|--help|-?] [-v]
34 bin/console cache get <key> [-h|--help|-?] [-v]
35 bin/console cache set <key> <value> [-h|--help|-?] [-v]
36 bin/console cache flush [-h|--help|-?] [-v]
37 bin/console cache clear [-h|--help|-?] [-v]
40 bin/console cache list [<prefix>]
41 List all cache keys, optionally filtered by a prefix
43 bin/console cache get <key>
44 Shows the value of the provided cache key
46 bin/console cache set <key> <value> [<ttl>]
47 Sets the value of the provided cache key, optionally with the provided TTL (time to live) with a default of five minutes.
49 bin/console cache flush
50 Clears expired cache keys
52 bin/console cache clear
56 -h|--help|-? Show help information
57 -v Show more debug information.
62 public function __construct(App\Mode $appMode, array $argv = null)
64 parent::__construct($argv);
66 $this->appMode = $appMode;
69 protected function doExecute()
71 if ($this->getOption('v')) {
72 $this->out('Executable: ' . $this->executable);
73 $this->out('Class: ' . __CLASS__);
74 $this->out('Arguments: ' . var_export($this->args, true));
75 $this->out('Options: ' . var_export($this->options, true));
78 if (!$this->appMode->has(App\Mode::DBCONFIGAVAILABLE)) {
79 $this->out('Database isn\'t ready or populated yet, database cache won\'t be available');
84 if ($this->getOption('v')) {
85 $this->out('Cache Driver Name: ' . Core\Cache::$driver_name);
86 $this->out('Cache Driver Class: ' . Core\Cache::$driver_class);
89 switch ($this->getArgument(0)) {
100 $this->executeFlush();
103 $this->executeClear();
107 if (count($this->args) == 0) {
108 $this->out($this->getHelp());
115 private function executeList()
117 $prefix = $this->getArgument(1);
118 $keys = Core\Cache::getAllKeys($prefix);
120 if (empty($prefix)) {
121 $this->out('Listing all cache keys:');
123 $this->out('Listing all cache keys starting with "' . $prefix . '":');
127 foreach ($keys as $key) {
132 $this->out($count . ' keys found');
135 private function executeGet()
137 if (count($this->args) >= 2) {
138 $key = $this->getArgument(1);
139 $value = Core\Cache::get($key);
141 $this->out("{$key} => " . var_export($value, true));
143 throw new CommandArgsException('Too few arguments for get');
147 private function executeSet()
149 if (count($this->args) >= 3) {
150 $key = $this->getArgument(1);
151 $value = $this->getArgument(2);
152 $duration = intval($this->getArgument(3, Core\Cache::FIVE_MINUTES));
154 if (is_array(Core\Cache::get($key))) {
155 throw new RuntimeException("$key is an array and can't be set using this command.");
158 $result = Core\Cache::set($key, $value, $duration);
160 $this->out("{$key} <= " . Core\Cache::get($key));
162 $this->out("Unable to set {$key}");
165 throw new CommandArgsException('Too few arguments for set');
169 private function executeFlush()
171 $result = Core\Cache::clear();
173 $this->out('Cache successfully flushed');
175 $this->out('Unable to flush the cache');
179 private function executeClear()
181 $result = Core\Cache::clear(false);
183 $this->out('Cache successfully cleared');
185 $this->out('Unable to flush the cache');