From 9c54bdcb34f0a8b6a519c14c9aa32820506df799 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Lewandowski?= Date: Wed, 20 Nov 2024 10:18:19 +0100 Subject: [PATCH] refactor(ARCO-283): refactor in-memory cache method --- internal/cache/in_memory.go | 18 +++++++++--------- internal/cache/redis.go | 4 ++-- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/internal/cache/in_memory.go b/internal/cache/in_memory.go index 71b15b6fb..527d81bee 100644 --- a/internal/cache/in_memory.go +++ b/internal/cache/in_memory.go @@ -43,7 +43,7 @@ func (s *MemoryStore) Del(keys ...string) error { return nil } -// MapGet retrieves a value by key and hash. +// MapGet retrieves a value by key and hash. Return err if hash or key not found. func (s *MemoryStore) MapGet(hash string, key string) ([]byte, error) { hashValue, found := s.data.Load(hash) if !found { @@ -98,7 +98,7 @@ func (s *MemoryStore) MapDel(hash string, keys ...string) error { return nil } -// MapGetAll retrieves all key-value pairs for a specific hash. +// MapGetAll retrieves all key-value pairs for a specific hash. Return err if hash not found. func (s *MemoryStore) MapGetAll(hash string) (map[string][]byte, error) { hashValue, found := s.data.Load(hash) if !found { @@ -113,16 +113,16 @@ func (s *MemoryStore) MapGetAll(hash string) (map[string][]byte, error) { return hashMap, nil } -// MapExtractAll retrieves all key-value pairs for a specific hash and deletes the hash. +// MapExtractAll retrieves all key-value pairs for a specific hash and deletes the hash. Return err if hash not found. func (s *MemoryStore) MapExtractAll(hash string) (map[string][]byte, error) { - hashMap, err := s.MapGetAll(hash) - if err != nil { - return nil, err + hashValue, found := s.data.LoadAndDelete(hash) + if !found { + return nil, ErrCacheNotFound } - err = s.Del(hash) - if err != nil { - return nil, err + hashMap, ok := hashValue.(map[string][]byte) + if !ok { + return nil, ErrCacheFailedToGet } return hashMap, nil diff --git a/internal/cache/redis.go b/internal/cache/redis.go index 3bc851c88..fb8865b8f 100644 --- a/internal/cache/redis.go +++ b/internal/cache/redis.go @@ -59,7 +59,7 @@ func (r *RedisStore) Del(keys ...string) error { return nil } -// MapGet retrieves a value by key and hash (if given). +// MapGet retrieves a value by key and hash (if given). Return err if hash or key not found. func (r *RedisStore) MapGet(hash string, key string) ([]byte, error) { result, err := r.client.HGet(r.ctx, hash, key).Result() @@ -97,7 +97,7 @@ func (r *RedisStore) MapDel(hash string, keys ...string) error { return nil } -// MapGetAll retrieves all key-value pairs for a specific hash. +// MapGetAll retrieves all key-value pairs for a specific hash. Return err if hash not found. func (r *RedisStore) MapGetAll(hash string) (map[string][]byte, error) { values, err := r.client.HGetAll(r.ctx, hash).Result() if err != nil {