Skip to content

Commit

Permalink
refactor(ARCO-283): refactor in-memory cache method
Browse files Browse the repository at this point in the history
  • Loading branch information
pawellewandowski98 committed Nov 20, 2024
1 parent f43cf5a commit 9c54bdc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
18 changes: 9 additions & 9 deletions internal/cache/in_memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions internal/cache/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 9c54bdc

Please sign in to comment.