This repository was archived by the owner on Jul 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlrucache.go
146 lines (119 loc) · 3.14 KB
/
lrucache.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Package lrucache provides a byte-size-limited implementation of
// httpcache.Cache that stores data in memory.
package lrucache
import (
"container/list"
"sync"
"time"
)
// LruCache is a thread-safe, in-memory httpcache.Cache that evicts the
// least recently used entries from memory when either MaxSize (in bytes)
// limit would be exceeded or (if set) the entries are older than MaxAge (in
// seconds). Use the New constructor to create one.
type LruCache struct {
MaxSize int64
MaxAge int64
mu sync.Mutex
cache map[string]*list.Element
lru *list.List // Front is least-recent
size int64
}
// New creates an LruCache that will restrict itself to maxSize bytes of
// memory. If maxAge > 0, entries will also be expired after maxAge
// seconds.
func New(maxSize, maxAge int64) *LruCache {
c := &LruCache{
MaxSize: maxSize,
MaxAge: maxAge,
lru: list.New(),
cache: make(map[string]*list.Element),
}
return c
}
// Get returns the []byte representation of a cached response and a bool
// set to true if the key was found.
func (c *LruCache) Get(key string) ([]byte, bool) {
c.mu.Lock()
le, ok := c.cache[key]
if !ok {
c.mu.Unlock() // Avoiding defer overhead
return nil, false
}
if c.MaxAge > 0 && le.Value.(*entry).expires <= time.Now().Unix() {
c.deleteElement(le)
c.maybeDeleteOldest()
c.mu.Unlock() // Avoiding defer overhead
return nil, false
}
c.lru.MoveToBack(le)
value := le.Value.(*entry).value
c.mu.Unlock() // Avoiding defer overhead
return value, true
}
// Set stores the []byte representation of a response for a given key.
func (c *LruCache) Set(key string, value []byte) {
c.mu.Lock()
expires := int64(0)
if c.MaxAge > 0 {
expires = time.Now().Unix() + c.MaxAge
}
if le, ok := c.cache[key]; ok {
c.lru.MoveToBack(le)
e := le.Value.(*entry)
c.size += int64(len(value)) - int64(len(e.value))
e.value = value
e.expires = expires
} else {
e := &entry{key: key, value: value, expires: expires}
c.cache[key] = c.lru.PushBack(e)
c.size += e.size()
}
c.maybeDeleteOldest()
c.mu.Unlock()
}
// Delete removes the value associated with a key.
func (c *LruCache) Delete(key string) {
c.mu.Lock()
if le, ok := c.cache[key]; ok {
c.deleteElement(le)
}
c.mu.Unlock()
}
// Size returns the estimated current memory usage of LruCache.
func (c *LruCache) Size() int64 {
c.mu.Lock()
size := c.size
c.mu.Unlock()
return size
}
func (c *LruCache) maybeDeleteOldest() {
for c.size > c.MaxSize {
le := c.lru.Front()
if le == nil {
panic("LruCache: non-zero size but empty lru")
}
c.deleteElement(le)
}
if c.MaxAge > 0 {
now := time.Now().Unix()
for le := c.lru.Front(); le != nil && le.Value.(*entry).expires <= now; le = c.lru.Front() {
c.deleteElement(le)
}
}
}
func (c *LruCache) deleteElement(le *list.Element) {
c.lru.Remove(le)
e := le.Value.(*entry)
delete(c.cache, e.key)
c.size -= e.size()
}
// Rough estimate of map + entry object + string + byte slice overheads in bytes.
const entryOverhead = 168
type entry struct {
key string
value []byte
expires int64
}
func (e *entry) size() int64 {
return entryOverhead + int64(len(e.key)) + int64(len(e.value))
}