]> git.mxchange.org Git - friendica.git/blob - src/Core/Console/Cache.php
0dfef43f35b4bbe64fe167de9f91fb1b1200538a
[friendica.git] / src / Core / Console / Cache.php
1 <?php
2
3 namespace Friendica\Core\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 (!App\Mode::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                         if (empty($prefix) || strpos($key, $prefix) === 0) {
119                                 $this->out($key);
120                                 $count++;
121                         }
122                 }
123
124                 $this->out($count . ' keys found');
125         }
126
127         private function executeGet()
128         {
129                 if (count($this->args) >= 2) {
130                         $key = $this->getArgument(1);
131                         $value = Core\Cache::get($key);
132
133                         $this->out("{$key} => " . var_export($value, true));
134                 } else {
135                         throw new CommandArgsException('Too few arguments for get');
136                 }
137         }
138
139         private function executeSet()
140         {
141                 if (count($this->args) >= 3) {
142                         $key = $this->getArgument(1);
143                         $value = $this->getArgument(2);
144                         $duration = intval($this->getArgument(3, Core\Cache::FIVE_MINUTES));
145
146                         if (is_array(Core\Cache::get($key))) {
147                                 throw new RuntimeException("$key is an array and can't be set using this command.");
148                         }
149
150                         $result = Core\Cache::set($key, $value, $duration);
151                         if ($result) {
152                                 $this->out("{$key} <= " . Core\Cache::get($key));
153                         } else {
154                                 $this->out("Unable to set {$key}");
155                         }
156                 } else {
157                         throw new CommandArgsException('Too few arguments for set');
158                 }
159         }
160
161         private function executeFlush()
162         {
163                 $result = Core\Cache::clear();
164                 if ($result) {
165                         $this->out('Cache successfully flushed');
166                 } else {
167                         $this->out('Unable to flush the cache');
168                 }
169         }
170
171         private function executeClear()
172         {
173                 $result = Core\Cache::clear(false);
174                 if ($result) {
175                         $this->out('Cache successfully cleared');
176                 } else {
177                         $this->out('Unable to flush the cache');
178                 }
179         }
180 }