-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLru.go
130 lines (114 loc) · 2.43 KB
/
Lru.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 cacheutils
import (
"errors"
"time"
"github.com/RustyNailPlease/CacheUtil/internal"
)
type LRUCache[T interface{}] struct {
CacheInfo[T]
// 双向链表
LinkedList *internal.DoubleLinkedList[T]
}
func NewLRU[T interface{}](cap int64) *LRUCache[T] {
return &LRUCache[T]{
CacheInfo: CacheInfo[T]{
Capacity: int64(cap),
Size: 0,
Data: make(map[string]CacheItem[T]),
},
LinkedList: internal.NewDoubleLinkedList[T](),
}
}
func (c *LRUCache[T]) GetCapacity() int64 {
return c.Capacity
}
func (c *LRUCache[T]) GetSize() int64 {
return c.Size
}
func (c *LRUCache[T]) Set(key string, value T) error {
if c.Fulled() {
c.Prune()
if c.Fulled() {
first := c.LinkedList.GetFirst()
if first != nil {
c.Remove(first.Key)
}
}
}
// infinity
c.Data[key] = CacheItem[T]{key, value, nil}
c.LinkedList.Add(key, value)
c.Size++
// todo
return nil
}
func (c *LRUCache[T]) SetWithTimeout(key string, value T, timeout time.Time) error {
if c.Fulled() {
c.Prune()
if c.Fulled() {
first := c.LinkedList.GetFirst()
if first != nil {
c.Remove(first.Key)
}
}
}
c.Data[key] = CacheItem[T]{key, value, &timeout}
c.LinkedList.Add(key, value)
c.Size++
return nil
}
func (c *LRUCache[T]) Get(key string) (t T, err error) {
if item, ok := c.Data[key]; ok {
if item.ExpireTime != nil {
if item.ExpireTime.After(time.Now()) {
c.LinkedList.Delete(key)
c.LinkedList.Add(key, c.Data[key].Value)
return item.Value, nil
} else {
c.Remove(key)
return t, errors.New("expired")
}
} else {
c.LinkedList.Delete(key)
c.LinkedList.Add(key, c.Data[key].Value)
return item.Value, nil
}
} else {
return t, errors.New("not found")
}
}
func (c *LRUCache[T]) Prune() (int, error) {
count := 0
for _, v := range c.Data {
// expired
if v.ExpireTime != nil && v.ExpireTime.Before(time.Now()) {
c.Remove(v.Key)
count++
}
}
return count, nil
}
func (c *LRUCache[T]) Fulled() bool {
return c.GetCapacity() == c.GetSize()
}
func (c *LRUCache[T]) Remove(key string) error {
delete(c.Data, key)
c.LinkedList.Delete(key)
c.Size--
return nil
}
func (c *LRUCache[T]) Contains(key string) bool {
e, ok := c.Data[key]
expired := false
if e.ExpireTime != nil && e.ExpireTime.Before(time.Now()) {
expired = true
}
return ok && !expired
}
func (c *LRUCache[T]) Keys() []string {
keys := make([]string, 0)
for k := range c.Data {
keys = append(keys, k)
}
return keys
}