3 namespace Friendica\Console;
5 use Asika\SimpleConsole\CommandArgsException;
7 use Friendica\Core\Cache\ICacheDriver;
8 use Friendica\Core\Config\Configuration;
9 use Friendica\Factory\CacheDriverFactory;
13 * @brief tool to access the cache from the CLI
15 * With this script you can access the cache of your node from the CLI.
16 * You can read current values stored in the cache and set new values
19 * @author Hypolite Petovan <hypolite@mrpetovan.com>
21 class Cache extends \Asika\SimpleConsole\Console
23 protected $helpOptions = ['h', 'help', '?'];
31 * @var string The cache driver name
33 private $cacheDriverName;
40 protected function getHelp()
43 console cache - Manage node cache
45 bin/console cache list [-h|--help|-?] [-v]
46 bin/console cache get <key> [-h|--help|-?] [-v]
47 bin/console cache set <key> <value> [-h|--help|-?] [-v]
48 bin/console cache flush [-h|--help|-?] [-v]
49 bin/console cache clear [-h|--help|-?] [-v]
52 bin/console cache list [<prefix>]
53 List all cache keys, optionally filtered by a prefix
55 bin/console cache get <key>
56 Shows the value of the provided cache key
58 bin/console cache set <key> <value> [<ttl>]
59 Sets the value of the provided cache key, optionally with the provided TTL (time to live) with a default of five minutes.
61 bin/console cache flush
62 Clears expired cache keys
64 bin/console cache clear
68 -h|--help|-? Show help information
69 -v Show more debug information.
74 public function __construct(App\Mode $appMode, Configuration $config, ICacheDriver $cache, array $argv = null)
76 parent::__construct($argv);
78 $this->appMode = $appMode;
79 $this->cache = $cache;
80 $this->cacheDriverName = $config->get('system', 'cache_driver', CacheDriverFactory::DEFAULT_DRIVER);
83 protected function doExecute()
85 if ($this->getOption('v')) {
86 $this->out('Executable: ' . $this->executable);
87 $this->out('Class: ' . __CLASS__);
88 $this->out('Arguments: ' . var_export($this->args, true));
89 $this->out('Options: ' . var_export($this->options, true));
92 if (!$this->appMode->has(App\Mode::DBCONFIGAVAILABLE)) {
93 $this->out('Database isn\'t ready or populated yet, database cache won\'t be available');
96 if ($this->getOption('v')) {
97 $this->out('Cache Driver Name: ' . $this->cacheDriverName);
98 $this->out('Cache Driver Class: ' . get_class($this->cache));
101 switch ($this->getArgument(0)) {
103 $this->executeList();
112 $this->executeFlush();
115 $this->executeClear();
119 if (count($this->args) == 0) {
120 $this->out($this->getHelp());
127 private function executeList()
129 $prefix = $this->getArgument(1);
130 $keys = $this->cache->getAllKeys($prefix);
132 if (empty($prefix)) {
133 $this->out('Listing all cache keys:');
135 $this->out('Listing all cache keys starting with "' . $prefix . '":');
139 foreach ($keys as $key) {
144 $this->out($count . ' keys found');
147 private function executeGet()
149 if (count($this->args) >= 2) {
150 $key = $this->getArgument(1);
151 $value = $this->cache->get($key);
153 $this->out("{$key} => " . var_export($value, true));
155 throw new CommandArgsException('Too few arguments for get');
159 private function executeSet()
161 if (count($this->args) >= 3) {
162 $key = $this->getArgument(1);
163 $value = $this->getArgument(2);
164 $duration = intval($this->getArgument(3, ICacheDriver::FIVE_MINUTES));
166 if (is_array($this->cache->get($key))) {
167 throw new RuntimeException("$key is an array and can't be set using this command.");
170 $result = $this->cache->set($key, $value, $duration);
172 $this->out("{$key} <= " . $this->cache->get($key));
174 $this->out("Unable to set {$key}");
177 throw new CommandArgsException('Too few arguments for set');
181 private function executeFlush()
183 $result = $this->cache->clear();
185 $this->out('Cache successfully flushed');
187 $this->out('Unable to flush the cache');
191 private function executeClear()
193 $result = $this->cache->clear(false);
195 $this->out('Cache successfully cleared');
197 $this->out('Unable to flush the cache');