Update copyright
[friendica.git/.git] / src / Console / Cache.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Console;
23
24 use Asika\SimpleConsole\CommandArgsException;
25 use Friendica\App;
26 use Friendica\Core\Cache\Duration;
27 use Friendica\Core\Cache\ICache;
28 use RuntimeException;
29
30 /**
31  * tool to access the cache from the CLI
32  *
33  * With this script you can access the cache of your node from the CLI.
34  * You can read current values stored in the cache and set new values
35  * in cache keys.
36  */
37 class Cache extends \Asika\SimpleConsole\Console
38 {
39         protected $helpOptions = ['h', 'help', '?'];
40
41         /**
42          * @var App\Mode
43          */
44         private $appMode;
45
46         /**
47          * @var ICache
48          */
49         private $cache;
50
51         protected function getHelp()
52         {
53                 $help = <<<HELP
54 console cache - Manage node cache
55 Synopsis
56         bin/console cache list [-h|--help|-?] [-v]
57         bin/console cache get <key> [-h|--help|-?] [-v]
58         bin/console cache set <key> <value> [-h|--help|-?] [-v]
59         bin/console cache flush [-h|--help|-?] [-v]
60         bin/console cache clear [-h|--help|-?] [-v]
61
62 Description
63         bin/console cache list [<prefix>]
64                 List all cache keys, optionally filtered by a prefix
65
66         bin/console cache get <key>
67                 Shows the value of the provided cache key
68
69         bin/console cache set <key> <value> [<ttl>]
70                 Sets the value of the provided cache key, optionally with the provided TTL (time to live) with a default of five minutes.
71
72         bin/console cache flush
73                 Clears expired cache keys
74
75         bin/console cache clear
76                 Clears all cache keys
77
78 Options
79     -h|--help|-? Show help information
80     -v           Show more debug information.
81 HELP;
82                 return $help;
83         }
84
85         public function __construct(App\Mode $appMode, ICache $cache, array $argv = null)
86         {
87                 parent::__construct($argv);
88
89                 $this->appMode = $appMode;
90                 $this->cache   = $cache;
91         }
92
93         protected function doExecute()
94         {
95                 if ($this->getOption('v')) {
96                         $this->out('Executable: ' . $this->executable);
97                         $this->out('Class: ' . __CLASS__);
98                         $this->out('Arguments: ' . var_export($this->args, true));
99                         $this->out('Options: ' . var_export($this->options, true));
100                 }
101
102                 if (!$this->appMode->has(App\Mode::DBCONFIGAVAILABLE)) {
103                         $this->out('Database isn\'t ready or populated yet, database cache won\'t be available');
104                 }
105
106                 if ($this->getOption('v')) {
107                         $this->out('Cache Driver Name: ' . $this->cache->getName());
108                         $this->out('Cache Driver Class: ' . get_class($this->cache));
109                 }
110
111                 switch ($this->getArgument(0)) {
112                         case 'list':
113                                 $this->executeList();
114                                 break;
115                         case 'get':
116                                 $this->executeGet();
117                                 break;
118                         case 'set':
119                                 $this->executeSet();
120                                 break;
121                         case 'flush':
122                                 $this->executeFlush();
123                                 break;
124                         case 'clear':
125                                 $this->executeClear();
126                                 break;
127                 }
128
129                 if (count($this->args) == 0) {
130                         $this->out($this->getHelp());
131                         return 0;
132                 }
133
134                 return 0;
135         }
136
137         private function executeList()
138         {
139                 $prefix = $this->getArgument(1);
140                 $keys   = $this->cache->getAllKeys($prefix);
141
142                 if (empty($prefix)) {
143                         $this->out('Listing all cache keys:');
144                 } else {
145                         $this->out('Listing all cache keys starting with "' . $prefix . '":');
146                 }
147
148                 $count = 0;
149                 foreach ($keys as $key) {
150                         $this->out($key);
151                         $count++;
152                 }
153
154                 $this->out($count . ' keys found');
155         }
156
157         private function executeGet()
158         {
159                 if (count($this->args) >= 2) {
160                         $key   = $this->getArgument(1);
161                         $value = $this->cache->get($key);
162
163                         $this->out("{$key} => " . var_export($value, true));
164                 } else {
165                         throw new CommandArgsException('Too few arguments for get');
166                 }
167         }
168
169         private function executeSet()
170         {
171                 if (count($this->args) >= 3) {
172                         $key      = $this->getArgument(1);
173                         $value    = $this->getArgument(2);
174                         $duration = intval($this->getArgument(3, Duration::FIVE_MINUTES));
175
176                         if (is_array($this->cache->get($key))) {
177                                 throw new RuntimeException("$key is an array and can't be set using this command.");
178                         }
179
180                         $result = $this->cache->set($key, $value, $duration);
181                         if ($result) {
182                                 $this->out("{$key} <= " . $this->cache->get($key));
183                         } else {
184                                 $this->out("Unable to set {$key}");
185                         }
186                 } else {
187                         throw new CommandArgsException('Too few arguments for set');
188                 }
189         }
190
191         private function executeFlush()
192         {
193                 $result = $this->cache->clear();
194                 if ($result) {
195                         $this->out('Cache successfully flushed');
196                 } else {
197                         $this->out('Unable to flush the cache');
198                 }
199         }
200
201         private function executeClear()
202         {
203                 $result = $this->cache->clear(false);
204                 if ($result) {
205                         $this->out('Cache successfully cleared');
206                 } else {
207                         $this->out('Unable to flush the cache');
208                 }
209         }
210 }