Skip to content

Commit

Permalink
Improve get-cached-response method (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
evgeniy-scherbina authored Oct 31, 2023
1 parent b637222 commit db25858
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
6 changes: 6 additions & 0 deletions service/cachemdw/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions service/cachemdw/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package cachemdw

import "errors"

var ErrRequestIsNotCacheable = errors.New("request is not cacheable")
2 changes: 1 addition & 1 deletion service/cachemdw/is_cached_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down

0 comments on commit db25858

Please sign in to comment.