1
|
|
|
<?php
|
|
|
|
|
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
|
1 |
|
public function mainAction()
|
24
|
|
|
{
|
25
|
1 |
|
$this->clearFileCache();
|
26
|
1 |
|
$this->clearMemCached();
|
27
|
1 |
|
}
|
28
|
|
|
|
29
|
|
|
/**
|
30
|
|
|
* Clears file based cache
|
31
|
|
|
*/
|
32
|
1 |
|
private function clearFileCache()
|
33
|
|
|
{
|
34
|
1 |
|
echo 'Clearing Cache folders' . PHP_EOL;
|
35
|
|
|
|
36
|
1 |
|
$fileList = [];
|
37
|
1 |
|
$whitelist = ['.', '..', '.gitignore'];
|
38
|
1 |
|
$path = appPath('storage/cache');
|
39
|
1 |
|
$dirIterator = new RecursiveDirectoryIterator($path);
|
40
|
1 |
|
$iterator = new RecursiveIteratorIterator(
|
41
|
1 |
|
$dirIterator,
|
42
|
1 |
|
RecursiveIteratorIterator::CHILD_FIRST
|
43
|
|
|
);
|
44
|
|
|
|
45
|
|
|
/**
|
46
|
|
|
* Get how many files we have there and where they are
|
47
|
|
|
*/
|
48
|
1 |
|
foreach ($iterator as $file) {
|
49
|
1 |
|
if (true !== $file->isDir() && true !== in_array($file->getFilename(), $whitelist)) {
|
50
|
1 |
|
$fileList[] = $file->getPathname();
|
51
|
|
|
}
|
52
|
|
|
}
|
53
|
|
|
|
54
|
1 |
|
echo sprintf('Found %s files', count($fileList)) . PHP_EOL;
|
55
|
1 |
|
foreach ($fileList as $file) {
|
56
|
1 |
|
echo '.';
|
57
|
1 |
|
unlink($file);
|
58
|
|
|
}
|
59
|
|
|
|
60
|
1 |
|
echo PHP_EOL . 'Cleared Cache folders' . PHP_EOL;
|
61
|
1 |
|
}
|
62
|
|
|
|
63
|
|
|
/**
|
64
|
|
|
* Clears memcached data cache
|
65
|
|
|
*/
|
66
|
1 |
|
private function clearMemCached()
|
67
|
|
|
{
|
68
|
1 |
|
echo 'Clearing data cache' . PHP_EOL;
|
69
|
1 |
|
$options = $this->cache->getOptions();
|
70
|
1 |
|
$servers = $options['servers'] ?? [];
|
71
|
1 |
|
$memcached = new \Memcached();
|
72
|
1 |
|
foreach ($servers as $server) {
|
73
|
1 |
|
$memcached->addServer($server['host'], $server['port'], $server['weight']);
|
74
|
|
|
}
|
75
|
|
|
|
76
|
1 |
|
$keys = $memcached->getAllKeys();
|
77
|
|
|
//print_r($keys);
|
78
|
1 |
|
echo sprintf('Found %s keys', count($keys)) . PHP_EOL;
|
79
|
1 |
|
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
|
1 |
|
echo PHP_EOL . 'Cleared data cache' . PHP_EOL;
|
93
|
1 |
|
}
|
94
|
|
|
}
|
95
|
|
|
|