String lastExecTime = redisUtil.get(tableInterceptorPrefix + RedisKeyConstant.KEY_DEX_STAR_GATE_INFO_CONSULT_CACHE_TIME); if (StringUtils.isEmpty(lastExecTime)) { lastExecTime = DateUtil.now(); } String currentExecTimeStr = DateUtil.now(); List<DexBridgeStargateInfo> dexBridgeStargateInfos = dexBridgeStarGateInfoExtMapper.selectLastModifyTokenList(lastExecTime, null); if (CollectionUtils.isEmpty(dexBridgeStargateInfos)) { log.warn("refreshStarGateCache dexBridgeStargateInfos is empty"); return; } redisUtil.set(tableInterceptorPrefix + RedisKeyConstant.KEY_DEX_STAR_GATE_INFO_CONSULT_CACHE_TIME, currentExecTimeStr); 有bug嘛
时间: 2024-02-26 11:52:51 浏览: 11
从这段代码来看,没有明显的 bug。不过,需要注意的是,这段代码中使用了 Redis 缓存,如果 Redis 宕机或者出现其他问题,可能会导致程序出现异常。另外,如果 dexBridgeStarGateInfoExtMapper.selectLastModifyTokenList 这个方法有 bug 的话,也会导致程序出现异常。
相关问题
String lastExecTime = redisUtil.get(tableInterceptorPrefix + RedisKeyConstant.KEY_DEX_STAR_GATE_INFO_CONSULT_CACHE_TIME); if (StringUtils.isEmpty(lastExecTime)) { lastExecTime = DateUtil.now(); } String currentExecTimeStr = DateUtil.now(); List<DexBridgeStargateInfo> dexBridgeStargateInfos = dexBridgeStarGateInfoExtMapper.selectLastModifyTokenList(lastExecTime, null); if (CollectionUtils.isEmpty(dexBridgeStargateInfos)) { log.warn("refreshStarGateCache dexBridgeStargateInfos is empty"); return; } redisUtil.set(tableInterceptorPrefix + RedisKeyConstant.KEY_DEX_STAR_GATE_INFO_CONSULT_CACHE_TIME, currentExecTimeStr);
这段代码的功能是刷新缓存中的星门信息数据。具体来说,它会从 Redis 中获取最后一次执行时间 lastExecTime,如果为空,则将当前时间作为 lastExecTime;然后获取最近修改过的星门信息数据 dexBridgeStargateInfos,如果数据为空则直接返回。最后,将当前时间 currentExecTimeStr 存入 Redis,以便下次查询时使用。
如果不使用库 自己手写应该怎么写
如果不想使用第三方库,自己手写节流函数也是可行的。以下是一种实现方式:
1. 在组件中定义一个变量来保存上一次执行的时间戳和延迟时间:
```
let lastExecTime = 0
let delay = 200
```
2. 在滚动条事件中定义一个函数,用来判断是否需要执行目标函数:
```
const shouldExec = () => {
const now = Date.now()
if (now - lastExecTime > delay) {
lastExecTime = now
return true
}
return false
}
```
这个函数会计算当前时间和上一次执行的时间戳之间的差值,如果差值大于延迟时间,就返回true,表示可以执行目标函数;否则返回false,表示不可以执行目标函数。
3. 在滚动条事件中调用shouldExec函数来判断是否执行目标函数:
```
const handleScroll = (index: number,e:Event) => {
if (shouldExec()) {
const container = document.querySelector(".index_"+index) as HTMLElement
const scrollWidth = container.scrollWidth; // 滚动宽度
const scrollLeft = container.scrollLeft; // 滚动条的左侧距离容器左侧的距离
const clientWidth = container.clientWidth; // 可见宽度
if(scrollLeft >= (scrollWidth - clientWidth ) || (scrollWidth - clientWidth ) < 5){
console.log('最右边了',index)
jumpToDetailPage(index)
}
}
}
```
这里的shouldExec函数会控制目标函数的执行频率,如果shouldExec返回true,就执行目标函数;否则不执行。
这样,你就手写了一个简单的节流函数,用于控制滚动条事件的执行频率。