该职能向下游部门发出一项要求,以检索一些数据;主要服务严重依赖Redis。 我想实施一项战略,以减少对主要服务的影响,如果下游服务再出现故障。 目前,我的想法是,如果下游服务或再服务有:
var SendDownstreamServiceTypeRequest = func(ctx context.Context, param1 int64) ([]ds.ServiceType, error) {
var rsp *Response
cachedResult, err := getCachedInfo(ctx, param1)
if err == nil {
logger.SetLoggerContext(ctx, "UsingCache", true)
rsp = &Response{Data: cachedResult}
} else {
fallbackCachedResult, fallbackErr := getFallbackCachedInfo(ctx, param1)
if fallbackErr == nil {
logger.SetLoggerContext(ctx, "UsingFallbackCache", true)
rsp = &Response{Data: fallbackCachedResult}
} else {
// ... request from downstream service
// ...
// add response data from downstream service to primary cache and fallback cache
errPrimaryCache := setCachedInfo(ctx, param1, rsp.Data)
if errPrimaryCache != nil {
logger.SetLoggerContext(ctx, "UsingCache", err.Error())
}
errFallbackCache := setFallbackCachedInfo(ctx, param1, rsp.Data)
if errFallbackCache != nil {
logger.SetLoggerContext(ctx, "UsingFallbackCache", err.Error())
}
}
}
var result ds.Response
err = json.Unmarshal(rsp.Data, &result.Data)
if err != nil {
return nil, err
}
return result.Data, nil
}
Is there any better fallback caching strategy that can be implemented into the main service in order to add more redundancies? Thanks