Conditions | 9 |
Paths | 5 |
Total Lines | 53 |
Code Lines | 24 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
38 | public function handleOnCluster(): void |
||
39 | { |
||
40 | // Tenta di acquisire un lock globale |
||
41 | $lockAcquired = $this->superCache->lock('clean_orphans:lock', $this->option('connection_name'), 300); |
||
42 | if (!$lockAcquired) { |
||
43 | return; |
||
44 | } |
||
45 | // Purtroppo lo scan non funziona sul cluster per cui va eseguito su ogni nodo (master) |
||
46 | $array_nodi = $this->getClusterNodesService->getClusterNodes($this->option('connection_name')); |
||
47 | |||
48 | foreach ($array_nodi as $node) { |
||
49 | [$host, $port] = explode(':', $node); |
||
50 | |||
51 | // Per ogni shard vado a cercare i bussolotti (SET) dei tags che contengono le chiavi |
||
52 | for ($i = 0; $i < $this->numShards; $i++) { |
||
53 | // Inserisco nel pattern supercache: così sonop sicura di trovare solo i SET che riguardano il contesto della supercache |
||
54 | $shard_key = '*' . config('supercache.prefix') . 'tag:*:shard:' . $i; |
||
55 | // Creo una connessione persistente perchè considerando la durata dell'elaborazione si evita che dopo 30 secondi muoia tutto! |
||
56 | $connection = $this->redis->getNativeRedisConnection($this->option('connection_name'), $host, $port); |
||
57 | |||
58 | $cursor = null; |
||
59 | do { |
||
60 | $response = $connection['connection']->scan($cursor, $shard_key); |
||
61 | |||
62 | if ($response === false) { |
||
63 | //Nessuna chiave trovata ... |
||
64 | break; |
||
65 | } |
||
66 | |||
67 | foreach ($response as $key) { |
||
68 | // Ho trovato un bussolotto che matcha, vado a recuperare i membri del SET |
||
69 | $members = $connection['connection']->sMembers($key); |
||
70 | foreach ($members as $member) { |
||
71 | // Controllo che la chiave sia morta, ma ATTENZIONE non posso usare la connessione che ho già perchè sono su un singolo nodo, mentre nel bussolotto potrebbero esserci chiavi in sharding diversi |
||
72 | if ($this->redis->getRedisConnection($this->option('connection_name'))->exists($member)) { |
||
73 | // La chiave è viva! vado avanti |
||
74 | continue; |
||
75 | } |
||
76 | // Altrimenti rimuovo i cadaveri dal bussolotto e dai tags |
||
77 | // Qui posso usare la connessione che già ho in quanto sto modificando il bussolotto che sicuramente è nello shard corretto del nodo |
||
78 | $connection['connection']->srem($key, $member); |
||
79 | // Rimuovo anche i tag, che però potrebbero essere in un altro nodo quindi uso una nuova connessione |
||
80 | $this->redis->getRedisConnection($this->option('connection_name'))->del($member . ':tags'); |
||
81 | } |
||
82 | } |
||
83 | } while ($cursor !== 0); // Continua fino a completamento |
||
84 | |||
85 | // Chiudo la connessione |
||
86 | $connection['connection']->close(); |
||
87 | } |
||
88 | } |
||
89 | // Rilascio il lock globale |
||
90 | $this->superCache->unLock('clean_orphans:lock', $this->option('connection_name')); |
||
91 | } |
||
182 |