feat(structure-hashx): add hashx range

This commit is contained in:
bandl 2021-11-15 23:57:39 +08:00
parent 366793d955
commit 594f8acf32
2 changed files with 38 additions and 0 deletions

View File

@ -65,4 +65,5 @@ type HashXInterface interface {
Add(renewal int, key ...string) (int, []string, error) // 访问影响成功的结果
SetX(key string, val string) (bool, UpdateLength) // 不存在才插入
Length() int
Range(consur, count int, regex string) []string
}

View File

@ -1,6 +1,8 @@
package hashx
import (
"regexp"
"gitee.com/wheat-os/wheatCache/pkg/errorx"
"gitee.com/wheat-os/wheatCache/pkg/structure"
)
@ -118,3 +120,38 @@ func (h HashX) SetX(key string, val string) (bool, structure.UpdateLength) {
func (h HashX) Length() int {
return len(h)
}
func (h HashX) Range(consur, count int, regex string) []string {
var reComp *regexp.Regexp
if regex == "" {
reComp = nil
} else {
reComp = regexp.MustCompile(regex)
}
result := make([]string, 0)
for _, val := range h {
if consur > 0 {
consur--
continue
}
if count == 0 && count != -1 {
break
}
s := val.ToString()
if reComp == nil {
count--
result = append(result, s)
continue
}
if reComp.MatchString(s) {
count--
result = append(result, s)
}
}
return result
}