Skip to content

Commit

Permalink
CR's fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
evgeniy-scherbina committed Oct 18, 2023
1 parent 199fb9a commit 8039d95
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 48 deletions.
27 changes: 5 additions & 22 deletions service/cachemdw/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down
26 changes: 1 addition & 25 deletions service/cachemdw/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion service/cachemdw/caching_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 8039d95

Please sign in to comment.