Référence › Référence des fonctions › utils

utils.getCachedInternal

dépendance

Appel

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

Enregistrez la dépendance utils.getCachedInternal, puis appelez-la sous ce nom. Un appel par espace de noms ne transmet pas self : utilisez un appel avec deux-points ou passez l'objet vous-même.

Ce qu'elle fait

Fonction utilitaire fournie par utils.lua.

Arguments

NomTypeCe qu'elle fait
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.

Retours

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

Éditions

Les fonctions utilitaires intégrées sont enregistrées sur toutes les éditions. Ce qui diffère, c'est l'API du jeu qu'elles lisent ; quand l'une manque, la fonction utilitaire se rabat sur une alternative ou renvoie nil avec une raison.

Elle ne lit directement aucune API du jeu.

Valeurs secrètes (Retail et Forever)

Sur Retail et Forever, le jeu transmet aux addons des valeurs secrètes. Cette fonction utilitaire vérifie ce qu'elle lit (addon.Platform:CanReadValue) et renvoie nil avec une raison telle que restricted-value au lieu d'une valeur secrète qu'elle ne peut pas comparer. Traitez nil comme indisponible, pas comme false.

Comment l'addon l'appelle

Dépendances

Source

Functions/cache.lua, ligne 57. En lecture seule dans le jeu ; Dupliquer crée une copie modifiable.

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)

Retour à Référence des fonctions