Module:Cache

local trim = mw.text.trim local cache = require 'mw.ext.LuaCache'

local args_table -- cache -- helper function local getArg = function(key) local value = trim(args_table[key] or '') --Note: unnamed parameters will not be trimmed and named parameters will. return (value ~= '') and value or nil end

-- A simple wrap for LuaCache

return { -- get -- return cached value or nothing get = function(frame) args_table = frame.args local result = cache.get(getArg(1) or getArg('key')) if result then return tostring(result) end end,

-- get & replace -- return cached value with all tokens replaced, or nothing getreplace = function(frame) args_table = frame.args local result = cache.get(getArg(1) or getArg('key')) if not result then return end result = tostring(result) for token, replace in pairs(args_table) do			if mw.ustring.sub(token, 1, 1) == '$' then -- escape, to perform plain text replacement. see Module:String token = mw.ustring.gsub(token, "([%(%)%.%%%+%-%*%?%[%^%$%]])", "%%%1") replace = mw.ustring.gsub(replace, "%%", "%%%%" ) result = mw.ustring.gsub(result, token, replace) end end end,

-- set -- return 'true' or '' if $return or $4 = true set = function(frame) args_table = frame.args local result = cache.set(getArg(1) or getArg('key'), getArg(2) or getArg('value'), getArg(3) or getArg('exptime')) if getArg('return') or getArg(4) then return result or '' end end,

-- delete -- return 'true' or '' if $return or $2 = true delete = function(frame) args_table = frame.args local result = cache.delete(getArg(1) or getArg('key')) if getArg('return') or getArg(2) then return result or '' end end,

}