foreword
There is such a business scenario: the service of department A needs to use the business data of the service of department B, and the precondition for the service of department A to use the business data of the service of department B is that department B must authorize A. The authorization and business data of department B are divided into different services. The request process is as follows
Because the requested value of A's authentication information is fixed, the authentication result is likely to be a fixed value. At that time, a business service development colleague in department B, in order to improve efficiency. The cache is added, that is, the business service of B will cache the authentication result of A. It was written as follows
private final LoadingCache<String,Boolean> checkSvcCache = Caffeine
.newBuilder().maximumSize(Constants.MAX_SIZE)
.expireAfterWrite(Conastants.EXPIRE, TimeUnit.DAYS)
.build(key -> loadCache(key));
@Nullable
private Boolean loadCache(@NonNull String key) {
if(key.contains(Constant.UNDER_LINE)){
try {
String[] arr = key.split(Constant.UNDER_LINE);
Integer ak = Integer.parseInt(arr[0]);
String sk = arr[1];
RPCResult<Boolean> result = authService.checkSvc(ak, sk);
if(result.getSuccess()){
Boolean data = result.getData();
return data;
}
return false;
} catch (Exception e) {
log.error("{}",e);
}
}
return false;
}
think
Do you have any problems with the writing of the above code block?
At first glance, it seems to be no problem, but it is actually a small problem. When making a remote call, if an exception occurs, the boolean value will return false. In this way, the correct result may be covered up. For example, the value of ak and sk are passed according to the agreement, and the result returns that the authentication fails.
repair
How to fix it? Talking a little about philosophy, the world is not black and white, and there may actually be gray areas. In the world of java, boolean values are not only true or false. When the boolean value is a wrapper class, it also has a state of null. So it can be modified to
@Nullable
private Boolean loadCache(@NonNull String key) {
if(key.contains(Constant.UNDER_LINE)){
try {
String[] arr = key.split(Constant.UNDER_LINE);
Integer ak = Integer.parseInt(arr[0]);
String sk = arr[1];
RPCResult<Boolean> result = authService.checkSvc(ak, sk);
if(result.getSuccess()){
Boolean data = result.getData();
return data;
}
return false;
} catch (Exception e) {
log.error("{}",e);
}
}
return null;
}
But is there any problem with this change? In fact, there is still a problem, because the null value is not the correct result. But we can take advantage of null to do some extra exceptions. For example, when there is a null, there is a problem. We can give a friendly prompt to A instead of returning the authentication failure, which is also convenient to expose the problem in advance. When the next request comes in, the cache will trigger the remote call again because the value is null.
Summarize
Exceptional process thinking is important. . .
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。