呼叫方式
utils.getCachedInternal(cache, recordnil, func, ...)
記錄相依項目 utils.getCachedInternal,然後以該名稱呼叫。含命名空間的呼叫不會傳入 self:請使用冒號呼叫,或自行傳入該物件。
作用
來自 utils.lua 的內建輔助函式。
參數
| 名稱 | 類型 | 作用 |
|---|---|---|
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. |
傳回值
| 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.getCachedreturn getCachedInternal(cache, true, func, ...)Functions.cache.getRetryCachedreturn getCachedInternal(cache, false, func, ...)
相依項目
_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
原始碼
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)