Skip to content

Commit

Permalink
TMP Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
evgeniy-scherbina committed Oct 9, 2023
1 parent da96767 commit e8c87a1
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 22 deletions.
5 changes: 4 additions & 1 deletion clients/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ package cache

import (
"context"
"errors"
"time"
)

var ErrNotFound = errors.New("value not found in the cache")

type Cache interface {
Set(ctx context.Context, key string, data []byte, expiration time.Duration) error
Get(ctx context.Context, key string) ([]byte, bool)
Get(ctx context.Context, key string) ([]byte, error)
Delete(ctx context.Context, key string) error
}
6 changes: 3 additions & 3 deletions clients/cache/inmemory.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (c *InMemoryCache) Set(
}

// Get retrieves the value of a key from the cache.
func (c *InMemoryCache) Get(ctx context.Context, key string) ([]byte, bool) {
func (c *InMemoryCache) Get(ctx context.Context, key string) ([]byte, error) {
c.mutex.RLock()
defer c.mutex.RUnlock()

Expand All @@ -63,10 +63,10 @@ func (c *InMemoryCache) Get(ctx context.Context, key string) ([]byte, bool) {
// Not a real ttl but just replicates it for fetching
delete(c.data, key)

return nil, false
return nil, ErrNotFound
}

return item.data, true
return item.data, nil
}

// GetAll returns all the non-expired data in the cache.
Expand Down
12 changes: 8 additions & 4 deletions clients/cache/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,16 @@ func (rc *RedisCache) Set(
func (rc *RedisCache) Get(
ctx context.Context,
key string,
) ([]byte, bool) {
) ([]byte, error) {
val, err := rc.client.Get(ctx, key).Bytes()
if err == redis.Nil {
return nil, ErrNotFound
}
if err != nil {
return nil, err
}

// Mo error == found. This throws away any other errors that aren't "key not
// found" errors.
return val, err == nil
return val, nil
}

// Delete deletes the value for the given key in the cache.
Expand Down
27 changes: 13 additions & 14 deletions service/cachemdw/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,23 @@ func (c *ServiceCache) GetCachedQueryResponse(
ctx context.Context,
requestHost string,
req *decode.EVMRPCRequestEnvelope,
) ([]byte, bool) {
) ([]byte, error) {
chainID, err := c.GetAndCacheChainID(ctx, requestHost)
found := err == nil // TODO(yevhenii): refactor
if !found {
return nil, false
if err != nil {
return nil, err
}

key, err := GetQueryKey(chainID, req)
if err != nil {
return nil, false
return nil, err
}

value, found := c.cacheClient.Get(ctx, key)
if !found {
return nil, false
value, err := c.cacheClient.Get(ctx, key)
if err != nil {
return nil, err
}

return value, true
return value, nil
}

func (c *ServiceCache) CacheQueryResponse(
Expand Down Expand Up @@ -133,15 +132,15 @@ func (c *ServiceCache) ValidateAndCacheQueryResponse(
func (c *ServiceCache) GetCachedChainID(
ctx context.Context,
host string,
) (string, bool) {
) (string, error) {
key := GetChainKey(host)

chainID, found := c.cacheClient.Get(ctx, key)
if !found {
return "", false
chainID, err := c.cacheClient.Get(ctx, key)
if err != nil {
return "", err
}

return string(chainID), true
return string(chainID), nil
}

func (c *ServiceCache) GetAndCacheChainID(
Expand Down

0 comments on commit e8c87a1

Please sign in to comment.