@Override public Long execute(Jedis connection) { return connection.setnx(key, value); } }.run(key);
@Override public Long execute(Jedis connection) { return connection.setnx(key, value); } }.runBinary(key);
public Long execute(Jedis connection) { return connection.setnx(keyByte, value); } }.runBinary(keyByte);
@Override public Long setnx(byte[] key, byte[] value) { Jedis j = getShard(key); return j.setnx(key, value); }
@Override public Long setnx(String key, String value) { Jedis j = getShard(key); return j.setnx(key, value); }
public boolean acquire(Jedis jedis) { String value = jedis.get(lockKey); if (value == null) { boolean success = jedis.setnx(lockKey, lockValue) == 1; if (success) { jedis.expire(lockKey, expiredSeconds); return true; } } else if (lockValue.equals(value)) { jedis.expire(lockKey, expiredSeconds); return true; } return false; }
public boolean acquire(Jedis jedis) { String value = jedis.get(lockKey); if (value == null) { boolean success = jedis.setnx(lockKey, lockValue) == 1; if (success) { jedis.expire(lockKey, expiredSeconds); return true; } } else if (lockValue.equals(value)) { jedis.expire(lockKey, expiredSeconds); return true; } return false; }
/** * 设置失效时间 * @param key * @param value * @return */ public Long setnx(String key ,String value){ Jedis jedis =null; Long result = null; try { jedis = jedisPool.getResource(); result = jedis.setnx(key,value); }catch (Exception e){ log.error("expire key:{} error",key,e); jedisPool.returnResource(jedis); return result; } jedisPool.returnResource(jedis); return result; } /**
@Override public Long setnx(String key, String value) { Jedis jedis = null; try { jedis = jedisPool.getResource(); return jedis.setnx(key, value); } finally { if (jedis != null) jedis.close(); } }
public String getLock(String key , int timeOut){ try { Jedis jedis = RedisManager.getJedis(); String value = UUID.randomUUID().toString(); long end =System.currentTimeMillis()+timeOut; while (System.currentTimeMillis()<end){ if(jedis.setnx(key,value) ==1){ jedis.expire(key, timeOut); //锁设置成功 redis操作成功 return value; } if(jedis.ttl(key)== -1){ jedis.expire(key, timeOut); } Thread.sleep(1000); } } catch (Exception e) { e.printStackTrace(); } return null; }
@Override public Boolean setNX(byte[] key, byte[] value) { Assert.notNull(key, "Key must not be null!"); Assert.notNull(value, "Value must not be null!"); try { if (isPipelined()) { pipeline(connection.newJedisResult(connection.getRequiredPipeline().setnx(key, value), JedisConverters.longToBoolean())); return null; } if (isQueueing()) { transaction(connection.newJedisResult(connection.getRequiredTransaction().setnx(key, value), JedisConverters.longToBoolean())); return null; } return JedisConverters.toBoolean(connection.getJedis().setnx(key, value)); } catch (Exception ex) { throw convertJedisAccessException(ex); } }
@Override public Boolean doWorkCallback(Jedis jedis) { Long result = jedis.setnx(key,value); return result == 1L && jedis.expire(key, seconds) > 0; } });
public Long setnx(String key,String value) { Jedis jedis = fetchResource(false); try { return jedis.setnx(key,value); } finally { if (jedis != null) { jedis.close(); } } }
/** * 设置 * @param key * @param value * @return */ public boolean setNxStringByMillisec(String key, String value, int millisec){ Jedis jedis = null; boolean success = true; boolean result = false; try { jedis = jedisPool.getResource(); result = (jedis.setnx(key, value) != 0); if(millisec > -1){ jedis.pexpire(key, millisec); } } catch (Exception e) { success = false; releasBrokenReidsSource(jedis, key, "setNxStringByMillisec", e, false); } finally { releaseReidsSource(success, jedis); } return result; }
/** * 设置 * @param key * @param value * @return */ public boolean setNxString(String key, String value, int seconds) throws Exception{ Jedis jedis = null; boolean success = true; boolean result; try { jedis = jedisPool.getResource(); result = (jedis.setnx(key, value) != 0); if(seconds > -1){ jedis.expire(key, seconds); } } catch (Exception e) { success = false; releasBrokenReidsSource(jedis, key, "setNxString", e, false); throw e; } finally { releaseReidsSource(success, jedis); } return result; }
@Override public Object execute(Jedis jedis) { return jedis.setnx(key, value); } });
Message msg = ... // eg. ConsumerIterator.next().message().fromJson(); Jedis jedis = ... // eg. JedisPool.getResource(); String key = "SPOUT:" + msg.uniqId; // prefix name at will String val = Long.toString(System.currentTimeMillis()); long rsps = jedis.setnx(key, val); if (rsps <= 0) { log.warn("kafka dup: {}", msg.toJson()); // and other logic } else { jedis.expire(key, 7200); // 2 hours is ok for production environment; }
@Override public Long setnx(String key, String value) { String command = "setnx"; return instrumented(command, payloadSize(value), () -> delegated.setnx(key, value)); }
public Long execute(final Jedis conn) throws Exception { byte[] ds = getRedisBuffer().write(value); return conn.setnx(SafeEncoder.encode(key), ds); } });
@Override public Long setnx(Object key, Object value) { Jedis jedis = getJedis(); try { return jedis.setnx(keyToBytes(key), valueToBytes(value)); } finally { returnResource(jedis); } }