hypixel skyblock
Lock
Page Protected from Editing
This page is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, it is subject to Page Protection.

This is a class that lets you access data from the cache for a given prefix.

How it works

It looks for data in the following order:

  • VariablesLua (util) - this is essentially a page-level cache that last until the end of the page render (speeds up accessing data fetched previously on the same page).
  • LuaCache - this is a site-level cache that stores anything loaded into it previously. See Module:Cache for more details on refreshing this cache.
  • If no data is in the cache, we default back to using mw.loadData to load the data and fetch it normally
At each stage, the cache will we update it nothing was available. So if nothing was in the lua cache, it will be placed into the lua cache for the next time.Module:VarsCacheMap/doc

Module Code

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

local p = {}

local lang = mw.getLanguage('en')

local Cache = makeClass "Cache"

local checkType = libU.checkType

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

function Cache.prototype:makeKey(localKey)
	return self.prefix .. lang:lc(localKey)
end

-- localKey is the key found in the source data file
-- Modes: 0-Default 1-No Fallback (not loadData in any circumstanace)
function Cache.prototype:get(localKey, mode)
	mode = mode or 0
	local key = self:makeKey(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 and mode ~= 1 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

-- Note: this shouldn't be used inside normal modals, and should only be called by refresh scripts
function Cache.prototype:addAllDataToCache()
	local data = require('Module:'..self.dataModule) -- has to be a require to allow looping
	for key, val in pairs(data) do
		-- mw.log( self:makeKey(key) )
		cache.set(self:makeKey(key), val)
	end
end

-- Deletes all keys pertaining to this cache, to allow for a cleaner refresh
-- Note: this shouldn't be used inside normal modals, and should only be called by refresh scripts
function Cache.prototype:deleteCache()
	local data = require('Module:'..self.dataModule) -- has to be a require to allow looping
	for key, _ in pairs(data) do
		cache.delete(self:makeKey(key))
	end
end

function Cache.prototype:refreshCache()
	self:deleteCache()
	self:addAllDataToCache()
end

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

return p