参考 › 函数参考 › utils

utils.getCachedInternal

依赖项

调用方式

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

记录依赖项 utils.getCachedInternal,然后按该名称调用。带命名空间的调用不会传入 self:请使用冒号调用,或自行传入该对象。

作用

来自 utils.lua 的内置辅助函数。

参数

名称类型作用
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.

返回值

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

版本

内置辅助函数在每个版本上都会注册。不同的是它们读取的游戏 API;某个 API 缺失时,辅助函数会改用后备方案,或返回 nil 并附上原因。

它不直接读取任何游戏 API。

秘密值(正式服和永久服)

在正式服和永久服上,游戏交给插件的是秘密值。此辅助函数会检查它读取的内容(addon.Platform:CanReadValue),遇到无法比较的秘密值时返回 nil 并附上原因(例如 restricted-value),而不是返回该秘密值。请将 nil 视为不可用,而不是假。

插件如何调用它

依赖项

源码

Functions/cache.lua,第 57 行。在游戏中为只读;“复制”会生成一份可编辑的副本。

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)

返回函数参考