redis防止重複提交
阿新 • • 發佈:2019-01-02
// // 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 intDEFAULT_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(); } }