Chame como
utils.getCachedInternal(cache, recordnil, func, ...)
Registre a dependência utils.getCachedInternal e depois chame-a por esse nome. Uma chamada com namespace não passa self: use uma chamada com dois-pontos ou passe o objeto você mesmo.
O que faz
Função auxiliar incluída de utils.lua.
Argumentos
| Nome | Tipo | O que faz |
|---|---|---|
cache | table | nil | Evaluation-pass cache, or nil to call func directly without caching. |
recordnil | boolean | True to cache every result, including all-nil ones (getCached); false to cache only results with a non-nil value (getRetryCached). |
func | function | string | Function to call, or the name of a global function; anything else returns nothing. |
... | any | Arguments forwarded to func and used as the exact cache key; a restricted or NaN argument bypasses the cache. |
Retornos
| 1 | ... | Every result of func, preserving nil positions and the return count; cached results are replayed on later calls with the same arguments. |
|---|
Edições
As funções auxiliares integradas são registradas em todas as edições. O que muda é a API do jogo que elas leem; quando uma está ausente, a função auxiliar recorre a uma alternativa ou retorna nil com um motivo.
Não lê nenhuma API do jogo diretamente.
Valores secretos (Retail e Forever)
No Retail e no Forever, o jogo entrega valores secretos aos addons. Esta função auxiliar verifica o que lê (addon.Platform:CanReadValue) e retorna nil com um motivo como restricted-value em vez de um valor secreto que não consegue comparar. Trate nil como indisponível, não como false.
Como o addon a chama
Functions.cache.getCachedreturn getCachedInternal(cache, true, func, ...)Functions.cache.getRetryCachedreturn getCachedInternal(cache, false, func, ...)
Dependências
_G(builtin:utils.lua:_G)cacheHits(builtin:utils.lua:cacheHits)cacheMisses(builtin:utils.lua:cacheMisses)cachedResult(builtin:utils.lua:cachedResult)nilArgument(builtin:utils.lua:nilArgument)packResults→utils.packResults
Código-fonte
Functions/cache.lua, linha 57. Somente leitura no jogo; Duplicar cria uma cópia editável.
utils.getCachedInternal(cache, recordnil, func, ...)
if type(func) == 'string' then func = _G[func] end
if type(func) ~= 'function' then return end
if cache == nil then return func(...) end
-- Exact keys preserve argument types, identity and arity. Hashing concatenated
-- strings allowed unrelated observations to collide, even for numeric IDs.
local args, count = { ... }, select('#', ...)
for i = 1, count do
local value = args[i]
if addon.Platform and addon.Platform.CanReadValue and not addon.Platform:CanReadValue(value) then
return func(...)
end
if type(value) == 'number' and value ~= value then return func(...) end
if value == nil then args[i] = nilArgument end
end
local node = cache[func]
for i = 1, count do
if not node then break end
node = node[args[i]]
end
local result = node and node[cachedResult]
if result then
cacheHits = cacheHits + 1
return unpack(result, 1, result.n)
end
cacheMisses = cacheMisses + 1
result = packResults(func(...))
local store = recordnil
if not store then
-- Inspect the packed keys, not potentially restricted return values.
for key in pairs(result) do
if type(key) == 'number' then store = true; break end
end
end
if store then
-- Retry misses and thrown calls must not accumulate empty paths.
if not cache[func] then cache[func] = {} end
node = cache[func]
for i = 1, count do
local key = args[i]
if not node[key] then node[key] = {} end
node = node[key]
end
node[cachedResult] = result
end
return unpack(result, 1, result.n)