Skip to content

Commit b4ab9d7

Browse files
committed
refactor: 优化用户配置存储获取函数流程
1 parent 454dedb commit b4ab9d7

File tree

1 file changed

+57
-51
lines changed

1 file changed

+57
-51
lines changed

Boilerplate_!Base/src/lib/Storage.UserSettings.lua

Lines changed: 57 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,11 @@ end
504504
-- @param {string} szDataSetKey 配置项组(如用户多套自定义偏好)唯一键,当且仅当 szKey 对应注册项携带 bDataSet 标记位时有效
505505
-- @return
506506
function X.GetUserSettings(szKey, szDataSetKey)
507+
local res, bData, bCache = nil, false, false
508+
local info = USER_SETTINGS_INFO[szKey]
509+
if not info then
510+
assert(false, 'GetUserSettings KEY(' .. X.EncodeLUAData(szKey) .. '): `Key` has not been registered.')
511+
end
507512
-- 缓存加速
508513
local cache = DATA_CACHE[szKey]
509514
if szDataSetKey then
@@ -512,46 +517,45 @@ function X.GetUserSettings(szKey, szDataSetKey)
512517
or nil
513518
end
514519
if X.IsTable(cache) and cache.bValue then
515-
return cache.xValue
516-
end
517-
-- 参数检查
518-
local nParameter = select('#', ...) + 1
519-
local info = USER_SETTINGS_INFO[szKey]
520-
if not info then
521-
assert(false, 'GetUserSettings KEY(' .. X.EncodeLUAData(szKey) .. '): `Key` has not been registered.')
520+
res, bData, bCache = cache.xValue, true, true
522521
end
523-
local inst = DATABASE_INSTANCE[info.ePathType]
524-
if not inst then
525-
assert(false, 'GetUserSettings KEY(' .. X.EncodeLUAData(szKey) .. '): Database not connected.')
526-
end
527-
local szDataSetKey
528-
if info.bDataSet then
529-
if nParameter ~= 2 then
530-
assert(false, 'GetUserSettings KEY(' .. X.EncodeLUAData(szKey) .. '): 2 parameters expected, got ' .. nParameter)
531-
end
532-
szDataSetKey = ...
533-
if not X.IsString(szDataSetKey) and not X.IsNumber(szDataSetKey) then
534-
assert(false, 'GetUserSettings KEY(' .. X.EncodeLUAData(szKey) .. '): `DataSetKey` should be a string or number value.')
522+
-- 未命中缓存,从数据库读取
523+
if not bCache then
524+
-- 参数检查
525+
local nParameter = select('#', ...) + 1
526+
local inst = DATABASE_INSTANCE[info.ePathType]
527+
if not inst then
528+
assert(false, 'GetUserSettings KEY(' .. X.EncodeLUAData(szKey) .. '): Database not connected.')
535529
end
536-
else
537-
if nParameter ~= 1 then
538-
assert(false, 'GetUserSettings KEY(' .. X.EncodeLUAData(szKey) .. '): 1 parameter expected, got ' .. nParameter)
539-
end
540-
end
541-
-- 读数据库
542-
local res, bData = GetInstanceInfoData(inst, info), false
543-
if X.IsTable(res) and res.v == info.szVersion then
544-
local data = res.d
530+
local szDataSetKey
545531
if info.bDataSet then
546-
if X.IsTable(data) then
547-
data = data[szDataSetKey]
548-
else
549-
data = nil
532+
if nParameter ~= 2 then
533+
assert(false, 'GetUserSettings KEY(' .. X.EncodeLUAData(szKey) .. '): 2 parameters expected, got ' .. nParameter)
534+
end
535+
szDataSetKey = ...
536+
if not X.IsString(szDataSetKey) and not X.IsNumber(szDataSetKey) then
537+
assert(false, 'GetUserSettings KEY(' .. X.EncodeLUAData(szKey) .. '): `DataSetKey` should be a string or number value.')
538+
end
539+
else
540+
if nParameter ~= 1 then
541+
assert(false, 'GetUserSettings KEY(' .. X.EncodeLUAData(szKey) .. '): 1 parameter expected, got ' .. nParameter)
550542
end
551543
end
552-
if not info.xSchema or not X.Schema.CheckSchema(data, info.xSchema) then
553-
bData = true
554-
res = data
544+
-- 读数据库
545+
res, bData = GetInstanceInfoData(inst, info), false
546+
if X.IsTable(res) and res.v == info.szVersion then
547+
local data = res.d
548+
if info.bDataSet then
549+
if X.IsTable(data) then
550+
data = data[szDataSetKey]
551+
else
552+
data = nil
553+
end
554+
end
555+
if not info.xSchema or not X.Schema.CheckSchema(data, info.xSchema) then
556+
bData = true
557+
res = data
558+
end
555559
end
556560
end
557561
-- 默认值
@@ -566,22 +570,24 @@ function X.GetUserSettings(szKey, szDataSetKey)
566570
end
567571
res = X.Clone(res)
568572
end
569-
-- 缓存
570-
if info.bDataSet then
571-
if not DATA_CACHE[szKey] then
572-
DATA_CACHE[szKey] = { bDataSet = true, tDataSet = {} }
573-
end
574-
DATA_CACHE[szKey].tDataSet[szDataSetKey] = {
575-
bValue = true,
576-
xValue = res,
577-
xRawValue = X.Clone(res),
578-
}
579-
else
580-
DATA_CACHE[szKey] = {
581-
bValue = true,
582-
xValue = res,
583-
xRawValue = X.Clone(res),
584-
}
573+
-- 写入缓存
574+
if not bCache then
575+
if info.bDataSet then
576+
if not DATA_CACHE[szKey] then
577+
DATA_CACHE[szKey] = { bDataSet = true, tDataSet = {} }
578+
end
579+
DATA_CACHE[szKey].tDataSet[szDataSetKey] = {
580+
bValue = true,
581+
xValue = res,
582+
xRawValue = X.Clone(res),
583+
}
584+
else
585+
DATA_CACHE[szKey] = {
586+
bValue = true,
587+
xValue = res,
588+
xRawValue = X.Clone(res),
589+
}
590+
end
585591
end
586592
return res
587593
end

0 commit comments

Comments
 (0)