More actions
dustloop>Alistair3149 Created page with "local cargo = {} local extCargo = mw.ext.cargo local checkType = require( 'libraryUtil' ).checkType --- Return the result of a Cargo query (#cargo_query) --- TODO: Move to a shared module since this will be reused --- --- For info on the parameters: --- @see https://www.mediawiki.org/wiki/Extension:Cargo/Other_features#Lua_support --- --- @param tables string The set of Cargo tables to be queried --- @param fields string The field or set of fields to be displayed --- @..." |
dustloop>Alistair3149 Add cache handling |
||
| Line 2: | Line 2: | ||
local extCargo = mw.ext.cargo | local extCargo = mw.ext.cargo | ||
local checkType = require( 'libraryUtil' ).checkType | local checkType = require( 'libraryUtil' ).checkType | ||
local cache = {} | |||
--- Flatten key value pairs table into string | |||
--- | |||
--- @param t table Table to be flatten | |||
--- @return string | |||
local function flattenTableToString( t ) | |||
local stringTable = {} | |||
for k, v in pairs( t ) do | |||
table.insert( stringTable, string.format( '%s:%s', k, v ) ) | |||
end | |||
return table.concat( stringTable, ',' ) | |||
end | |||
--- Cache Cargo query result | |||
--- | |||
--- @param results table Results from Cargo query | |||
--- @param tables string Tables param from Cargo query | |||
--- @param fields string Fields param from Cargo query | |||
--- @param argsString string Flatten args param from Cargo query | |||
local function cacheQueryResults( results, tables, fields, argsString ) | |||
-- Init index | |||
if not cache[ tables ] then | |||
cache[ tables ] = {} | |||
end | |||
if not cache[ tables ][ fields ] then | |||
cache[ tables ][ fields ] = {} | |||
end | |||
if not cache[ tables ][ fields ][ argsString ] then | |||
cache[ tables ][ fields ][ argsString ] = {} | |||
end | |||
mw.log( string.format( '[Cargo] Cached Cargo query result at: cache[\'%s\'][\'%s\'][\'%s\']', tables, fields, argsString ) ) | |||
cache[ tables ][ fields ][ argsString ] = results | |||
end | |||
| Line 22: | Line 58: | ||
checkType( 'Module:Cargo/cargo.getQueryResults', 3, args, 'table' ) | checkType( 'Module:Cargo/cargo.getQueryResults', 3, args, 'table' ) | ||
local argsString = flattenTableToString( args ) | |||
if cache[ tables ] and cache[ tables ][ fields ] and cache[ tables ][ fields ][ argsString ] then | |||
return cache[ tables ][ fields ][ argsString ] | |||
end | |||
local success, results = pcall( extCargo.query, tables, fields, args ) | |||
mw.logObject( { tables, fields, args }, '[Cargo] Run Cargo query with the following params' ) | |||
if not success then | if not success then | ||
error( 'Failed to run cargo query at getQueryResults()') | error( 'Failed to run cargo query at getQueryResults()' ) | ||
end | end | ||
mw.logObject( | mw.logObject( results, '[Cargo] Found Cargo query result' ) | ||
cacheQueryResults( results, tables, fields, argsString ) | |||
return results | |||
end | end | ||
return cargo | return cargo | ||
Revision as of 19:52, 11 May 2024
Documentation for this module may be created at Module:Cargo/doc
local cargo = {}
local extCargo = mw.ext.cargo
local checkType = require( 'libraryUtil' ).checkType
local cache = {}
--- Flatten key value pairs table into string
---
--- @param t table Table to be flatten
--- @return string
local function flattenTableToString( t )
local stringTable = {}
for k, v in pairs( t ) do
table.insert( stringTable, string.format( '%s:%s', k, v ) )
end
return table.concat( stringTable, ',' )
end
--- Cache Cargo query result
---
--- @param results table Results from Cargo query
--- @param tables string Tables param from Cargo query
--- @param fields string Fields param from Cargo query
--- @param argsString string Flatten args param from Cargo query
local function cacheQueryResults( results, tables, fields, argsString )
-- Init index
if not cache[ tables ] then
cache[ tables ] = {}
end
if not cache[ tables ][ fields ] then
cache[ tables ][ fields ] = {}
end
if not cache[ tables ][ fields ][ argsString ] then
cache[ tables ][ fields ][ argsString ] = {}
end
mw.log( string.format( '[Cargo] Cached Cargo query result at: cache[\'%s\'][\'%s\'][\'%s\']', tables, fields, argsString ) )
cache[ tables ][ fields ][ argsString ] = results
end
--- Return the result of a Cargo query (#cargo_query)
--- TODO: Move to a shared module since this will be reused
---
--- For info on the parameters:
--- @see https://www.mediawiki.org/wiki/Extension:Cargo/Other_features#Lua_support
---
--- @param tables string The set of Cargo tables to be queried
--- @param fields string The field or set of fields to be displayed
--- @param args table Optional parameters for the query
--- @return table
function cargo.getQueryResults( tables, fields, args )
-- args are optional
args = args or {}
checkType( 'Module:Cargo/cargo.getQueryResults', 1, tables, 'string' )
checkType( 'Module:Cargo/cargo.getQueryResults', 2, fields, 'string' )
checkType( 'Module:Cargo/cargo.getQueryResults', 3, args, 'table' )
local argsString = flattenTableToString( args )
if cache[ tables ] and cache[ tables ][ fields ] and cache[ tables ][ fields ][ argsString ] then
return cache[ tables ][ fields ][ argsString ]
end
local success, results = pcall( extCargo.query, tables, fields, args )
mw.logObject( { tables, fields, args }, '[Cargo] Run Cargo query with the following params' )
if not success then
error( 'Failed to run cargo query at getQueryResults()' )
end
mw.logObject( results, '[Cargo] Found Cargo query result' )
cacheQueryResults( results, tables, fields, argsString )
return results
end
return cargo