forked from halfrost/LeetCode-Go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCLRUCache_test.go
130 lines (119 loc) · 2.54 KB
/
CLRUCache_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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package template
import (
"container/list"
"math/rand"
"strconv"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func Test_CLRUCache(t *testing.T) {
obj := New(2)
time.Sleep(150 * time.Millisecond)
obj.Put("1", 1)
time.Sleep(150 * time.Millisecond)
obj.Put("2", 2)
time.Sleep(150 * time.Millisecond)
param1 := obj.Get("1")
time.Sleep(150 * time.Millisecond)
assert.Equal(t, 1, param1)
obj.Put("3", 3)
time.Sleep(150 * time.Millisecond)
param1 = obj.Get("2")
assert.Equal(t, nil, param1)
obj.Put("4", 4)
time.Sleep(150 * time.Millisecond)
param1 = obj.Get("1")
time.Sleep(150 * time.Millisecond)
assert.Equal(t, nil, param1)
param1 = obj.Get("3")
time.Sleep(150 * time.Millisecond)
assert.Equal(t, 3, param1)
param1 = obj.Get("4")
time.Sleep(150 * time.Millisecond)
assert.Equal(t, 4, param1)
}
func MList2Ints(lru *CLRUCache) [][]interface{} {
res := [][]interface{}{}
for head := lru.list.Front(); head != nil; head = head.Next() {
tmp := []interface{}{head.Value.(Pair).key, head.Value.(Pair).value}
res = append(res, tmp)
}
return res
}
func BenchmarkGetAndPut1(b *testing.B) {
b.ResetTimer()
obj := New(128)
wg := sync.WaitGroup{}
wg.Add(b.N * 2)
for i := 0; i < b.N; i++ {
go func() {
defer wg.Done()
obj.Get(strconv.Itoa(rand.Intn(200)))
}()
go func() {
defer wg.Done()
obj.Put(strconv.Itoa(rand.Intn(200)), strconv.Itoa(rand.Intn(200)))
}()
}
wg.Wait()
}
type Cache struct {
sync.RWMutex
Cap int
Keys map[string]*list.Element
List *list.List
}
type pair struct {
K, V string
}
func NewLRUCache(capacity int) Cache {
return Cache{
Cap: capacity,
Keys: make(map[string]*list.Element),
List: list.New(),
}
}
func (c *Cache) Get(key string) interface{} {
c.Lock()
if el, ok := c.Keys[key]; ok {
c.List.MoveToFront(el)
return el.Value.(pair).V
}
c.Unlock()
return nil
}
func (c *Cache) Put(key string, value string) {
c.Lock()
if el, ok := c.Keys[key]; ok {
el.Value = pair{K: key, V: value}
c.List.MoveToFront(el)
} else {
el := c.List.PushFront(pair{K: key, V: value})
c.Keys[key] = el
}
if c.List.Len() > c.Cap {
el := c.List.Back()
c.List.Remove(el)
delete(c.Keys, el.Value.(pair).K)
}
c.Unlock()
}
func BenchmarkGetAndPut2(b *testing.B) {
b.ResetTimer()
obj := NewLRUCache(128)
wg := sync.WaitGroup{}
wg.Add(b.N * 2)
for i := 0; i < b.N; i++ {
go func() {
defer wg.Done()
obj.Get(strconv.Itoa(rand.Intn(200)))
}()
go func() {
defer wg.Done()
obj.Put(strconv.Itoa(rand.Intn(200)), strconv.Itoa(rand.Intn(200)))
}()
}
wg.Wait()
}