Skip to content

Commit 232c37a

Browse files
author
lixizan
committed
add docs
1 parent 4810a47 commit 232c37a

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010

1111
[4]: https://codecov.io/gh/lxzan/memorycache
1212

13+
### Description
14+
Minimalist in-memory KV storage, powered by hashmap and minimal heap, with no special optimizations for GC.
15+
It has O(1) read efficiency, O(logN) write efficiency.
16+
Cache deprecation policy: obsolete or overflowed keys are flushed, with a 30s (default) check.
17+
1318
### Usage
1419
```go
1520
package main

index.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type MemoryCache struct {
1616
}
1717

1818
// New 创建缓存数据库实例
19+
// Creating a Cached Database Instance
1920
func New(options ...Option) *MemoryCache {
2021
var config = &types.Config{}
2122
options = append(options, withInitialize())
@@ -63,6 +64,7 @@ func (c *MemoryCache) getExp(d time.Duration) int64 {
6364
}
6465

6566
// Set 设置键值和过期时间. exp<=0表示永不过期.
67+
// Set the key value and expiration time. exp<=0 means never expire.
6668
func (c *MemoryCache) Set(key string, value any, exp time.Duration) (replaced bool) {
6769
var b = c.getBucket(key)
6870
b.Lock()
@@ -88,7 +90,7 @@ func (c *MemoryCache) Set(key string, value any, exp time.Duration) (replaced bo
8890
return false
8991
}
9092

91-
// Get 获取
93+
// Get
9294
func (c *MemoryCache) Get(key string) (any, bool) {
9395
var b = c.getBucket(key)
9496
b.Lock()
@@ -101,6 +103,7 @@ func (c *MemoryCache) Get(key string) (any, bool) {
101103
}
102104

103105
// GetAndRefresh 获取. 如果存在, 刷新过期时间.
106+
// Get a value. If it exists, refreshes the expiration time.
104107
func (c *MemoryCache) GetAndRefresh(key string, exp time.Duration) (any, bool) {
105108
var b = c.getBucket(key)
106109
b.Lock()
@@ -116,7 +119,7 @@ func (c *MemoryCache) GetAndRefresh(key string, exp time.Duration) (any, bool) {
116119
return v, true
117120
}
118121

119-
// Delete 删除一个键
122+
// Delete
120123
func (c *MemoryCache) Delete(key string) (deleted bool) {
121124
var b = c.getBucket(key)
122125
b.Lock()
@@ -132,7 +135,8 @@ func (c *MemoryCache) Delete(key string) (deleted bool) {
132135
return true
133136
}
134137

135-
// Keys 获取前缀匹配的key, 星号匹配所有
138+
// Keys 获取前缀匹配的key. 可以通过星号获取所有的key.
139+
// Get prefix matching key, You can get all the keys with an asterisk.
136140
func (c *MemoryCache) Keys(prefix string) []string {
137141
var arr = make([]string, 0)
138142
var now = time.Now().UnixMilli()
@@ -148,15 +152,21 @@ func (c *MemoryCache) Keys(prefix string) []string {
148152
return arr
149153
}
150154

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 {
153159
var num = 0
154160
var now = time.Now().UnixMilli()
155161
for _, b := range c.storage {
156162
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+
}
160170
}
161171
}
162172
b.Unlock()

index_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func TestNew(t *testing.T) {
4646
db.Set("d", 1, 40*time.Millisecond)
4747

4848
time.Sleep(50 * time.Millisecond)
49-
as.Equal(0, db.Len())
49+
as.Equal(0, db.Len(true))
5050
})
5151
}
5252

@@ -150,7 +150,7 @@ func TestMemoryCache_Delete(t *testing.T) {
150150
deleted = mc.Delete(key)
151151
assert.False(t, deleted)
152152
}
153-
assert.Equal(t, mc.Len(), count-100)
153+
assert.Equal(t, mc.Len(true), count-100)
154154
}
155155

156156
func TestMaxCap(t *testing.T) {
@@ -164,5 +164,5 @@ func TestMaxCap(t *testing.T) {
164164
mc.Set(key, 1, -1)
165165
}
166166
time.Sleep(200 * time.Millisecond)
167-
assert.Equal(t, mc.Len(), 100)
167+
assert.Equal(t, mc.Len(false), 100)
168168
}

0 commit comments

Comments
 (0)