-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache_test.go
61 lines (46 loc) · 1.32 KB
/
cache_test.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
package cache_test
import (
"fmt"
"math/rand"
"testing"
"time"
"github.com/janqx/go-cache"
)
const keyChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
func GenerateKey() string {
result := ""
for i := 0; i < 8; i++ {
result += string(keyChars[rand.Intn(len(keyChars))])
}
return result
}
func Test_HashCache(t *testing.T) {
start := time.Now()
c := cache.NewHashCache(1*time.Minute, 1*time.Second)
addCount := 0
removeCount := 0
expiredCount := 0
c.AddPutListener(func(key string, value interface{}) {
addCount++
})
c.AddRemoveListener(func(key string, value interface{}) {
removeCount++
})
c.AddExpirationListener(func(key string, value interface{}) {
expiredCount++
})
for i := 0; i < 10000; i++ {
key := GenerateKey()
c.PutIfAbsent(key, 1, time.Duration(rand.Intn(100))*time.Millisecond)
c.Get(key)
c.Exists(key)
}
c.Put("name", "jack", cache.DefaultExpiration)
c.Put("val", 10, cache.NoExpiration)
elapse := time.Since(start)
c.ForEach(func(key string, value interface{}) bool {
fmt.Printf("foreach key: %s\n", key)
return true
})
fmt.Printf("elapse = %dms, count = %d, addCount = %d, removeCount = %d, expiredCount = %d\n", elapse.Milliseconds(), c.Count(), addCount, removeCount, expiredCount)
}