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