forked from p93542168/wheat-cache
feat(structure-hashx): add hashx range
This commit is contained in:
parent
366793d955
commit
594f8acf32
|
@ -65,4 +65,5 @@ type HashXInterface interface {
|
||||||
Add(renewal int, key ...string) (int, []string, error) // 访问影响成功的结果
|
Add(renewal int, key ...string) (int, []string, error) // 访问影响成功的结果
|
||||||
SetX(key string, val string) (bool, UpdateLength) // 不存在才插入
|
SetX(key string, val string) (bool, UpdateLength) // 不存在才插入
|
||||||
Length() int
|
Length() int
|
||||||
|
Range(consur, count int, regex string) []string
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package hashx
|
package hashx
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"regexp"
|
||||||
|
|
||||||
"gitee.com/wheat-os/wheatCache/pkg/errorx"
|
"gitee.com/wheat-os/wheatCache/pkg/errorx"
|
||||||
"gitee.com/wheat-os/wheatCache/pkg/structure"
|
"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 {
|
func (h HashX) Length() int {
|
||||||
return len(h)
|
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
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue