@@ -16,6 +16,7 @@ type MemoryCache struct {
16
16
}
17
17
18
18
// New 创建缓存数据库实例
19
+ // Creating a Cached Database Instance
19
20
func New (options ... Option ) * MemoryCache {
20
21
var config = & types.Config {}
21
22
options = append (options , withInitialize ())
@@ -63,6 +64,7 @@ func (c *MemoryCache) getExp(d time.Duration) int64 {
63
64
}
64
65
65
66
// Set 设置键值和过期时间. exp<=0表示永不过期.
67
+ // Set the key value and expiration time. exp<=0 means never expire.
66
68
func (c * MemoryCache ) Set (key string , value any , exp time.Duration ) (replaced bool ) {
67
69
var b = c .getBucket (key )
68
70
b .Lock ()
@@ -88,7 +90,7 @@ func (c *MemoryCache) Set(key string, value any, exp time.Duration) (replaced bo
88
90
return false
89
91
}
90
92
91
- // Get 获取
93
+ // Get
92
94
func (c * MemoryCache ) Get (key string ) (any , bool ) {
93
95
var b = c .getBucket (key )
94
96
b .Lock ()
@@ -101,6 +103,7 @@ func (c *MemoryCache) Get(key string) (any, bool) {
101
103
}
102
104
103
105
// GetAndRefresh 获取. 如果存在, 刷新过期时间.
106
+ // Get a value. If it exists, refreshes the expiration time.
104
107
func (c * MemoryCache ) GetAndRefresh (key string , exp time.Duration ) (any , bool ) {
105
108
var b = c .getBucket (key )
106
109
b .Lock ()
@@ -116,7 +119,7 @@ func (c *MemoryCache) GetAndRefresh(key string, exp time.Duration) (any, bool) {
116
119
return v , true
117
120
}
118
121
119
- // Delete 删除一个键
122
+ // Delete
120
123
func (c * MemoryCache ) Delete (key string ) (deleted bool ) {
121
124
var b = c .getBucket (key )
122
125
b .Lock ()
@@ -132,7 +135,8 @@ func (c *MemoryCache) Delete(key string) (deleted bool) {
132
135
return true
133
136
}
134
137
135
- // Keys 获取前缀匹配的key, 星号匹配所有
138
+ // Keys 获取前缀匹配的key. 可以通过星号获取所有的key.
139
+ // Get prefix matching key, You can get all the keys with an asterisk.
136
140
func (c * MemoryCache ) Keys (prefix string ) []string {
137
141
var arr = make ([]string , 0 )
138
142
var now = time .Now ().UnixMilli ()
@@ -148,15 +152,21 @@ func (c *MemoryCache) Keys(prefix string) []string {
148
152
return arr
149
153
}
150
154
151
- // Len 获取有效元素(未过期)数量
152
- func (c * MemoryCache ) Len () int {
155
+ // Len 获取元素数量
156
+ // Get the number of elements
157
+ // @check: 是否检查过期时间 (whether to check expiration time)
158
+ func (c * MemoryCache ) Len (check bool ) int {
153
159
var num = 0
154
160
var now = time .Now ().UnixMilli ()
155
161
for _ , b := range c .storage {
156
162
b .Lock ()
157
- for _ , v := range b .Heap .Data {
158
- if ! v .Expired (now ) {
159
- num ++
163
+ if ! check {
164
+ num += b .Heap .Len ()
165
+ } else {
166
+ for _ , v := range b .Heap .Data {
167
+ if ! v .Expired (now ) {
168
+ num ++
169
+ }
160
170
}
161
171
}
162
172
b .Unlock ()
0 commit comments