| Conditions | 3 |
| Paths | 7 |
| Total Lines | 71 |
| Code Lines | 61 |
| 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 |
||
| 117 | protected function processBatchOnStandalone(): void |
||
| 118 | { |
||
| 119 | $debug = 'Processo batch: ' . implode(', ', $this->batch) . PHP_EOL . |
||
| 120 | 'Host: ' . $this->option('host') . PHP_EOL . |
||
| 121 | 'Port: ' . $this->option('port') . PHP_EOL; |
||
| 122 | |||
| 123 | $luaScript = <<<'LUA' |
||
| 124 | |||
| 125 | local success, result = pcall(function() |
||
| 126 | local keys = ARGV |
||
| 127 | local prefix = ARGV[1] |
||
| 128 | local database_prefix = ARGV[3] |
||
| 129 | local shard_count = ARGV[2] |
||
| 130 | -- redis.log(redis.LOG_NOTICE, 'prefix: ' .. prefix); |
||
| 131 | -- redis.log(redis.LOG_NOTICE, 'database_prefix: ' .. database_prefix); |
||
| 132 | -- redis.log(redis.LOG_NOTICE, 'shard_count: ' .. shard_count); |
||
| 133 | for i, key in ipairs(keys) do |
||
| 134 | -- salto le prime 3 chiavi che ho usato come settings |
||
| 135 | if i > 3 then |
||
| 136 | local row = {} |
||
| 137 | for value in string.gmatch(key, "[^|]+") do |
||
| 138 | table.insert(row, value) |
||
| 139 | end |
||
| 140 | local fullKey = database_prefix .. row[1] |
||
| 141 | -- redis.log(redis.LOG_NOTICE, 'Chiave Redis Expired: ' .. fullKey) |
||
| 142 | -- Controlla se la chiave è effettivamente scaduta |
||
| 143 | if redis.call('EXISTS', fullKey) == 0 then |
||
| 144 | -- local tagsKey = prefix .. 'tags:' .. row[1] |
||
| 145 | local tagsKey = fullKey .. ':tags' |
||
| 146 | -- redis.log(redis.LOG_NOTICE, 'Tag: ' .. tagsKey); |
||
| 147 | local tags = redis.call("SMEMBERS", tagsKey) |
||
| 148 | -- redis.log(redis.LOG_NOTICE, 'Tags associati: ' .. table.concat(tags, ", ")); |
||
| 149 | -- Rimuove la chiave dai set di tag associati |
||
| 150 | for j, tag in ipairs(tags) do |
||
| 151 | local shardIndex = tonumber(row[2]) % tonumber(shard_count) |
||
| 152 | local shardKey = database_prefix .. prefix .. "tag:" .. tag .. ":shard:" .. shardIndex |
||
| 153 | -- redis.log(redis.LOG_NOTICE, 'Rimuovo la chiave dallo shard: ' .. row[1]); |
||
| 154 | redis.call("SREM", shardKey, row[1]) |
||
| 155 | -- redis.log(redis.LOG_NOTICE, 'Rimossa chiave tag: ' .. shardKey); |
||
| 156 | end |
||
| 157 | -- Rimuove l'associazione della chiave con i tag |
||
| 158 | redis.call("DEL", tagsKey) |
||
| 159 | -- redis.log(redis.LOG_NOTICE, 'Rimossa chiave tags: ' .. tagsKey); |
||
| 160 | else |
||
| 161 | redis.log(redis.LOG_WARNING, 'la chiave ' .. fullKey .. ' è ancora attiva'); |
||
| 162 | end |
||
| 163 | end |
||
| 164 | end |
||
| 165 | end) |
||
| 166 | if not success then |
||
| 167 | redis.log(redis.LOG_WARNING, "Errore durante l'esecuzione del batch: " .. result) |
||
| 168 | return result; |
||
| 169 | end |
||
| 170 | return "OK" |
||
| 171 | LUA; |
||
| 172 | |||
| 173 | |||
| 174 | try { |
||
| 175 | // Esegue lo script Lua passando le chiavi in batch |
||
| 176 | $connection = $this->redis->getNativeRedisConnection($this->option('connection_name'), $this->option('host'), $this->option('port')); |
||
| 177 | |||
| 178 | $return = $connection['connection']->eval($luaScript, [config('supercache.prefix'), config('supercache.num_shards'), config('database.redis.options')['prefix'], ...$this->batch], 0); |
||
| 179 | if ($return !== 'OK') { |
||
| 180 | Log::error('Errore durante l\'esecuzione dello script Lua: ' . $return); |
||
| 181 | } |
||
| 182 | // Pulisce il batch dopo il successo |
||
| 183 | $this->batch = []; |
||
| 184 | // Essendo una connessione nativa va chiusa |
||
| 185 | $connection['connection']->close(); |
||
| 186 | } catch (\Throwable $e) { |
||
| 187 | Log::error('Errore durante l\'esecuzione dello script Lua: ' . $e->getMessage()); |
||
| 188 | } |
||
| 226 |