@@ -17,11 +17,6 @@ type Cache[K comparable, V any] struct {
1717
1818var _ cache.Cache [interface {}, any ] = (* Cache [interface {}, any ])(nil )
1919
20- type item [K comparable , V any ] struct {
21- Key K
22- Value V
23- }
24-
2520// NewCache creates a new LRU cache whose capacity is the default size (128).
2621func NewCache [K comparable , V any ]() * Cache [K , V ] {
2722 return NewCacheWithCap [K , V ](128 )
@@ -32,38 +27,41 @@ func NewCacheWithCap[K comparable, V any](cap int) *Cache[K, V] {
3227 return & Cache [K , V ]{
3328 cap : cap ,
3429 list : list .New (),
35- items : make (map [K ]* list.Element ),
30+ items : make (map [K ]* list.Element , cap ),
3631 }
3732}
3833
3934// Get looks up a key's value from the cache.
4035func (c * Cache [K , V ]) Get (key K ) (zero V , _ bool ) {
4136 c .mu .RLock ()
4237 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
4745 }
48- return
46+ // updates cache order
47+ c .list .MoveToFront (e )
48+ return item .Value , true
4949}
5050
5151// 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 ) {
5353 c .mu .Lock ()
5454 defer c .mu .Unlock ()
5555
5656 if e , ok := c .items [key ]; ok {
5757 // updates cache order
5858 c .list .MoveToFront (e )
59- e .Value .(* item [K , V ]).Value = val
59+ e .Value .(* cache. Item [K , V ]).Value = val
6060 return
6161 }
6262
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 )
6765 c .items [key ] = e
6866
6967 if c .list .Len () > c .cap {
@@ -77,7 +75,8 @@ func (c *Cache[K, V]) Keys() []K {
7775 defer c .mu .RUnlock ()
7876 keys := make ([]K , 0 , len (c .items ))
7977 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 )
8180 }
8281 return keys
8382}
@@ -102,8 +101,12 @@ func (c *Cache[K, V]) Delete(key K) {
102101func (c * Cache [K , V ]) Contains (key K ) bool {
103102 c .mu .RLock ()
104103 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 ()
107110}
108111
109112func (c * Cache [K , V ]) deleteOldest () {
@@ -113,6 +116,6 @@ func (c *Cache[K, V]) deleteOldest() {
113116
114117func (c * Cache [K , V ]) delete (e * list.Element ) {
115118 c .list .Remove (e )
116- item := e .Value .(* item [K , V ])
119+ item := e .Value .(* cache. Item [K , V ])
117120 delete (c .items , item .Key )
118121}
0 commit comments