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防止重复提交 - Python技术站