本文整理了Java中redis.clients.jedis.Jedis.setnx()
方法的一些代码示例,展示了Jedis.setnx()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Jedis.setnx()
方法的具体详情如下:
包路径:redis.clients.jedis.Jedis
类名称:Jedis
方法名:setnx
[英]SETNX works exactly like #set(String,String) with the only difference that if the key already exists no operation is performed. SETNX actually means "SET if Not eXists".
Time complexity: O(1)
[中]SETNX的工作原理与#set(String,String)完全相同,唯一的区别是,如果密钥已经存在,则不会执行任何操作。SETNX实际上意味着“如果不存在则设置”。
时间复杂度:O(1)
代码示例来源:origin: sohutv/cachecloud
@Override
public Long execute(Jedis connection) {
return connection.setnx(key, value);
}
}.run(key);
代码示例来源:origin: sohutv/cachecloud
@Override
public Long execute(Jedis connection) {
return connection.setnx(key, value);
}
}.runBinary(key);
代码示例来源:origin: sohutv/cachecloud
public Long execute(Jedis connection) {
return connection.setnx(keyByte, value);
}
}.runBinary(keyByte);
代码示例来源:origin: sohutv/cachecloud
@Override
public Long setnx(byte[] key, byte[] value) {
Jedis j = getShard(key);
return j.setnx(key, value);
}
代码示例来源:origin: sohutv/cachecloud
@Override
public Long setnx(String key, String value) {
Jedis j = getShard(key);
return j.setnx(key, value);
}
代码示例来源:origin: ltsopensource/light-task-scheduler
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;
}
代码示例来源:origin: ltsopensource/light-task-scheduler
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;
}
代码示例来源:origin: qiurunze123/miaosha
/**
* 设置失效时间
* @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;
}
/**
代码示例来源:origin: Netflix/conductor
@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();
}
}
代码示例来源:origin: qiurunze123/miaosha
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;
}
代码示例来源:origin: spring-projects/spring-data-redis
@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);
}
}
代码示例来源:origin: mrdear/JavaWEB
@Override
public Boolean doWorkCallback(Jedis jedis) {
Long result = jedis.setnx(key,value);
return result == 1L && jedis.expire(key, seconds) > 0;
}
});
代码示例来源:origin: javahongxi/whatsmars
public Long setnx(String key,String value) {
Jedis jedis = fetchResource(false);
try {
return jedis.setnx(key,value);
} finally {
if (jedis != null) {
jedis.close();
}
}
}
代码示例来源:origin: jwpttcg66/NettyGameServer
/**
* 设置
* @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;
}
代码示例来源:origin: jwpttcg66/NettyGameServer
/**
* 设置
* @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;
}
代码示例来源:origin: io.leopard/leopard-redis
@Override
public Object execute(Jedis jedis) {
return jedis.setnx(key, value);
}
});
代码示例来源:origin: stackoverflow.com
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;
}
代码示例来源:origin: com.netflix.spinnaker.kork/kork-jedis
@Override
public Long setnx(String key, String value) {
String command = "setnx";
return instrumented(command, payloadSize(value), () -> delegated.setnx(key, value));
}
代码示例来源:origin: com.github.yamingd.argo/argo-redis
public Long execute(final Jedis conn) throws Exception {
byte[] ds = getRedisBuffer().write(value);
return conn.setnx(SafeEncoder.encode(key), ds);
}
});
代码示例来源:origin: yangfuhai/jboot
@Override
public Long setnx(Object key, Object value) {
Jedis jedis = getJedis();
try {
return jedis.setnx(keyToBytes(key), valueToBytes(value));
} finally {
returnResource(jedis);
}
}
内容来源于网络,如有侵权,请联系作者删除!