@@ -133,7 +133,7 @@ func (c *Cache[K, V]) set(key K, value V, ttl time.Duration) *Item[K, V] {
133
133
ttl = c .options .ttl
134
134
}
135
135
136
- elem := c .get (key , false )
136
+ elem := c .get (key , false , true )
137
137
if elem != nil {
138
138
// update/overwrite an existing item
139
139
item := elem .Value .(* Item [K , V ])
@@ -176,14 +176,14 @@ func (c *Cache[K, V]) set(key K, value V, ttl time.Duration) *Item[K, V] {
176
176
// It returns nil if the item is not found or is expired.
177
177
// Not safe for concurrent use by multiple goroutines without additional
178
178
// locking.
179
- func (c * Cache [K , V ]) get (key K , touch bool ) * list.Element {
179
+ func (c * Cache [K , V ]) get (key K , touch bool , includeExpired bool ) * list.Element {
180
180
elem := c .items .values [key ]
181
181
if elem == nil {
182
182
return nil
183
183
}
184
184
185
185
item := elem .Value .(* Item [K , V ])
186
- if item .isExpiredUnsafe () {
186
+ if ! includeExpired && item .isExpiredUnsafe () {
187
187
return nil
188
188
}
189
189
@@ -218,7 +218,7 @@ func (c *Cache[K, V]) getWithOpts(key K, lockAndLoad bool, opts ...Option[K, V])
218
218
c .items .mu .Lock ()
219
219
}
220
220
221
- elem := c .get (key , ! getOpts .disableTouchOnHit )
221
+ elem := c .get (key , ! getOpts .disableTouchOnHit , false )
222
222
223
223
if lockAndLoad {
224
224
c .items .mu .Unlock ()
@@ -436,7 +436,7 @@ func (c *Cache[K, V]) DeleteExpired() {
436
436
// If the item is not found, the method is no-op.
437
437
func (c * Cache [K , V ]) Touch (key K ) {
438
438
c .items .mu .Lock ()
439
- c .get (key , true )
439
+ c .get (key , true , false )
440
440
c .items .mu .Unlock ()
441
441
}
442
442
@@ -469,7 +469,7 @@ func (c *Cache[K, V]) Items() map[K]*Item[K, V] {
469
469
470
470
items := make (map [K ]* Item [K , V ], len (c .items .values ))
471
471
for k := range c .items .values {
472
- item := c .get (k , false )
472
+ item := c .get (k , false , false )
473
473
if item != nil {
474
474
items [k ] = item .Value .(* Item [K , V ])
475
475
}
0 commit comments