From a1e778afe3cc7bbcaa809a23c5c7264cd5a6fff2 Mon Sep 17 00:00:00 2001 From: Konstantin Grachev Date: Sat, 3 Feb 2024 17:37:02 +0300 Subject: [PATCH] lru: cache value instead of pointer --- lru/lru.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lru/lru.go b/lru/lru.go index 2218a4a..4cc9961 100644 --- a/lru/lru.go +++ b/lru/lru.go @@ -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() {