From e8c87a1a18c38505e3f2222b295208bce5b66850 Mon Sep 17 00:00:00 2001 From: evgeniy-scherbina Date: Mon, 9 Oct 2023 14:37:59 -0400 Subject: [PATCH] TMP Commit --- clients/cache/cache.go | 5 ++++- clients/cache/inmemory.go | 6 +++--- clients/cache/redis.go | 12 ++++++++---- service/cachemdw/cache.go | 27 +++++++++++++-------------- 4 files changed, 28 insertions(+), 22 deletions(-) diff --git a/clients/cache/cache.go b/clients/cache/cache.go index 1d5e8cb..addf846 100644 --- a/clients/cache/cache.go +++ b/clients/cache/cache.go @@ -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 } diff --git a/clients/cache/inmemory.go b/clients/cache/inmemory.go index 437f935..f298958 100644 --- a/clients/cache/inmemory.go +++ b/clients/cache/inmemory.go @@ -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() @@ -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. diff --git a/clients/cache/redis.go b/clients/cache/redis.go index 8610663..b400a30 100644 --- a/clients/cache/redis.go +++ b/clients/cache/redis.go @@ -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. diff --git a/service/cachemdw/cache.go b/service/cachemdw/cache.go index 664ff42..9384e83 100644 --- a/service/cachemdw/cache.go +++ b/service/cachemdw/cache.go @@ -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( @@ -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(