@@ -17,11 +17,6 @@ type Cache[K comparable, V any] struct {
17
17
18
18
var _ cache.Cache [interface {}, any ] = (* Cache [interface {}, any ])(nil )
19
19
20
- type item [K comparable , V any ] struct {
21
- Key K
22
- Value V
23
- }
24
-
25
20
// NewCache creates a new LRU cache whose capacity is the default size (128).
26
21
func NewCache [K comparable , V any ]() * Cache [K , V ] {
27
22
return NewCacheWithCap [K , V ](128 )
@@ -32,38 +27,41 @@ func NewCacheWithCap[K comparable, V any](cap int) *Cache[K, V] {
32
27
return & Cache [K , V ]{
33
28
cap : cap ,
34
29
list : list .New (),
35
- items : make (map [K ]* list.Element ),
30
+ items : make (map [K ]* list.Element , cap ),
36
31
}
37
32
}
38
33
39
34
// Get looks up a key's value from the cache.
40
35
func (c * Cache [K , V ]) Get (key K ) (zero V , _ bool ) {
41
36
c .mu .RLock ()
42
37
defer c .mu .RUnlock ()
43
- if e , ok := c .items [key ]; ok {
44
- // updates cache order
45
- c .list .MoveToFront (e )
46
- return e .Value .(* item [K , V ]).Value , true
38
+ e , ok := c .items [key ]
39
+ if ! ok {
40
+ return
41
+ }
42
+ item := e .Value .(* cache.Item [K , V ])
43
+ if item .HasExpired () {
44
+ return
47
45
}
48
- return
46
+ // updates cache order
47
+ c .list .MoveToFront (e )
48
+ return item .Value , true
49
49
}
50
50
51
51
// Set sets a value to the cache with key. replacing any existing value.
52
- func (c * Cache [K , V ]) Set (key K , val V ) {
52
+ func (c * Cache [K , V ]) Set (key K , val V , opts ... cache. ItemOption ) {
53
53
c .mu .Lock ()
54
54
defer c .mu .Unlock ()
55
55
56
56
if e , ok := c .items [key ]; ok {
57
57
// updates cache order
58
58
c .list .MoveToFront (e )
59
- e .Value .(* item [K , V ]).Value = val
59
+ e .Value .(* cache. Item [K , V ]).Value = val
60
60
return
61
61
}
62
62
63
- e := c .list .PushFront (& item [K , V ]{
64
- Key : key ,
65
- Value : val ,
66
- })
63
+ item := cache .NewItem (key , val , opts ... )
64
+ e := c .list .PushFront (item )
67
65
c .items [key ] = e
68
66
69
67
if c .list .Len () > c .cap {
@@ -77,7 +75,8 @@ func (c *Cache[K, V]) Keys() []K {
77
75
defer c .mu .RUnlock ()
78
76
keys := make ([]K , 0 , len (c .items ))
79
77
for ent := c .list .Back (); ent != nil ; ent = ent .Prev () {
80
- keys = append (keys , ent .Value .(* item [K , V ]).Key )
78
+ item := ent .Value .(* cache.Item [K , V ])
79
+ keys = append (keys , item .Key )
81
80
}
82
81
return keys
83
82
}
@@ -102,8 +101,12 @@ func (c *Cache[K, V]) Delete(key K) {
102
101
func (c * Cache [K , V ]) Contains (key K ) bool {
103
102
c .mu .RLock ()
104
103
defer c .mu .RUnlock ()
105
- _ , ok := c .items [key ]
106
- return ok
104
+ e , ok := c .items [key ]
105
+ if ! ok {
106
+ return false
107
+ }
108
+ item := e .Value .(* cache.Item [K , V ])
109
+ return ! item .HasExpired ()
107
110
}
108
111
109
112
func (c * Cache [K , V ]) deleteOldest () {
@@ -113,6 +116,6 @@ func (c *Cache[K, V]) deleteOldest() {
113
116
114
117
func (c * Cache [K , V ]) delete (e * list.Element ) {
115
118
c .list .Remove (e )
116
- item := e .Value .(* item [K , V ])
119
+ item := e .Value .(* cache. Item [K , V ])
117
120
delete (c .items , item .Key )
118
121
}
0 commit comments