Module:VarsCacheMap

local util_vars = require('Module:VariablesLuaUtil') local makeClass = require('Module:Class') require('Module:LibraryUtil') local cache = require('mw.ext.LuaCache')

local p = {}

local lang = mw.getLanguage('en')

local Cache = makeClass "Cache" function Cache.prototype:constructor(attrs) checkType(1, attrs, 'table', true) attrs = attrs or {} self.prefix = attrs.prefix self.dataModule = attrs.dataModule return self end

-- localKey is the key found in the source data file function Cache.prototype:get(localKey) local key = self.prefix .. lang:lc(localKey) -- Get from variable storage (page storage, fastest) local data = util_vars.getObject(key) if not data then -- Else get from cache (site storage, slower but still fast) data = cache.get(key) if not data then -- Otherwise fallback to accessing data from `loadData` -- We shouldn't hit here ideally, but best to have a fallback -- Note: cache should be pre-populated to avoid falling back here, this is only meant as a backup data = mw.loadData('Module:'..self.dataModule)[localKey] -- update cache for future calls cache.set(key, data) end -- update vars for future calls util_vars.setObject(key, data) end return data end

function p.create(attrs) return Cache(attrs) end

return p