Module:NecesseCache

From Necesse Wiki
Jump to navigation Jump to search

Documentation for this module may be created at Module:NecesseCache/doc

local p = {}

function p.loadItems()
	return load('necesse_items', 'Module:NecesseItems/data')
end

function p.purgeItems()
	mw.ext.LuaCache.delete('necesse_items')
end

function p.loadRecipes()
	return load('necesse_recipes', 'Module:NecesseRecipes/data')
end

function p.purgeRecipes()
	mw.ext.LuaCache.delete('necesse_recipes')
end

function load(cache, module)
	-- First try to load it from the cache
	local success, result = pcall(function()
		return mw.text.jsonDecode(mw.ext.LuaCache.get(cache))
	end)
	if success and result then
		return result
	end
	
	-- Now load it from the module and cache it
	local data = require(module)
	mw.ext.LuaCache.set(cache, mw.text.jsonEncode(data))
	return data
end

return p