栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot整合ehcache 实现支付超时限制的方法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot整合ehcache 实现支付超时限制的方法

下面给大家介绍springboot整合ehcache 实现支付超时限制的方法,具体内容如下所示:


 net.sf.ehcache
 ehcache-core
 2.6.11

pom文件中引入ehcache依赖   

在类路径下存放ehcache.xml文件。

application.xml中指定:

spring:
 cache:
 jcache:
  config: classpath:ehcache.xml

类标注@EnableCaching

实现核心代码


public void pinUser(Object userKey) throws Exception{
  Cache cache = manager.getCache(cacheName);
  Element element = cache.get(userKey);
  if(element == null){
    
    Element newE = new Element(userKey, new Date().getTime());
    cache.put(newE);
  } else {
    
    long inTime = (Long) element.getObjectValue();
    long timeToWait = (getTimeToLive() - (new Date().getTime() - inTime)/1000);
    //提示需要等待的时间
    throw new Exception(String.format("还需等待%s秒",String.valueOf(timeToWait)));
  }
}

public void unPinUser(Object userKey) {
  Cache cache = manager.getCache(cacheName);
  cache.remove(userKey);
}

private long getTimeToLive(){
  Cache cache = manager.getCache(cacheName);
  return cache.getCacheConfiguration().getTimeToLiveSeconds();
}

使用

在调用支付接口的地方调用PayToken.getInstance().pinUser(user.getKey())即可,若抛出异常,即说明支付间隔时间太小,同时如果还有附加数据操作,抛出异常亦可以触发回滚操作。

若是系统原因导致执行失败而仍需用户等待是不合理的,因此增加了解除用户记录的方法PayToken.getInstance().unPinUser(user.getKey())。

总结

以上所述是小编给大家介绍的springboot整合ehcache 实现支付超时限制的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对考高分网网站的支持!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/141990.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号