From 8039d9560f0bdb482103357a6b09cd7d912d4f35 Mon Sep 17 00:00:00 2001 From: evgeniy-scherbina Date: Wed, 18 Oct 2023 15:47:10 -0400 Subject: [PATCH] CR's fixes --- service/cachemdw/cache.go | 27 +++++--------------------- service/cachemdw/cache_test.go | 26 +------------------------ service/cachemdw/caching_middleware.go | 2 +- 3 files changed, 7 insertions(+), 48 deletions(-) diff --git a/service/cachemdw/cache.go b/service/cachemdw/cache.go index f2326bf..b41ade8 100644 --- a/service/cachemdw/cache.go +++ b/service/cachemdw/cache.go @@ -90,26 +90,13 @@ func (c *ServiceCache) GetCachedQueryResponse( func (c *ServiceCache) CacheQueryResponse( ctx context.Context, req *decode.EVMRPCRequestEnvelope, - chainID string, - response []byte, + responseInBytes []byte, ) error { + // don't cache uncacheable requests if !IsCacheable(ctx, c.blockGetter, c.ServiceLogger, req) { return errors.New("query isn't cacheable") } - key, err := GetQueryKey(chainID, req) - if err != nil { - return err - } - - return c.cacheClient.Set(ctx, key, response, c.cacheTTL) -} - -func (c *ServiceCache) ValidateAndCacheQueryResponse( - ctx context.Context, - req *decode.EVMRPCRequestEnvelope, - responseInBytes []byte, -) error { response, err := UnmarshalJsonRpcResponse(responseInBytes) if err != nil { return fmt.Errorf("can't unmarshal json-rpc response: %w", err) @@ -119,16 +106,12 @@ func (c *ServiceCache) ValidateAndCacheQueryResponse( return fmt.Errorf("response isn't cacheable") } - if err := c.CacheQueryResponse( - ctx, - req, - c.chainID, - responseInBytes, - ); err != nil { + key, err := GetQueryKey(c.chainID, req) + if err != nil { return err } - return nil + return c.cacheClient.Set(ctx, key, responseInBytes, c.cacheTTL) } func (c *ServiceCache) Healthcheck(ctx context.Context) error { diff --git a/service/cachemdw/cache_test.go b/service/cachemdw/cache_test.go index 7025995..626422a 100644 --- a/service/cachemdw/cache_test.go +++ b/service/cachemdw/cache_test.go @@ -90,31 +90,7 @@ func TestUnitTestCacheQueryResponse(t *testing.T) { require.Equal(t, cache.ErrNotFound, err) require.Empty(t, resp) - err = serviceCache.CacheQueryResponse(ctxb, req, defaultChainIDString, defaultQueryResp) - require.NoError(t, err) - - resp, err = serviceCache.GetCachedQueryResponse(ctxb, req) - require.NoError(t, err) - require.Equal(t, defaultQueryResp, resp) -} - -func TestUnitTestValidateAndCacheQueryResponse(t *testing.T) { - logger, err := logging.New("TRACE") - require.NoError(t, err) - - inMemoryCache := cache.NewInMemoryCache() - blockGetter := NewMockEVMBlockGetter() - cacheTTL := time.Hour - ctxb := context.Background() - - serviceCache := cachemdw.NewServiceCache(inMemoryCache, blockGetter, cacheTTL, service.DecodedRequestContextKey, defaultChainIDString, &logger) - - req := mkEVMRPCRequestEnvelope(defaultBlockNumber) - resp, err := serviceCache.GetCachedQueryResponse(ctxb, req) - require.Equal(t, cache.ErrNotFound, err) - require.Empty(t, resp) - - err = serviceCache.ValidateAndCacheQueryResponse(ctxb, req, defaultQueryResp) + err = serviceCache.CacheQueryResponse(ctxb, req, defaultQueryResp) require.NoError(t, err) resp, err = serviceCache.GetCachedQueryResponse(ctxb, req) diff --git a/service/cachemdw/caching_middleware.go b/service/cachemdw/caching_middleware.go index 25f1ae2..ec41d9f 100644 --- a/service/cachemdw/caching_middleware.go +++ b/service/cachemdw/caching_middleware.go @@ -40,7 +40,7 @@ func (c *ServiceCache) CachingMiddleware( // if request isn't already cached, request is cacheable and response is present in context - cache the response if !isCached && cacheable && ok { - if err := c.ValidateAndCacheQueryResponse( + if err := c.CacheQueryResponse( r.Context(), decodedReq, typedResponse,