From a161171e11b480bf7a1c7fa6261b23e25166c01f Mon Sep 17 00:00:00 2001 From: Andrey Ivanov Date: Wed, 24 Jun 2020 08:29:44 +0300 Subject: [PATCH] HW4 is completed --- hw04_lru_cache/cache.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/hw04_lru_cache/cache.go b/hw04_lru_cache/cache.go index e6929af..89ec238 100644 --- a/hw04_lru_cache/cache.go +++ b/hw04_lru_cache/cache.go @@ -1,5 +1,8 @@ package hw04_lru_cache //nolint:golint,stylecheck -import "sync" +import ( + "log" + "sync" +) type Key string @@ -39,7 +42,11 @@ func (l *lruCache) Set(key Key, value interface{}) bool { } if l.queue.Len() == l.capacity { l.mx.RLock() - delete(l.items, l.queue.Back().Value.(Item).Key) + k, ok := l.queue.Back().Value.(Item) + if !ok { + log.Fatal("Ошибка приведения типов") + } + delete(l.items, k.Key) l.queue.Remove(l.queue.Back()) l.mx.RUnlock() } @@ -56,7 +63,11 @@ func (l *lruCache) Get(key Key) (interface{}, bool) { return nil, false } l.queue.MoveToFront(l.items[key]) - return l.items[key].Value.(Item).Value, true + s, ok := l.items[key].Value.(Item) + if !ok { + log.Fatal("Ошибка приведения типов") + } + return s.Value, true } func (l *lruCache) Clear() {