Reference › Function reference › utils

utils.getCachedInternal

dependency

Call as

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

Record the dependency utils.getCachedInternal, then call it by that name. A namespaced call does not pass self: use a colon call or pass the object yourself.

What it does

Packaged helper from utils.lua.

Arguments

NameTypeWhat it does
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.

Returns

1...Every result of func, preserving nil positions and the return count; cached results are replayed on later calls with the same arguments.

Editions

Built In helpers are registered on every edition. What differs is the game API they read; where one is missing the helper falls back or returns nil with a reason.

It reads no game API directly.

Secret values (Retail and Forever)

On Retail and Forever the game hands addons secret values. This helper checks what it reads (addon.Platform:CanReadValue) and returns nil with a reason such as restricted-value instead of a secret it cannot compare. Treat nil as unavailable, not as false.

How the addon calls it

Dependencies

Source

Functions/cache.lua, line 57. Read only in game; Duplicate makes an editable copy.

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)

Back to Function reference