/
internal_kvstore.go
100 lines (85 loc) · 2.26 KB
/
internal_kvstore.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright (C) 2023 wwhai
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package core
import (
"strings"
"time"
"github.com/hootrhino/rulex/glogger"
"github.com/hootrhino/rulex/typex"
cache "github.com/wwhai/tinycache"
)
var GlobalStore typex.XStore
func StartStore(maxSize int) {
GlobalStore = NewRulexStore(maxSize)
}
type RulexStore struct {
cache *cache.Cache
}
func NewRulexStore(maxSize int) typex.XStore {
return &RulexStore{
cache: cache.New(0, 0),
}
}
/*
*
* 设置过期时间
*
*/
func (rs *RulexStore) SetWithDuration(k string, v string, d time.Duration) {
if (rs.cache.ItemCount() + 1) > 10000 {
glogger.GLogger.Error("Max store size reached:", rs.cache.ItemCount())
}
rs.cache.Set(k, v, d)
}
// 设置值
func (rs *RulexStore) Set(k string, v string) {
if (rs.cache.ItemCount() + 1) > 10000 {
glogger.GLogger.Error("Max store size reached:", rs.cache.ItemCount())
}
rs.cache.Set(k, v, -1)
}
// 获取值
func (rs *RulexStore) Get(k string) string {
v, ok := rs.cache.Get(k)
if ok {
return v.(string)
} else {
return ""
}
}
func (rs *RulexStore) Delete(k string) error {
rs.cache.Delete(k)
return nil
}
// 统计数量
func (rs *RulexStore) Count() int {
return rs.cache.ItemCount()
}
// 模糊查询匹配
// 支持: *AAA AAA* A*B
func (rs *RulexStore) FuzzyGet(Key string) any {
for k, v := range rs.cache.Items() {
if fuzzyMatch(k, Key) {
return v
}
}
return ""
}
// 将主字符串和子字符串都转换为小写,以进行不区分大小写的匹配
func fuzzyMatch(mainStr, subStr string) bool {
mainStr = strings.ToLower(mainStr)
subStr = strings.ToLower(subStr)
return strings.Contains(mainStr, subStr)
}