Referencia › Referencia de funciones › utils

utils.getCachedInternal

dependencia

Llamar como

utils.getCachedInternal(cache, recordnil, func, ...)

Registra la dependencia utils.getCachedInternal y después llámala con ese nombre. Una llamada con espacio de nombres no pasa self: usa una llamada con dos puntos o pasa el objeto tú mismo.

Qué hace

Función auxiliar incluida de utils.lua.

Argumentos

NombreTipoQué hace
cachetable | nilEvaluation-pass cache, or nil to call func directly without caching.
recordnilbooleanTrue to cache every result, including all-nil ones (getCached); false to cache only results with a non-nil value (getRetryCached).
funcfunction | stringFunction to call, or the name of a global function; anything else returns nothing.
...anyArguments 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.

Ediciones

Las funciones auxiliares integradas se registran en todas las ediciones. Lo que cambia es la API del juego que leen; donde falta alguna, la función auxiliar usa una alternativa o devuelve nil con un motivo.

No lee directamente ninguna API del juego.

Valores secretos (Retail y Forever)

En Retail y Forever, el juego les entrega a los addons valores secretos. Esta función auxiliar verifica lo que lee (addon.Platform:CanReadValue) y, en lugar de un secreto que no puede comparar, devuelve nil con un motivo como restricted-value. Trata nil como no disponible, no como falso.

Cómo la llama el addon

Dependencias

Código fuente

Functions/cache.lua, línea 57. Solo lectura en el juego; Duplicar crea una copia editable.

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)

Volver a Referencia de funciones