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