perf: use ZRangeWithScores to scan partial for ZSet

This commit is contained in:
Lykin 2023-11-24 00:22:40 +08:00
parent 66a057f504
commit 79c943f85d
2 changed files with 24 additions and 14 deletions

View File

@ -839,9 +839,10 @@ func (b *browserService) GetKeyDetail(param types.KeyDetailParam) (resp types.JS
var reset bool var reset bool
var cursor uint64 var cursor uint64
scanSize := int64(Preferences().GetScanSize()) scanSize := int64(Preferences().GetScanSize())
var loadedVal []string doFilter := matchPattern != "*"
if param.Full || matchPattern != "*" { if param.Full || doFilter {
// load all // load all
var loadedVal []string
cursor, reset = 0, true cursor, reset = 0, true
for { for {
loadedVal, cursor, err = client.ZScan(ctx, key, cursor, matchPattern, scanSize).Result() loadedVal, cursor, err = client.ZScan(ctx, key, cursor, matchPattern, scanSize).Result()
@ -872,18 +873,26 @@ func (b *browserService) GetKeyDetail(param types.KeyDetailParam) (resp types.JS
} else { } else {
cursor, _, reset = getEntryCursor() cursor, _, reset = getEntryCursor()
} }
loadedVal, cursor, err = client.ZScan(ctx, key, cursor, matchPattern, scanSize).Result() var loadedVal []redis.Z
loadedLen := len(loadedVal) loadedVal, err = client.ZRangeWithScores(ctx, key, int64(cursor), int64(cursor)+scanSize-1).Result()
items = make([]types.ZSetEntryItem, loadedLen/2) cursor = cursor + uint64(scanSize)
var score float64 if len(loadedVal) < int(scanSize) {
for i := 0; i < loadedLen; i += 2 { cursor = 0
if score, err = strconv.ParseFloat(loadedVal[i+1], 64); err == nil { }
items[i/2].Score = score
items[i/2].Value = loadedVal[i] items = make([]types.ZSetEntryItem, 0, len(loadedVal))
if doConvert { for _, z := range loadedVal {
if dv, _, _ := strutil.ConvertTo(loadedVal[i], param.Decode, param.Format); dv != loadedVal[i] { val := strutil.AnyToString(z.Score, "", 0)
items[i/2].DisplayValue = dv if doFilter && !strings.Contains(val, param.MatchPattern) {
} continue
}
items = append(items, types.ZSetEntryItem{
Score: z.Score,
Value: val,
})
if doConvert {
if dv, _, _ := strutil.ConvertTo(val, param.Decode, param.Format); dv != val {
items[len(items)-1].DisplayValue = dv
} }
} }
} }

View File

@ -85,6 +85,7 @@ const scoreColumn = computed(() => ({
align: 'center', align: 'center',
titleAlign: 'center', titleAlign: 'center',
resizable: true, resizable: true,
sorter: (row1, row2) => row1.s - row2.s,
// filterOptionValue: scoreFilterOption.value, // filterOptionValue: scoreFilterOption.value,
// filter(value, row) { // filter(value, row) {
// const score = parseFloat(row.s) // const score = parseFloat(row.s)