From db258585c8d062dd3165ab3f82be49dcdcce5d2e Mon Sep 17 00:00:00 2001 From: Evgeniy Scherbina Date: Tue, 31 Oct 2023 18:32:22 -0400 Subject: [PATCH] Improve get-cached-response method (#52) --- service/cachemdw/cache.go | 6 ++++++ service/cachemdw/errors.go | 5 +++++ service/cachemdw/is_cached_middleware.go | 2 +- 3 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 service/cachemdw/errors.go diff --git a/service/cachemdw/cache.go b/service/cachemdw/cache.go index 1f74dc2..a2202fa 100644 --- a/service/cachemdw/cache.go +++ b/service/cachemdw/cache.go @@ -90,6 +90,12 @@ func (c *ServiceCache) GetCachedQueryResponse( ctx context.Context, req *decode.EVMRPCRequestEnvelope, ) ([]byte, error) { + // if request isn't cacheable - there is no point to try to get it from cache so exit early with an error + cacheable := IsCacheable(c.ServiceLogger, req) + if !cacheable { + return nil, ErrRequestIsNotCacheable + } + key, err := GetQueryKey(c.cachePrefix, req) if err != nil { return nil, err diff --git a/service/cachemdw/errors.go b/service/cachemdw/errors.go new file mode 100644 index 0000000..225944c --- /dev/null +++ b/service/cachemdw/errors.go @@ -0,0 +1,5 @@ +package cachemdw + +import "errors" + +var ErrRequestIsNotCacheable = errors.New("request is not cacheable") diff --git a/service/cachemdw/is_cached_middleware.go b/service/cachemdw/is_cached_middleware.go index 64a3b6c..483aa11 100644 --- a/service/cachemdw/is_cached_middleware.go +++ b/service/cachemdw/is_cached_middleware.go @@ -61,7 +61,7 @@ func (c *ServiceCache) IsCachedMiddleware( // 1. if not cached or we encounter an error then mark as uncached and forward to next middleware // 2. if cached then mark as cached, set cached response in context and forward to next middleware cachedQueryResponse, err := c.GetCachedQueryResponse(r.Context(), decodedReq) - if err != nil && err != cache.ErrNotFound { + if err != nil && err != cache.ErrNotFound && err != ErrRequestIsNotCacheable { // log unexpected error c.Logger.Error(). Err(err).