redis防止重复提交
public interface DistributedLock {
boolean getLock(String var1, String var2, int var3);//加锁 void unLock(String var1, String var2);//释放
}
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
// package com.pt.platform.core.redis.lock; import com.pt.platform.core.ehcache.ObtainPropertiesInfo;
import com.pt.platform.core.redis.JedisSentinelPool;
import com.pt.platform.core.redis.lock.DistributedLock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.ShardedJedis;
import redis.clients.util.SafeEncoder; public class RedisLock implements DistributedLock {
private static final Logger logger = LoggerFactory.getLogger(RedisLock.class);
private static final int DEFAULT_EXPIRE_TIME = 60;
private static final String LOCK_KEY_PREFIX = "REDIS_LOCK";
private JedisSentinelPool pool; public RedisLock() {
} public JedisSentinelPool getPool() {
return this.pool;
} public void setPool(JedisSentinelPool pool) {
this.pool = pool;
} public boolean getLock(String module, String bizKey, int expireTime) {
if(module != null && !"".equals(module) && bizKey != null && !"".equals(bizKey)) {
if(this.getPool() != null) {
ShardedJedis jedis = null;
String lockKey = this.getLockKey(module, bizKey); boolean var10;
try {
jedis = (ShardedJedis)this.getPool().getResource();//获取到数据源
long e = System.currentTimeMillis();
long result = jedis.setnx(SafeEncoder.encode(lockKey), SafeEncoder.encode(e + "")).longValue();
if(result == 1L) {
if(expireTime > 0) {
jedis.expire(SafeEncoder.encode(lockKey), expireTime);
if(logger.isDebugEnabled()) {
logger.debug("key:" + lockKey + " locked and expire time:" + expireTime + "s");
}
} else {
jedis.expire(SafeEncoder.encode(lockKey), 60);
if(logger.isDebugEnabled()) {
logger.debug("key:" + lockKey + " locked and expire time:" + 60 + "s");
}
} var10 = true;
return var10;
} if(logger.isDebugEnabled()) {
logger.debug("key:" + lockKey + " has already bean locked");
} var10 = false;
} catch (Exception var14) {
logger.error("lock error", var14);
boolean var7 = false;
return var7;
} finally {
this.getPool().returnResource(jedis);
} return var10;
} else {
logger.error("jedisSentinelPool is null");
return true;
}
} else {
logger.error("parameters is null");
return false;
}
}
//删除key值释放锁
public void unLock(String module, String bizKey) {
if(module == null || "".equals(module) || bizKey == null || "".equals(bizKey)) {
logger.error("parameters is null");
} if(this.getPool() != null) {
ShardedJedis jedis = null;
String lockKey = this.getLockKey(module, bizKey); try {
jedis = (ShardedJedis)this.getPool().getResource();
jedis.del(SafeEncoder.encode(lockKey));
} catch (Exception var9) {
logger.error("unlock error", var9);
} finally {
this.getPool().returnResource(jedis);
}
} else {
logger.error("jedisSentinelPool is null");
} }
//组装key值
private String getLockKey(String module, String bizKey) {
StringBuffer sb = new StringBuffer();
sb.append("REDIS_LOCK").append(":").append(ObtainPropertiesInfo.getValByKey("app.code")).append(":").append(module).append(":").append(bizKey);
return sb.toString();
}
}
private Map<String ,String> getResult(LaTFundRegDTO dto,UserInfo userInfo){
Map<String,String> map=null;
//加锁
if(redisLock.getLock(FundConstant.REDIS_LOCK_MODEL_FUND, dto.getCreditorRightsNo(), 120)) {
try{
if (FundConstant.FUND_STATUS_3.equals(dto.getFundStatus())) {
service.updateReFund(dto, userInfo);
} else if (FundConstant.FUND_STATUS_1.equals(dto.getFundStatus())) {
service.updatedoFundData(dto, userInfo);
}
}catch(Exception e){
e.printStackTrace();
map=new HashMap<String,String>();
map.put("id",dto.getCreditorRightsNo());
map.put("msg",e.getMessage());
logger.error("",e);
}finally{
redisLock.unLock(FundConstant.REDIS_LOCK_MODEL_FUND,dto.getCreditorRightsNo());
}
}else{
map=new HashMap<String,String>();
map.put("id",dto.getId()+"");
map.put("msg","不允许重复发起放款操作");
logger.error("不允许重复发起放款操作");
}
return map;
}
redis防止重复提交的更多相关文章
- 使用redis防止重复提交
使用redis防止重复提交 其实主要思路是他的https://blog.csdn.net/u013378306/article/details/52944780 主要目前我的情况是,前后端分离的, ...
- Java使用Redis实现分布式锁来防止重复提交问题
如何用消息系统避免分布式事务? - 少年阿宾 - BlogJavahttp://www.blogjava.net/stevenjohn/archive/2018/01/04/433004.html [ ...
- 【Redis使用系列】使用Redis做防止重复提交
前言 在平时的开发中我们都需要处理重复提交的问题,避免业务出错或者产生脏数据,虽然可以通过前端控制但这并不是可以完全避免,最好的方式还是前后端均进行控制,这样的话就可以更有效,尽可能全面的去减少错误的 ...
- 浅谈C#在网络波动时防重复提交
前几天,公司数据库出现了两条相同的数据,而且时间相同(毫秒也相同).排查原因,发现是网络波动造成了重复提交. 由于网络波动而重复提交的例子也比较多: 网络上,防重复提交的方法也很多,使用redis锁, ...
- Restful api 防止重复提交
当前很多网站是前后分离的,前端(android,iso,h5)通过restful API 调用 后端服务器,这就存在一个问题,对于创建操作,比如购买某个商品,如果由于某种原因,手抖,控件bug,网络错 ...
- springmvc防止重复提交拦截器
一.拦截器实现,ResubmitInterceptorHandler.java import org.apache.commons.lang3.StringUtils; import org.spri ...
- 防CSRF攻击:一场由重复提交的问题引发的前端后端测试口水战
重复提交,这是一直以来都会存在的问题,当在网站某个接口调用缓慢的时候就会有可能引起表单重复提交的问题,不论form提交,还是ajax提交都会有这样的问题,最近在某社交app上看到这么一幕,这个团队没有 ...
- Token机制,防止web页面重复提交
1.业务要求:页面的数据只能被点击提交一次 2.发生原因: 由于重复点击或者网络重发,或者nginx重发等情况会导致数据被重复提交 3.解决办法: 集群环境:采用token加redis(redis单线 ...
- 由防止表单重复提交引发的一系列问题--servletRequest的复制、body值的获取
@Time:2019年1月4日 16:19:19 @Author:QGuo 背景:最开始打算写个防止表单重复提交的拦截器:网上见到一种不错的方式,比较合适前后端分离,校验在后台实现: 我在此基础上 ...
随机推荐
- github与svn的区别
github与svn都属于版本控件系统,但是两者不同于,github是分布式的,svn不是分布的是属于集中式的. 1) 最核心的区别Git是分布式的,而Svn不是分布的.能理解这点,上手会很容 ...
- Beta冲刺——汇总随笔
一.代码规范与计划随笔 Beta冲刺--代码规范与计划 二.凡事预则立随笔 Beta冲刺--凡事预则立 三.10篇冲刺随笔 Beta冲刺--第一天 Beta冲刺--第二天 Beta冲刺--第三天 Be ...
- SonarQube学习(四)- 使用Jenkins集成JaCoCo和SonarQube检查代码测试覆盖率
一.前言 我始终觉得学习这件事是自己的事,自己会了就是会了,无关于他人,但有点小伤感的是现在的阅读量开始走低. 二.准备 安装Jenkins,请移步<Docker学习(二)- Docker 安装 ...
- dede织梦技巧:教你彻底解决dede按权重排序的问题(转)
dede排序对网站来说一直存在问题,默认是按照最新发布时间排序.这样排序有个问题,一旦更新之后即被视为最新发布,于是原本做好的排序瞬间就乱了. 这种时候,按权重排序是个很好的选择,但按权重排序到处存在 ...
- Lesson_strange_words5
certain 一些 the company's 公司的 implement 实现 plane 平面 sluggishly 迟缓地,缓慢地 frustrated 懊恼的 profiler 分析器,分析 ...
- java反射-Method中的invoke方法的用法-以及函数式接口和lambda表达式
作者最近研究框架底层代码过程中感觉自己基础不太牢固,于是写了一点案例,以防日后忘记 接口类:Animals 1 public interface Animals { 2 3 public void e ...
- MySQL 集群知识点整理
随着项目架构的不断扩大,单台 MySQL 已经不能满足需要了,所以需要搭建集群将前来的请求进行分流处理.博客主要根据丁奇老师的专栏<<MySQL实战45讲>>学习的总结. 架构 ...
- 改进你的c#代码的5个技巧(四)
像每一篇文章一样,我会重复几行.我在我的Core i3 CPU.4GB主内存和Windows 7平台上测试了以下代码.如果你在不同的硬件配置或使用不同的平台,那么你的输出可能会随着我的输出屏幕而变化, ...
- LeetCode349. 两个数组的交集
题目 给定两个数组,编写一个函数来计算它们的交集. 分析 数组元素值可以很大,所以不适合直接开数组进行哈希,这里要学习另一种哈希方式:集合 集合有三种,区别见下面代码随想录的Carl大佬的表格,总结的 ...
- CTFshow-萌新赛逆向_flag白给
查看题目信息 下载后得到一个flag.exe文件,进行测试 使用PEiD查壳 发现一个upx的壳 使用命令进行解壳 upx -d 拿到一个无壳的程序 放进OD打开,查找关键词 发现信息 成功拿到序列号 ...