|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: hugh.li |
|
5
|
|
|
* Date: 2021/6/9 |
|
6
|
|
|
* Time: 7:25 δΈε. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace HughCube\Laravel\OTS\Commands; |
|
10
|
|
|
|
|
11
|
|
|
use Aliyun\OTS\OTSClientException; |
|
12
|
|
|
use Aliyun\OTS\OTSServerException; |
|
13
|
|
|
use Carbon\Carbon; |
|
14
|
|
|
use HughCube\Laravel\OTS\Cache\Store; |
|
15
|
|
|
use Illuminate\Console\Command; |
|
16
|
|
|
use Illuminate\Contracts\Cache\Repository; |
|
17
|
|
|
use Illuminate\Support\Facades\Cache; |
|
18
|
|
|
|
|
19
|
|
|
class CacheGc extends Command |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* The console command name. |
|
23
|
|
|
* |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $signature = 'ots:gc-cache |
|
27
|
|
|
{--cache=ots : name of cache. } |
|
28
|
|
|
{--expired_duration=2592000 : The data that has expired is cleared. }'; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* The console command description. |
|
32
|
|
|
* |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $description = 'ots cache gc'; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @return void |
|
39
|
|
|
* @throws OTSClientException |
|
40
|
|
|
* @throws OTSServerException |
|
41
|
|
|
*/ |
|
42
|
|
|
public function handle() |
|
43
|
|
|
{ |
|
44
|
|
|
$store = $this->getCache()->getStore(); |
|
45
|
|
|
if (!$store instanceof Store) { |
|
46
|
|
|
$this->warn('Only OTS cache can be processed.'); |
|
47
|
|
|
return; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$count = 0; |
|
51
|
|
|
$page = 200; |
|
52
|
|
|
while (true) { |
|
53
|
|
|
$deletedCount = $store->flushExpiredRows($page, $this->getExpiredDuration()); |
|
54
|
|
|
$this->comment(sprintf( |
|
55
|
|
|
'%s Delete %s rows from the "%s" table, The total number of deleted rows is %s.', |
|
56
|
|
|
Carbon::now()->format('Y-m-d H:i:s.u'), |
|
57
|
|
|
$deletedCount, $store->getTable(), ($count += $deletedCount) |
|
58
|
|
|
)); |
|
59
|
|
|
if (0 >= $deletedCount || $deletedCount < $page) { |
|
60
|
|
|
break; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
protected function getCache(): Repository |
|
66
|
|
|
{ |
|
67
|
|
|
return Cache::store(($this->option('cache') ?: null)); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
protected function getExpiredDuration(): int |
|
71
|
|
|
{ |
|
72
|
|
|
return intval($this->option('expired_duration')); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|