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', '?'];
29 * @var string The cache driver name
31 private $cacheDriverName;
38 protected function getHelp()
41 console cache - Manage node cache
43 bin/console cache list [-h|--help|-?] [-v]
44 bin/console cache get <key> [-h|--help|-?] [-v]
45 bin/console cache set <key> <value> [-h|--help|-?] [-v]
46 bin/console cache flush [-h|--help|-?] [-v]
47 bin/console cache clear [-h|--help|-?] [-v]
50 bin/console cache list [<prefix>]
51 List all cache keys, optionally filtered by a prefix
53 bin/console cache get <key>
54 Shows the value of the provided cache key
56 bin/console cache set <key> <value> [<ttl>]
57 Sets the value of the provided cache key, optionally with the provided TTL (time to live) with a default of five minutes.
59 bin/console cache flush
60 Clears expired cache keys
62 bin/console cache clear
66 -h|--help|-? Show help information
67 -v Show more debug information.
72 public function __construct(App\Mode $appMode, ICache $cache, array $argv = null)
74 parent::__construct($argv);
76 $this->appMode = $appMode;
77 $this->cache = $cache;
80 protected function doExecute()
82 if ($this->getOption('v')) {
83 $this->out('Executable: ' . $this->executable);
84 $this->out('Class: ' . __CLASS__);
85 $this->out('Arguments: ' . var_export($this->args, true));
86 $this->out('Options: ' . var_export($this->options, true));
89 if (!$this->appMode->has(App\Mode::DBCONFIGAVAILABLE)) {
90 $this->out('Database isn\'t ready or populated yet, database cache won\'t be available');
93 if ($this->getOption('v')) {
94 $this->out('Cache Driver Name: ' . (string)$this->cache);
95 $this->out('Cache Driver Class: ' . get_class($this->cache));
98 switch ($this->getArgument(0)) {
100 $this->executeList();
109 $this->executeFlush();
112 $this->executeClear();
116 if (count($this->args) == 0) {
117 $this->out($this->getHelp());
124 private function executeList()
126 $prefix = $this->getArgument(1);
127 $keys = $this->cache->getAllKeys($prefix);
129 if (empty($prefix)) {
130 $this->out('Listing all cache keys:');
132 $this->out('Listing all cache keys starting with "' . $prefix . '":');
136 foreach ($keys as $key) {
141 $this->out($count . ' keys found');
144 private function executeGet()
146 if (count($this->args) >= 2) {
147 $key = $this->getArgument(1);
148 $value = $this->cache->get($key);
150 $this->out("{$key} => " . var_export($value, true));
152 throw new CommandArgsException('Too few arguments for get');
156 private function executeSet()
158 if (count($this->args) >= 3) {
159 $key = $this->getArgument(1);
160 $value = $this->getArgument(2);
161 $duration = intval($this->getArgument(3, ICache::FIVE_MINUTES));
163 if (is_array($this->cache->get($key))) {
164 throw new RuntimeException("$key is an array and can't be set using this command.");
167 $result = $this->cache->set($key, $value, $duration);
169 $this->out("{$key} <= " . $this->cache->get($key));
171 $this->out("Unable to set {$key}");
174 throw new CommandArgsException('Too few arguments for set');
178 private function executeFlush()
180 $result = $this->cache->clear();
182 $this->out('Cache successfully flushed');
184 $this->out('Unable to flush the cache');
188 private function executeClear()
190 $result = $this->cache->clear(false);
192 $this->out('Cache successfully cleared');
194 $this->out('Unable to flush the cache');