]> git.mxchange.org Git - friendica.git/blob - src/Console/Cache.php
Merge pull request #7274 from MrPetovan/bug/7271-allow-brackets-in-urls
[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         protected function getHelp()
24         {
25                 $help = <<<HELP
26 console cache - Manage node cache
27 Synopsis
28         bin/console cache list [-h|--help|-?] [-v]
29         bin/console cache get <key> [-h|--help|-?] [-v]
30         bin/console cache set <key> <value> [-h|--help|-?] [-v]
31         bin/console cache flush [-h|--help|-?] [-v]
32         bin/console cache clear [-h|--help|-?] [-v]
33
34 Description
35         bin/console cache list [<prefix>]
36                 List all cache keys, optionally filtered by a prefix
37
38         bin/console cache get <key>
39                 Shows the value of the provided cache key
40
41         bin/console cache set <key> <value> [<ttl>]
42                 Sets the value of the provided cache key, optionally with the provided TTL (time to live) with a default of five minutes.
43
44         bin/console cache flush
45                 Clears expired cache keys
46
47         bin/console cache clear
48                 Clears all cache keys
49
50 Options
51     -h|--help|-? Show help information
52     -v           Show more debug information.
53 HELP;
54                 return $help;
55         }
56
57         protected function doExecute()
58         {
59                 $a = \Friendica\BaseObject::getApp();
60
61                 if ($this->getOption('v')) {
62                         $this->out('Executable: ' . $this->executable);
63                         $this->out('Class: ' . __CLASS__);
64                         $this->out('Arguments: ' . var_export($this->args, true));
65                         $this->out('Options: ' . var_export($this->options, true));
66                 }
67
68                 if ($a->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
69                         $this->out('Database isn\'t ready or populated yet, database cache won\'t be available');
70                 }
71
72                 Core\Cache::init();
73
74                 if ($this->getOption('v')) {
75                         $this->out('Cache Driver Name: ' . Core\Cache::$driver_name);
76                         $this->out('Cache Driver Class: ' . Core\Cache::$driver_class);
77                 }
78
79                 switch ($this->getArgument(0)) {
80                         case 'list':
81                                 $this->executeList();
82                                 break;
83                         case 'get':
84                                 $this->executeGet();
85                                 break;
86                         case 'set':
87                                 $this->executeSet();
88                                 break;
89                         case 'flush':
90                                 $this->executeFlush();
91                                 break;
92                         case 'clear':
93                                 $this->executeClear();
94                                 break;
95                 }
96
97                 if (count($this->args) == 0) {
98                         $this->out($this->getHelp());
99                         return 0;
100                 }
101
102                 return 0;
103         }
104
105         private function executeList()
106         {
107                 $prefix = $this->getArgument(1);
108                 $keys = Core\Cache::getAllKeys($prefix);
109
110                 if (empty($prefix)) {
111                         $this->out('Listing all cache keys:');
112                 } else {
113                         $this->out('Listing all cache keys starting with "' . $prefix . '":');
114                 }
115
116                 $count = 0;
117                 foreach ($keys as $key) {
118                         $this->out($key);
119                         $count++;
120                 }
121
122                 $this->out($count . ' keys found');
123         }
124
125         private function executeGet()
126         {
127                 if (count($this->args) >= 2) {
128                         $key = $this->getArgument(1);
129                         $value = Core\Cache::get($key);
130
131                         $this->out("{$key} => " . var_export($value, true));
132                 } else {
133                         throw new CommandArgsException('Too few arguments for get');
134                 }
135         }
136
137         private function executeSet()
138         {
139                 if (count($this->args) >= 3) {
140                         $key = $this->getArgument(1);
141                         $value = $this->getArgument(2);
142                         $duration = intval($this->getArgument(3, Core\Cache::FIVE_MINUTES));
143
144                         if (is_array(Core\Cache::get($key))) {
145                                 throw new RuntimeException("$key is an array and can't be set using this command.");
146                         }
147
148                         $result = Core\Cache::set($key, $value, $duration);
149                         if ($result) {
150                                 $this->out("{$key} <= " . Core\Cache::get($key));
151                         } else {
152                                 $this->out("Unable to set {$key}");
153                         }
154                 } else {
155                         throw new CommandArgsException('Too few arguments for set');
156                 }
157         }
158
159         private function executeFlush()
160         {
161                 $result = Core\Cache::clear();
162                 if ($result) {
163                         $this->out('Cache successfully flushed');
164                 } else {
165                         $this->out('Unable to flush the cache');
166                 }
167         }
168
169         private function executeClear()
170         {
171                 $result = Core\Cache::clear(false);
172                 if ($result) {
173                         $this->out('Cache successfully cleared');
174                 } else {
175                         $this->out('Unable to flush the cache');
176                 }
177         }
178 }