lru: cache value instead of pointer

This commit is contained in:
2024-02-03 17:37:02 +03:00
parent 991e24c1a4
commit a1e778afe3

View File

@ -12,7 +12,7 @@ type LRU[T any] struct {
}
type item[T any] struct {
key string
value *T
value T
eol time.Time
}
@ -30,7 +30,7 @@ func (lru *LRU[T]) Set(key string, value T, ttl time.Duration) (evicted bool) {
if element, ok := lru.items[key]; ok {
lru.queue.MoveToFront(element)
item := element.Value.(*item[T])
item.value = &value
item.value = value
item.eol = eol
return
@ -43,7 +43,7 @@ func (lru *LRU[T]) Set(key string, value T, ttl time.Duration) (evicted bool) {
item := &item[T]{
key: key,
value: &value,
value: value,
eol: eol,
}
@ -68,7 +68,7 @@ func (lru *LRU[T]) Get(name string) (value T, ok bool) {
lru.queue.MoveToFront(element)
return *item.value, true
return item.value, true
}
func (lru *LRU[T]) evict() {