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);
时间: 2024-02-26 13:52:51 浏览: 22
这段代码的功能是刷新缓存中的星门信息数据。具体来说,它会从 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,就执行目标函数;否则不执行。
这样,你就手写了一个简单的节流函数,用于控制滚动条事件的执行频率。
换一种利用new Date()的节流写法
当使用 `new Date()` 进行节流控制时,可以记录上次执行的时间戳,然后在下一次执行时与当前时间戳进行比较,如果时间间隔小于指定的延迟时间,则不执行函数,否则执行函数并更新上次执行的时间戳。以下是一个使用 `new Date()` 的节流函数的实现:
```javascript
function throttle(func, delay) {
let lastExecTime = 0;
return function (...args) {
const now = Date.now();
if (now - lastExecTime >= delay) {
func.apply(this, args);
lastExecTime = now;
}
};
}
```
该函数维护一个 `lastExecTime` 变量来记录上次执行的时间戳,初始值为 0。返回一个新函数,新函数在被调用时会获取当前时间戳,并将当前时间戳与上次执行时间戳进行比较。如果时间间隔大于等于指定的延迟时间,则执行函数,并更新 `lastExecTime` 为当前时间戳。否则不执行函数。这样可以确保 `func` 函数不会被频繁调用,从而减少性能开销。
相关推荐
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)