@@ -56,40 +56,49 @@ func (c *QueryCache) Retrieve(query *Query, getter func() (any, error)) (*Cached
56
56
if err != nil {
57
57
if ! errors .Is (err , & store.NotFound {}) {
58
58
metrics .SturdyQueryCacheErrorCount .WithLabelValues (cacheReq .Method ).Inc ()
59
- return nil , fmt .Errorf ("failed to cache.get: %w" , err )
59
+ return nil , fmt .Errorf ("error during cache.get: %w" , err )
60
60
}
61
61
62
62
metrics .SturdyQueryCacheMissCount .WithLabelValues (cacheReq .Method ).Inc ()
63
63
64
64
if getter == nil {
65
- log .Warnf ("nil getter provided for %s" , query .Method () )
65
+ log .Warnf ("nil getter provided for %s" , cacheReq .Method )
66
66
metrics .SturdyQueryCacheErrorCount .WithLabelValues (cacheReq .Method ).Inc ()
67
- return nil , errors . New ( "cache miss with no object getter provided" )
67
+ return nil , nil
68
68
}
69
69
70
- log .Infof ("cold cache retrieval for %s" , query .Method ())
70
+ // Cold object retrieval after cache miss
71
+ log .Infof ("cold object retrieval for %s [%s]" , cacheReq .Method , cacheReq .GetCacheKey ())
71
72
obj , err , _ := c .singleflight .Do (cacheReq .GetCacheKey (), getter )
72
73
if err != nil {
73
74
metrics .SturdyQueryCacheErrorCount .WithLabelValues (cacheReq .Method ).Inc ()
74
- return nil , fmt .Errorf ("failed to call object getter: %w" , err )
75
+ return nil , fmt .Errorf ("error calling getter: %w" , err )
75
76
}
77
+
76
78
res , ok := obj .(* jsonrpc.RPCResponse )
77
79
if ! ok {
78
80
return nil , errors .New ("unknown type returned by getter" )
79
81
}
82
+
80
83
cacheResp := & CachedResponse {Result : res .Result , Error : res .Error }
81
- err = c .cache .Set (
82
- ctx , cacheReq , cacheResp ,
83
- store .WithExpiration (cacheReq .Expiration ()),
84
- store .WithTags (cacheReq .Tags ()),
85
- )
86
- if err != nil {
87
- metrics .SturdyQueryCacheErrorCount .WithLabelValues (cacheReq .Method ).Inc ()
88
- monitor .ErrorToSentry (fmt .Errorf ("failed to cache.set: %w" , err ), map [string ]string {"method" : cacheReq .Method })
89
- return nil , fmt .Errorf ("failed to cache.set: %w" , err )
84
+ if res .Error != nil {
85
+ log .Debugf ("rpc error received (%s), not caching" , cacheReq .Method )
86
+ } else {
87
+ err = c .cache .Set (
88
+ ctx , cacheReq , cacheResp ,
89
+ store .WithExpiration (cacheReq .Expiration ()),
90
+ store .WithTags (cacheReq .Tags ()),
91
+ )
92
+ if err != nil {
93
+ metrics .SturdyQueryCacheErrorCount .WithLabelValues (cacheReq .Method ).Inc ()
94
+ monitor .ErrorToSentry (fmt .Errorf ("error during cache.set: %w" , err ), map [string ]string {"method" : cacheReq .Method })
95
+ return cacheResp , fmt .Errorf ("error during cache.set: %w" , err )
96
+ }
90
97
}
98
+
91
99
return cacheResp , nil
92
100
}
101
+ log .Debugf ("cache hit for %s [%s]" , cacheReq .Method , cacheReq .GetCacheKey ())
93
102
cacheResp , ok := hit .(* CachedResponse )
94
103
if ! ok {
95
104
metrics .SturdyQueryCacheErrorCount .WithLabelValues (cacheReq .Method ).Inc ()
@@ -157,12 +166,10 @@ func preflightCacheHook(caller *Caller, ctx context.Context) (*jsonrpc.RPCRespon
157
166
}
158
167
query := QueryFromContext (ctx )
159
168
cachedResp , err := caller .Cache .Retrieve (query , func () (any , error ) {
160
- log .Debugf ("cache miss, calling %s" , query .Method ())
161
169
return caller .SendQuery (ctx , query )
162
170
})
163
171
if err != nil {
164
172
return nil , rpcerrors .NewSDKError (err )
165
173
}
166
- log .Debugf ("cache hit for %s" , query .Method ())
167
174
return cachedResp .RPCResponse (query .Request .ID ), nil
168
175
}
0 commit comments