File size: 2,497 Bytes
ef987bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package lru

import "time"

type entry struct {
	key       string
	value     string
	expiresAt time.Time
	ttl       time.Duration
	prev      *entry
	next      *entry
}

type Cache struct {
	capacity int
	items    map[string]*entry
	head     *entry
	tail     *entry
	now      func() time.Time
}

func NewCache(capacity int) *Cache {
	if capacity < 1 {
		capacity = 1
	}
	return NewCacheWithClock(capacity, time.Now)
}

// NewCacheWithClock exposes deterministic TTL testing without changing NewCache.
func NewCacheWithClock(capacity int, now func() time.Time) *Cache {
	if capacity < 1 {
		capacity = 1
	}
	if now == nil {
		now = time.Now
	}
	return &Cache{capacity: capacity, items: map[string]*entry{}, now: now}
}

func (c *Cache) Set(key string, value string) { c.set(key, value, 0) }

func (c *Cache) SetWithTTL(key string, value string, ttl time.Duration) { c.set(key, value, ttl) }

func (c *Cache) set(key, value string, ttl time.Duration) {
	if e, ok := c.items[key]; ok {
		e.value = value
		e.ttl = ttl
		if ttl > 0 {
			e.expiresAt = c.now().Add(ttl)
		} else {
			e.expiresAt = time.Time{}
		}
		c.moveToFront(e)
		return
	}
	e := &entry{key: key, value: value, ttl: ttl}
	if ttl > 0 {
		e.expiresAt = c.now().Add(ttl)
	}
	c.items[key] = e
	c.pushFront(e)
	if len(c.items) > c.capacity {
		c.remove(c.tail)
	}
}

func (c *Cache) Get(key string) (string, bool) {
	e, ok := c.items[key]
	if !ok || c.expired(e) {
		if ok {
			c.remove(e)
		}
		return "", false
	}
	c.moveToFront(e)
	return e.value, true
}

func (c *Cache) GetAndRenewTTL(key string) (string, bool) {
	e, ok := c.items[key]
	if !ok || c.expired(e) {
		if ok {
			c.remove(e)
		}
		return "", false
	}
	if e.ttl > 0 {
		e.expiresAt = c.now().Add(e.ttl)
	}
	c.moveToFront(e)
	return e.value, true
}

func (c *Cache) expired(e *entry) bool { return !e.expiresAt.IsZero() && !c.now().Before(e.expiresAt) }

func (c *Cache) pushFront(e *entry) {
	e.prev = nil
	e.next = c.head
	if c.head != nil {
		c.head.prev = e
	}
	c.head = e
	if c.tail == nil {
		c.tail = e
	}
}

func (c *Cache) moveToFront(e *entry) {
	if e == c.head {
		return
	}
	if e.prev != nil {
		e.prev.next = e.next
	}
	if e.next != nil {
		e.next.prev = e.prev
	}
	if e == c.tail {
		c.tail = e.prev
	}
	c.pushFront(e)
}

func (c *Cache) remove(e *entry) {
	if e == nil {
		return
	}
	if e.prev != nil {
		e.prev.next = e.next
	} else {
		c.head = e.next
	}
	if e.next != nil {
		e.next.prev = e.prev
	} else {
		c.tail = e.prev
	}
	delete(c.items, e.key)
}