diff --git a/pkg/structure/define.go b/pkg/structure/define.go index 375e941..e3078bd 100644 --- a/pkg/structure/define.go +++ b/pkg/structure/define.go @@ -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 } diff --git a/pkg/structure/hashx/hashx.go b/pkg/structure/hashx/hashx.go index 8d6862b..0cb7f36 100644 --- a/pkg/structure/hashx/hashx.go +++ b/pkg/structure/hashx/hashx.go @@ -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 +}