1 | <?php |
||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||
2 | |||
3 | namespace Gewaer\Cli\Tasks; |
||
4 | |||
5 | use function Gewaer\Core\appPath; |
||
6 | use Phalcon\Cache\Backend\Libmemcached; |
||
7 | use Phalcon\Cli\Task as PhTask; |
||
8 | use RecursiveDirectoryIterator; |
||
9 | use RecursiveIteratorIterator; |
||
10 | |||
11 | /** |
||
12 | * Class ClearcacheTask |
||
13 | * |
||
14 | * @package Niden\Cli\Tasks |
||
15 | * |
||
16 | * @property Libmemcached $cache |
||
17 | */ |
||
18 | class ClearcacheTask extends PhTask |
||
19 | { |
||
20 | /** |
||
21 | * Clears the data cache from the application |
||
22 | */ |
||
23 | public function mainAction() |
||
24 | { |
||
25 | $this->clearFileCache(); |
||
26 | $this->clearMemCached(); |
||
27 | } |
||
28 | |||
29 | /** |
||
30 | * Clears file based cache |
||
31 | */ |
||
32 | private function clearFileCache() |
||
33 | { |
||
34 | echo 'Clearing Cache folders' . PHP_EOL; |
||
35 | |||
36 | $fileList = []; |
||
37 | $whitelist = ['.', '..', '.gitignore']; |
||
38 | $path = appPath('storage/cache'); |
||
39 | $dirIterator = new RecursiveDirectoryIterator($path); |
||
40 | $iterator = new RecursiveIteratorIterator( |
||
41 | $dirIterator, |
||
42 | RecursiveIteratorIterator::CHILD_FIRST |
||
43 | ); |
||
44 | |||
45 | /** |
||
46 | * Get how many files we have there and where they are |
||
47 | */ |
||
48 | foreach ($iterator as $file) { |
||
49 | if (true !== $file->isDir() && true !== in_array($file->getFilename(), $whitelist)) { |
||
50 | $fileList[] = $file->getPathname(); |
||
51 | } |
||
52 | } |
||
53 | |||
54 | echo sprintf('Found %s files', count($fileList)) . PHP_EOL; |
||
55 | foreach ($fileList as $file) { |
||
56 | echo '.'; |
||
57 | unlink($file); |
||
58 | } |
||
59 | |||
60 | echo PHP_EOL . 'Cleared Cache folders' . PHP_EOL; |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * Clears memcached data cache |
||
65 | */ |
||
66 | private function clearMemCached() |
||
67 | { |
||
68 | echo 'Clearing data cache' . PHP_EOL; |
||
69 | $options = $this->cache->getOptions(); |
||
70 | $servers = $options['servers'] ?? []; |
||
71 | $memcached = new \Memcached(); |
||
72 | foreach ($servers as $server) { |
||
73 | $memcached->addServer($server['host'], $server['port'], $server['weight']); |
||
74 | } |
||
75 | |||
76 | $keys = $memcached->getAllKeys(); |
||
77 | //print_r($keys); |
||
78 | echo sprintf('Found %s keys', count($keys)) . PHP_EOL; |
||
79 | foreach ($keys as $key) { |
||
80 | if ('bakaapi-' === substr($key, 0, 8)) { |
||
81 | $server = $memcached->getServerByKey($key); |
||
82 | $result = $memcached->deleteByKey($server['host'], $key); |
||
83 | $resultCode = $memcached->getResultCode(); |
||
84 | if (true === $result && $resultCode !== \Memcached::RES_NOTFOUND) { |
||
85 | echo '.'; |
||
86 | } else { |
||
87 | echo 'F'; |
||
88 | } |
||
89 | } |
||
90 | } |
||
91 | |||
92 | echo PHP_EOL . 'Cleared data cache' . PHP_EOL; |
||
93 | } |
||
94 | } |
||
95 |