Skip to content

Commit a96d23c

Browse files
author
lixizan
committed
add test cover rate
1 parent 95f251b commit a96d23c

File tree

7 files changed

+86
-40
lines changed

7 files changed

+86
-40
lines changed

README.md

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
# memorycache
22

3-
[![Build Status](https://github.com/lxzan/memorycache/workflows/Go%20Test/badge.svg?branch=main)](https://github.com/lxzan/memorycache/actions?query=branch%3Amain)
3+
[![Build Status][1]][2] [![codecov][3]][4]
4+
5+
[1]: https://github.com/lxzan/memorycache/workflows/Go%20Test/badge.svg?branch=main
6+
7+
[2]: https://github.com/lxzan/memorycache/actions?query=branch%3Amain
8+
9+
[3]: https://codecov.io/gh/lxzan/memorycache/graph/badge.svg?token=OHD6918OPT
10+
11+
[4]: https://codecov.io/gh/lxzan/memorycache
412

513
### Usage
614
```go
@@ -14,25 +22,35 @@ import (
1422

1523
func main() {
1624
mc := memorycache.New(
17-
memorycache.WithSegment(16),
18-
memorycache.WithTTLCheckInterval(100*time.Millisecond),
25+
memorycache.WithBucketNum(16),
26+
memorycache.WithBucketSize(1000, 100000),
27+
memorycache.WithInterval(100*time.Millisecond),
1928
)
29+
2030
mc.Set("xxx", 1, 500*time.Millisecond)
21-
time.Sleep(time.Second)
31+
2232
val, exist := mc.Get("xxx")
2333
fmt.Printf("val=%v, exist=%v\n", val, exist)
34+
35+
time.Sleep(time.Second)
36+
37+
val, exist = mc.Get("xxx")
38+
fmt.Printf("val=%v, exist=%v\n", val, exist)
2439
}
2540
```
2641

2742
### Benchmark
28-
- 10,000 elements, 2 threads
43+
- 10,000 elements
44+
- 1,000,000 elements
2945
```
46+
go test -benchmem -run=^$ -bench . github.com/lxzan/memorycache/benchmark
3047
goos: darwin
3148
goarch: arm64
3249
pkg: github.com/lxzan/memorycache/benchmark
33-
BenchmarkSet
34-
BenchmarkSet-8 727 1589200 ns/op
35-
BenchmarkGet
36-
BenchmarkGet-8 2191 530433 ns/op
50+
BenchmarkSet/10000-8 13830640 87.25 ns/op 0 B/op 0 allocs/op
51+
BenchmarkSet/1000000-8 3615801 326.6 ns/op 58 B/op 0 allocs/op
52+
BenchmarkGet/10000-8 14347058 82.28 ns/op 0 B/op 0 allocs/op
53+
BenchmarkGet/1000000-8 3899768 262.6 ns/op 54 B/op 0 allocs/op
3754
PASS
55+
ok github.com/lxzan/memorycache/benchmark 13.037s
3856
```

index.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type MemoryCache struct {
1515
storage []*bucket
1616
}
1717

18-
// New 创建缓存数据库
18+
// New 创建缓存数据库实例
1919
func New(options ...Option) *MemoryCache {
2020
var config = &types.Config{}
2121
options = append(options, withInitialize())
@@ -50,7 +50,7 @@ func New(options ...Option) *MemoryCache {
5050
}
5151

5252
func (c *MemoryCache) getBucket(key string) *bucket {
53-
var idx = utils.NewFnv32(key) & (c.config.BucketNum - 1)
53+
var idx = utils.Fnv32(key) & (c.config.BucketNum - 1)
5454
return c.storage[idx]
5555
}
5656

@@ -62,7 +62,7 @@ func (c *MemoryCache) getExp(d time.Duration) int64 {
6262
return time.Now().Add(d).UnixMilli()
6363
}
6464

65-
// 设置键值和过期时间. exp<=0表示永不过期.
65+
// Set 设置键值和过期时间. exp<=0表示永不过期.
6666
func (c *MemoryCache) Set(key string, value any, exp time.Duration) (replaced bool) {
6767
var b = c.getBucket(key)
6868
b.Lock()
@@ -116,7 +116,7 @@ func (c *MemoryCache) GetAndRefresh(key string, exp time.Duration) (any, bool) {
116116
return v, true
117117
}
118118

119-
// 删除一个键
119+
// Delete 删除一个键
120120
func (c *MemoryCache) Delete(key string) (deleted bool) {
121121
var b = c.getBucket(key)
122122
b.Lock()
@@ -132,7 +132,7 @@ func (c *MemoryCache) Delete(key string) (deleted bool) {
132132
return true
133133
}
134134

135-
// 获取前缀匹配的key
135+
// Keys 获取前缀匹配的key, 星号匹配所有
136136
func (c *MemoryCache) Keys(prefix string) []string {
137137
var arr = make([]string, 0)
138138
var now = time.Now().UnixMilli()
@@ -148,7 +148,7 @@ func (c *MemoryCache) Keys(prefix string) []string {
148148
return arr
149149
}
150150

151-
// 获取有效元素个数
151+
// Len 获取有效元素(未过期)数量
152152
func (c *MemoryCache) Len() int {
153153
var num = 0
154154
var now = time.Now().UnixMilli()
@@ -181,7 +181,7 @@ func (c *bucket) expireTimeCheck(maxNum int, maxCap int) {
181181
delete(c.Map, c.Heap.Pop().Key)
182182
num++
183183
}
184-
for c.Heap.Len() > maxNum && num < maxNum {
184+
for c.Heap.Len() > maxCap && num < maxNum {
185185
delete(c.Map, c.Heap.Pop().Key)
186186
num++
187187
}

index_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,17 @@ func TestMemoryCache_Delete(t *testing.T) {
152152
}
153153
assert.Equal(t, mc.Len(), count-100)
154154
}
155+
156+
func TestMaxCap(t *testing.T) {
157+
var mc = New(
158+
WithBucketNum(1),
159+
WithBucketSize(10, 100),
160+
WithInterval(100*time.Millisecond),
161+
)
162+
for i := 0; i < 1000; i++ {
163+
key := string(utils.AlphabetNumeric.Generate(8))
164+
mc.Set(key, 1, -1)
165+
}
166+
time.Sleep(200 * time.Millisecond)
167+
assert.Equal(t, mc.Len(), 100)
168+
}

internal/utils/hash.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ const (
55
prime32 = 16777619
66
)
77

8-
// NewFnv32 returns a new 32-bit FNV-1 hash.Hash.
9-
// Its Sum method will lay the value out in big-endian byte order.
10-
func NewFnv32(s string) uint32 {
8+
// Fnv32 returns a new 32-bit FNV-1 hash.Hash.
9+
func Fnv32(s string) uint32 {
1110
var hash uint32 = offset32
1211
for _, c := range s {
1312
hash *= prime32

internal/utils/helper.go

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,9 @@
11
package utils
22

3-
import (
4-
"sort"
5-
)
6-
73
type Integer interface {
84
int | int64 | int32 | uint | uint64 | uint32
95
}
106

11-
func SameStrings(arr1, arr2 []string) bool {
12-
sort.Strings(arr1)
13-
sort.Strings(arr2)
14-
var n = len(arr1)
15-
if n != len(arr2) {
16-
return false
17-
}
18-
for i := 0; i < n; i++ {
19-
if arr1[i] != arr2[i] {
20-
return false
21-
}
22-
}
23-
return true
24-
}
25-
267
func ToBinaryNumber[T Integer](n T) T {
278
var x T = 1
289
for x < n {

internal/utils/helper_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package utils
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"hash/fnv"
6+
"testing"
7+
)
8+
9+
func TestNewFnv32(t *testing.T) {
10+
for i := 0; i < 10; i++ {
11+
key := AlphabetNumeric.Generate(16)
12+
h := fnv.New32()
13+
h.Write(key)
14+
assert.Equal(t, h.Sum32(), Fnv32(string(key)))
15+
}
16+
}
17+
18+
func TestToBinaryNumber(t *testing.T) {
19+
assert.Equal(t, 8, ToBinaryNumber(7))
20+
assert.Equal(t, 1, ToBinaryNumber(0))
21+
assert.Equal(t, 128, ToBinaryNumber(120))
22+
assert.Equal(t, 1024, ToBinaryNumber(1024))
23+
}
24+
25+
func TestUniq(t *testing.T) {
26+
assert.ElementsMatch(t, Uniq([]int{1, 3, 5, 7, 7, 9}), []int{1, 3, 5, 7, 9})
27+
assert.ElementsMatch(t, Uniq([]string{"ming", "ming", "shi"}), []string{"ming", "shi"})
28+
}
29+
30+
func TestRandomString(t *testing.T) {
31+
assert.Less(t, Numeric.Intn(10), 10)
32+
Numeric.Uint32()
33+
Numeric.Uint64()
34+
}

options.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ func WithBucketNum(num uint32) Option {
2323
}
2424
}
2525

26-
// WithMaxKeysDeleted 设置单个存储桶每次TTL检查最大删除key数量
26+
// WithMaxKeysDeleted (单个存储桶)设置每次TTL检查最大删除key数量
2727
func WithMaxKeysDeleted(num int) Option {
2828
return func(c *types.Config) {
2929
c.MaxKeysDeleted = num
3030
}
3131
}
3232

33-
// WithBucketSize 设置单个存储桶的初始化大小, 最大容量
33+
// WithBucketSize (单个存储桶)设置初始化大小, 最大容量. 超过最大容量会被定期清除.
3434
func WithBucketSize(size, cap int) Option {
3535
return func(c *types.Config) {
3636
c.InitialSize = size

0 commit comments

Comments
 (0)