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