Conditions | 3 |
Paths | 4 |
Total Lines | 86 |
Code Lines | 70 |
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 |
||
93 | public function handleOnStandalone(): void |
||
94 | { |
||
95 | // Carica il prefisso di default per le chiavi |
||
96 | $prefix = config('supercache.prefix'); |
||
97 | |||
98 | // ATTENZIONE! il comando SMEMBERS non supporta *, per cui va usata la combinazipone di SCAN e SMEMBERS |
||
99 | // Non usare MAI il comando KEYS se non si vuole distruggere il server! |
||
100 | |||
101 | // Script Lua per pulire le chiavi orfane |
||
102 | $script = <<<'LUA' |
||
103 | local success, result = pcall(function() |
||
104 | local database_prefix = string.gsub(KEYS[5], "temp", "") |
||
105 | local shard_prefix = KEYS[1] |
||
106 | local num_shards = string.gsub(KEYS[2], database_prefix, "") |
||
107 | local lock_key = KEYS[3] |
||
108 | local lock_ttl = string.gsub(KEYS[4], database_prefix, "") |
||
109 | |||
110 | -- Tenta di acquisire un lock globale |
||
111 | if redis.call("SET", lock_key, "1", "NX", "EX", lock_ttl) then |
||
112 | -- Scansiona tutti gli shard |
||
113 | -- redis.log(redis.LOG_NOTICE, "Scansiona tutti gli shard: " .. num_shards) |
||
114 | for i = 0, num_shards - 1 do |
||
115 | local shard_key = shard_prefix .. ":" .. i |
||
116 | -- redis.log(redis.LOG_NOTICE, "shard_key: " .. shard_key) |
||
117 | -- Ottieni tutte le chiavi dallo shard |
||
118 | local cursor = "0" |
||
119 | local keys = {} |
||
120 | repeat |
||
121 | local result = redis.call("SCAN", cursor, "MATCH", shard_key) |
||
122 | cursor = result[1] |
||
123 | for _, key in ipairs(result[2]) do |
||
124 | table.insert(keys, key) |
||
125 | end |
||
126 | until cursor == "0" |
||
127 | -- Cerco tutte le chiavi associate a questa chiave |
||
128 | for _, key in ipairs(keys) do |
||
129 | -- redis.log(redis.LOG_NOTICE, "CHIAVE: " .. key) |
||
130 | local members = redis.call("SMEMBERS", key) |
||
131 | for _, member in ipairs(members) do |
||
132 | redis.log(redis.LOG_NOTICE, "member: " .. database_prefix .. member) |
||
133 | if redis.call("EXISTS", database_prefix .. member) == 0 then |
||
134 | -- La chiave è orfana, rimuovila dallo shard |
||
135 | redis.call("SREM", key, member) |
||
136 | -- redis.log(redis.LOG_NOTICE, "Rimossa chiave orfana key: " .. key .. " member: " .. member) |
||
137 | -- Devo rimuovere anche la chiave con i tags |
||
138 | local tagsKey = database_prefix .. member .. ":tags" |
||
139 | -- redis.log(redis.LOG_NOTICE, "Rimuovo la chiave con tagsKey: " .. tagsKey) |
||
140 | redis.call("DEL", tagsKey) |
||
141 | end |
||
142 | end |
||
143 | end |
||
144 | end |
||
145 | -- Rilascia il lock |
||
146 | redis.call("DEL", lock_key) |
||
147 | end |
||
148 | end) |
||
149 | if not success then |
||
150 | redis.log(redis.LOG_WARNING, "Errore durante l'esecuzione del batch: " .. result) |
||
151 | return result; |
||
152 | end |
||
153 | return "OK" |
||
154 | LUA; |
||
155 | |||
156 | try { |
||
157 | // Parametri dello script |
||
158 | $shardPrefix = $prefix . 'tag:*:shard'; |
||
159 | $tagPrefix = $prefix; |
||
|
|||
160 | $lockKey = $prefix . 'clean_orphans:lock'; |
||
161 | $lockTTL = 300; // Timeout lock di 300 secondi |
||
162 | |||
163 | // Esegui lo script Lua |
||
164 | $return = $this->redis->getRedisConnection($this->option('connection_name'))->eval( |
||
165 | $script, |
||
166 | 5, // Numero di parametri passati a Lua come KEYS |
||
167 | $shardPrefix, |
||
168 | $this->numShards, |
||
169 | $lockKey, |
||
170 | $lockTTL, |
||
171 | 'temp', |
||
172 | ); |
||
173 | |||
174 | if ($return !== 'OK') { |
||
175 | Log::error('Errore durante l\'esecuzione dello script Lua: ' . $return); |
||
176 | } |
||
177 | } catch (\Exception $e) { |
||
178 | Log::error('Errore durante l\'esecuzione dello script Lua: ' . $e->getMessage()); |
||
179 | } |
||
182 |