当前位置:网站首页>Talk about the pit of cache Boolean value
Talk about the pit of cache Boolean value
2022-04-22 18:37:00 【Linyb geek Road】
Preface
There is such a business scenario : department A The service needs the user department B Business data of the service , department A Service user department B The business data precondition of the service is B The Department must give A to grant authorization .B The authorization and business data of the Department belong to different services . The request process is as follows

because A The request value of authentication information is fixed , Therefore, the probability of authentication results is also a fixed value . at that time B Business service development colleagues in the Department , In order to improve efficiency . Add cache , namely B Our business services will A Cache the authentication results . At that time, 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; }
reflection
Let's see if there is any problem with the writing of the above code block ?
At first glance, there seems to be no problem , But there is actually a little problem . When making a remote call , If there is an anomaly , The Boolean will return false. In this way, the correct result may be covered up , For example, they all follow the agreement ak,sk It's worth it , Result: authentication failed .
Repair
How to fix it ? A little philosophical stuff , The world is not black or white , In fact, there may still be gray areas . Boolean values are in java In the world of , It's not just true perhaps false, When the Boolean value is a wrapper class , He also has a state of null. So it can be changed 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 no problem with this change , In fact, there are still problems , because null The value is not the correct result . But we can use null To do some extra abnormal disclosure . For example, it appears null when , There's just a problem , We can A Give friendly tips , Instead of returning authentication failure , It is also convenient to expose problems in advance , And the next time you ask to come in , The cache will be because the value is null, Trigger the remote call again
summary
Abnormal process thinking is important ...
版权声明
本文为[Linyb geek Road]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204221829557114.html
边栏推荐
- Recent learning experience
- 2022年安全、下单速度快、费用低的期货公司推荐?
- DL之YoloV3:YoloV3论文《YOLOv3: An Incremental Improvement》的翻译与解读
- .net core 添加中间件
- 人气种草产品预测,猜中TOP 3得红包!
- There may be some reasons why foreign websites such as zenodo download too slowly.
- 【网络安全】Duomicms的变量覆盖漏洞从白盒测试到实战
- [fundamentals of interface testing] Chapter 10 | explain the pre script of postman request and its working principle in detail
- Alipay open platform application - Township Health Center Application
- 文本分类问题中的一些指标
猜你喜欢
随机推荐
Fastjson 2 is coming, the performance continues to improve, and it can fight for another ten years
【接口测试基础】第十篇 | 详解Postman请求前置脚本及其工作原理
Domestic chip dp9637-k bus transceiver replaces l9637d chip and si9241
Packet capture analysis of interface protocol TCP protocol
leetcode-470. 用 Rand7() 实现 Rand10()
如何设计 API 接口,实现统一格式返回
PCB Layout Stackup setting
Applet - API
An idea plug-in that doesn't work, but can install X
【2021】腾讯秋招技术岗编程 安排超市
大话测试数据(二):概念测试数据的获取
[fundamentals of interface testing] Chapter 10 | explain the pre script of postman request and its working principle in detail
Domestic chip dp9637-k bus transceiver replaces l9637d chip and si9241
Alipay open platform application - Township Health Center Application
【浏览器】谷歌浏览器自带翻译失效
There may be some reasons why foreign websites such as zenodo download too slowly.
.net core 添加中间件
Pytoch Note58 CNN可视化
PostgreSQL 15 will soon support merge statements in the SQL standard
redis主从复制
![[drive] TX2 transplants EC20 startup module](/img/f1/5ef4a9bc5deb84523dc935719b9dea.png)





![B-tree [concept]](/img/de/e52df3061a1615291de9b29c7b5c5c.png)


