3 namespace Friendica\Console;
5 use Asika\SimpleConsole\CommandArgsException;
7 use Friendica\Core\Cache\Cache as CacheClass;
8 use Friendica\Core\Cache\ICache;
12 * @brief tool to access the cache from the CLI
14 * With this script you can access the cache of your node from the CLI.
15 * You can read current values stored in the cache and set new values
18 * @author Hypolite Petovan <hypolite@mrpetovan.com>
20 class Cache extends \Asika\SimpleConsole\Console
22 protected $helpOptions = ['h', 'help', '?'];
34 protected function getHelp()
37 console cache - Manage node cache
39 bin/console cache list [-h|--help|-?] [-v]
40 bin/console cache get <key> [-h|--help|-?] [-v]
41 bin/console cache set <key> <value> [-h|--help|-?] [-v]
42 bin/console cache flush [-h|--help|-?] [-v]
43 bin/console cache clear [-h|--help|-?] [-v]
46 bin/console cache list [<prefix>]
47 List all cache keys, optionally filtered by a prefix
49 bin/console cache get <key>
50 Shows the value of the provided cache key
52 bin/console cache set <key> <value> [<ttl>]
53 Sets the value of the provided cache key, optionally with the provided TTL (time to live) with a default of five minutes.
55 bin/console cache flush
56 Clears expired cache keys
58 bin/console cache clear
62 -h|--help|-? Show help information
63 -v Show more debug information.
68 public function __construct(App\Mode $appMode, ICache $cache, array $argv = null)
70 parent::__construct($argv);
72 $this->appMode = $appMode;
73 $this->cache = $cache;
76 protected function doExecute()
78 if ($this->getOption('v')) {
79 $this->out('Executable: ' . $this->executable);
80 $this->out('Class: ' . __CLASS__);
81 $this->out('Arguments: ' . var_export($this->args, true));
82 $this->out('Options: ' . var_export($this->options, true));
85 if (!$this->appMode->has(App\Mode::DBCONFIGAVAILABLE)) {
86 $this->out('Database isn\'t ready or populated yet, database cache won\'t be available');
89 if ($this->getOption('v')) {
90 $this->out('Cache Driver Name: ' . $this->cache->getName());
91 $this->out('Cache Driver Class: ' . get_class($this->cache));
94 switch ($this->getArgument(0)) {
105 $this->executeFlush();
108 $this->executeClear();
112 if (count($this->args) == 0) {
113 $this->out($this->getHelp());
120 private function executeList()
122 $prefix = $this->getArgument(1);
123 $keys = $this->cache->getAllKeys($prefix);
125 if (empty($prefix)) {
126 $this->out('Listing all cache keys:');
128 $this->out('Listing all cache keys starting with "' . $prefix . '":');
132 foreach ($keys as $key) {
137 $this->out($count . ' keys found');
140 private function executeGet()
142 if (count($this->args) >= 2) {
143 $key = $this->getArgument(1);
144 $value = $this->cache->get($key);
146 $this->out("{$key} => " . var_export($value, true));
148 throw new CommandArgsException('Too few arguments for get');
152 private function executeSet()
154 if (count($this->args) >= 3) {
155 $key = $this->getArgument(1);
156 $value = $this->getArgument(2);
157 $duration = intval($this->getArgument(3, CacheClass::FIVE_MINUTES));
159 if (is_array($this->cache->get($key))) {
160 throw new RuntimeException("$key is an array and can't be set using this command.");
163 $result = $this->cache->set($key, $value, $duration);
165 $this->out("{$key} <= " . $this->cache->get($key));
167 $this->out("Unable to set {$key}");
170 throw new CommandArgsException('Too few arguments for set');
174 private function executeFlush()
176 $result = $this->cache->clear();
178 $this->out('Cache successfully flushed');
180 $this->out('Unable to flush the cache');
184 private function executeClear()
186 $result = $this->cache->clear(false);
188 $this->out('Cache successfully cleared');
190 $this->out('Unable to flush the cache');