]> git.mxchange.org Git - friendica.git/blob - src/Console/Cache.php
Merge pull request #7444 from nupplaphil/task/dice_console
[friendica.git] / src / Console / Cache.php
1 <?php
2
3 namespace Friendica\Console;
4
5 use Asika\SimpleConsole\CommandArgsException;
6 use Friendica\App;
7 use Friendica\Core;
8 use RuntimeException;
9
10 /**
11  * @brief tool to access the cache from the CLI
12  *
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
15  * in cache keys.
16  *
17  * @author Hypolite Petovan <hypolite@mrpetovan.com>
18  */
19 class Cache extends \Asika\SimpleConsole\Console
20 {
21         protected $helpOptions = ['h', 'help', '?'];
22
23         /**
24          * @var App\Mode
25          */
26         private $appMode;
27
28         protected function getHelp()
29         {
30                 $help = <<<HELP
31 console cache - Manage node cache
32 Synopsis
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]
38
39 Description
40         bin/console cache list [<prefix>]
41                 List all cache keys, optionally filtered by a prefix
42
43         bin/console cache get <key>
44                 Shows the value of the provided cache key
45
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.
48
49         bin/console cache flush
50                 Clears expired cache keys
51
52         bin/console cache clear
53                 Clears all cache keys
54
55 Options
56     -h|--help|-? Show help information
57     -v           Show more debug information.
58 HELP;
59                 return $help;
60         }
61
62         public function __construct(App\Mode $appMode, array $argv = null)
63         {
64                 parent::__construct($argv);
65
66                 $this->appMode = $appMode;
67         }
68
69         protected function doExecute()
70         {
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));
76                 }
77
78                 if (!$this->appMode->has(App\Mode::DBCONFIGAVAILABLE)) {
79                         $this->out('Database isn\'t ready or populated yet, database cache won\'t be available');
80                 }
81
82                 Core\Cache::init();
83
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);
87                 }
88
89                 switch ($this->getArgument(0)) {
90                         case 'list':
91                                 $this->executeList();
92                                 break;
93                         case 'get':
94                                 $this->executeGet();
95                                 break;
96                         case 'set':
97                                 $this->executeSet();
98                                 break;
99                         case 'flush':
100                                 $this->executeFlush();
101                                 break;
102                         case 'clear':
103                                 $this->executeClear();
104                                 break;
105                 }
106
107                 if (count($this->args) == 0) {
108                         $this->out($this->getHelp());
109                         return 0;
110                 }
111
112                 return 0;
113         }
114
115         private function executeList()
116         {
117                 $prefix = $this->getArgument(1);
118                 $keys = Core\Cache::getAllKeys($prefix);
119
120                 if (empty($prefix)) {
121                         $this->out('Listing all cache keys:');
122                 } else {
123                         $this->out('Listing all cache keys starting with "' . $prefix . '":');
124                 }
125
126                 $count = 0;
127                 foreach ($keys as $key) {
128                         $this->out($key);
129                         $count++;
130                 }
131
132                 $this->out($count . ' keys found');
133         }
134
135         private function executeGet()
136         {
137                 if (count($this->args) >= 2) {
138                         $key = $this->getArgument(1);
139                         $value = Core\Cache::get($key);
140
141                         $this->out("{$key} => " . var_export($value, true));
142                 } else {
143                         throw new CommandArgsException('Too few arguments for get');
144                 }
145         }
146
147         private function executeSet()
148         {
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));
153
154                         if (is_array(Core\Cache::get($key))) {
155                                 throw new RuntimeException("$key is an array and can't be set using this command.");
156                         }
157
158                         $result = Core\Cache::set($key, $value, $duration);
159                         if ($result) {
160                                 $this->out("{$key} <= " . Core\Cache::get($key));
161                         } else {
162                                 $this->out("Unable to set {$key}");
163                         }
164                 } else {
165                         throw new CommandArgsException('Too few arguments for set');
166                 }
167         }
168
169         private function executeFlush()
170         {
171                 $result = Core\Cache::clear();
172                 if ($result) {
173                         $this->out('Cache successfully flushed');
174                 } else {
175                         $this->out('Unable to flush the cache');
176                 }
177         }
178
179         private function executeClear()
180         {
181                 $result = Core\Cache::clear(false);
182                 if ($result) {
183                         $this->out('Cache successfully cleared');
184                 } else {
185                         $this->out('Unable to flush the cache');
186                 }
187         }
188 }