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