You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// NOTE: This is an example. The cache has this functionality internally.func (o*OrderAPI) OrderStatus(ctx context.Context, idstring) (string, error) {
cacheKey:="order-status-"+idfetchFn:=func(ctx context.Context) (string, error) {
// Check redis cache first.iforderStatus, ok:=o.redisClient.Get(cacheKey); ok {
returnorderStatus, nil
}
// Fetch the order status from the underlying data source.varresponseOrderStatusResponseerr:=requests.URL(o.baseURL).
Param("id", id).
ToJSON(&response).
Fetch(ctx)
iferr!=nil {
return"", err
}
// Add the order status to the redis cache.gofunc() { o.RedisClient.Set(cacheKey, response.OrderStatus, time.Hour) }()
returnresponse.OrderStatus, nil
}
returno.GetOrFetch(ctx, id, fetchFn)
}```
Why not make it always check from local? This will be much faster. Of cource this needs firing local cache update when distributedCache(redis) changes.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi,
You check redis cache first and fill it later.
Beta Was this translation helpful? Give feedback.
All reactions