本文整理匯總了Java中redis.clients.jedis.Jedis.setnx方法的典型用法代碼示例。如果您正苦於以下問題:Java Jedis.setnx方法的具體用法?Java Jedis.setnx怎麽用?Java Jedis.setnx使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類redis.clients.jedis.Jedis
的用法示例。
在下文中一共展示了Jedis.setnx方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: setnx
import redis.clients.jedis.Jedis; //導入方法依賴的package包/類
/**
* 指定的 key 不存在時,為 key 設置指定的值。
*
* @param key key
* @return true:存在,false:不存在
*/
public boolean setnx(String key, Object object, int seconds) {
Jedis jedis = null;
try {
jedis = getConnect();
Long res = jedis.setnx(key, object.toString());
if (new Long(1L).equals(res)) {
// 設定過期時間,最多30秒自動過期,防止長期死鎖發生
jedis.expire(key.getBytes(), seconds);
return true;
}
return false;
} catch (Exception e) {
logger.error("redis getRedisData data failed!", e);
return false;
} finally {
close(jedis);
}
}
示例2: setNxString
import redis.clients.jedis.Jedis; //導入方法依賴的package包/類
/**
* 設置
*
* @param key
* @param value
* @return
*/
public boolean setNxString(String key, String value, int seconds) throws Exception {
Jedis jedis = null;
boolean success = true;
boolean ret = false;
try {
jedis = jedisPool.getResource();
if (jedis == null) {
success = false;
return false;
}
ret = (jedis.setnx(key, value) > 0);
if (seconds >= 0) {
jedis.expire(key, seconds);
}
} catch (Exception e) {
success = false;
releaseBrokenReidsSource(jedis, key, "setNxString", e, false);
// throw e;
} finally {
releaseReidsSource(success, jedis);
}
return ret;
}
示例3: setNxStringByMillisec
import redis.clients.jedis.Jedis; //導入方法依賴的package包/類
/**
* 設置
*
* @param key
* @param value
* @return
*/
@SuppressWarnings("deprecation")
public boolean setNxStringByMillisec(String key, String value, int millisec) {
Jedis jedis = null;
boolean success = true;
boolean ret = false;
try {
jedis = jedisPool.getResource();
if (jedis == null) {
success = false;
return false;
}
ret = (jedis.setnx(key, value) > 0);
if (millisec > -1) {
jedis.pexpire(key, millisec);
}
} catch (Exception e) {
success = false;
releaseBrokenReidsSource(jedis, key, "setNxStringByMillisec", e, false);
} finally {
releaseReidsSource(success, jedis);
}
return ret;
}
示例4: seen
import redis.clients.jedis.Jedis; //導入方法依賴的package包/類
public boolean seen(String idempotencyKey) {
if (idempotencyKey == null) {
return false;
}
Jedis jedis = null;
try {
jedis = redisProvider.get();
String cacheKey = "outland:feature:idem_key:" + idempotencyKey;
final Long res = jedis.setnx(cacheKey, "");
jedis.expire(cacheKey, TTL_SECONDS);
return res == 0;
} catch (Exception e) {
logger.error("{}", kvp("op", "idempotency_check",
"key", idempotencyKey,
"err", "[" + e.getMessage() + "]"));
} finally {
redisProvider.closeSafely(jedis);
}
return false;
}
示例5: acquire
import redis.clients.jedis.Jedis; //導入方法依賴的package包/類
/**
* Acquire lock.
*
* @param jedis
* @return true if lock is acquired, false acquire timeouted
* @throws InterruptedException
* in case of thread interruption
*/
public synchronized boolean acquire(Jedis jedis) throws InterruptedException {
int timeout = timeoutMsecs * 1000;
while (timeout >= 0) {
if (jedis.setnx(lockKey, lockKey) == 1) {
// lock acquired
locked = Boolean.TRUE;
jedis.setex(lockKey, expireMsecs, lockKey);
return locked;
}
timeout -= 100;
try {
Thread.sleep(100);
} catch (Exception e) {
}
}
return false;
}
示例6: lock
import redis.clients.jedis.Jedis; //導入方法依賴的package包/類
public boolean lock(String key){
boolean isLock = false;
String value = "LOCK";
Jedis jedis = null;
try {
jedis = get();
if (jedis == null)
return isLock;
isLock = (jedis.setnx(key, value) == 1);
if (isLock) {
jedis.expire(key, 3);
}
pool.returnResource(jedis);
} catch (Exception e) {
pool.returnBrokenResource(jedis);
}
return isLock;
}
示例7: setnx
import redis.clients.jedis.Jedis; //導入方法依賴的package包/類
@Override
public Long setnx(String key, String value) {
Jedis jedis = null;
try {
jedis = pool.getResource();
return jedis.setnx(key, value);
} catch (Exception e) {
LOGGER.error(e.getMessage());
return 0L;
} finally {
returnResource(pool, jedis);
}
}
示例8: createSession
import redis.clients.jedis.Jedis; //導入方法依賴的package包/類
@Override
public Session createSession(String requestedSessionId) {
RedisSession session = null;
String sessionId = null;
String jvmRoute = getJvmRoute();
Boolean error = true;
Jedis jedis = null;
try {
jedis = acquireConnection();
// Ensure generation of a unique session identifier.
if (null != requestedSessionId) {
sessionId = sessionIdWithJvmRoute(requestedSessionId, jvmRoute);
if (jedis.setnx(sessionId.getBytes(), NULL_SESSION) == 0L) {
sessionId = null;
}
} else {
do {
sessionId = sessionIdWithJvmRoute(generateSessionId(), jvmRoute);
} while (jedis.setnx(sessionId.getBytes(), NULL_SESSION) == 0L); // 1 = key set; 0 = key already existed
}
/* Even though the key is set in Redis, we are not going to flag
the current thread as having had the session persisted since
the session isn't actually serialized to Redis yet.
This ensures that the save(session) at the end of the request
will serialize the session into Redis with 'set' instead of 'setnx'. */
error = false;
if (null != sessionId) {
session = (RedisSession)createEmptySession();
session.setNew(true);
session.setValid(true);
session.setCreationTime(System.currentTimeMillis());
session.setMaxInactiveInterval(getMaxInactiveInterval());
session.setId(sessionId);
session.tellNew();
}
currentSession.set(session);
currentSessionId.set(sessionId);
currentSessionIsPersisted.set(false);
currentSessionSerializationMetadata.set(new SessionSerializationMetadata());
if (null != session) {
try {
error = saveInternal(jedis, session, true);
} catch (IOException ex) {
log.error("Error saving newly created session: " + ex.getMessage());
currentSession.set(null);
currentSessionId.set(null);
session = null;
}
}
} finally {
if (jedis != null) {
returnConnection(jedis, error);
}
}
return session;
}
示例9: createSession
import redis.clients.jedis.Jedis; //導入方法依賴的package包/類
@Override
public RedisSession createSession(String requestedSessionId) {
RedisSession session = null;
String sessionId = null;
String jvmRoute = getJvmRoute();
Boolean error = true;
Jedis jedis = null;
try {
jedis = acquireConnection();
// Ensure generation of a unique session identifier.
if (null != requestedSessionId) {
sessionId = sessionIdWithJvmRoute(requestedSessionId, jvmRoute);
if (jedis.setnx(sessionId.getBytes(), NULL_SESSION) == 0L) {
sessionId = null;
}
} else {
do {
sessionId = sessionIdWithJvmRoute(generateSessionId(), jvmRoute);
} while (jedis.setnx(sessionId.getBytes(), NULL_SESSION) == 0L); // 1 = key set; 0 = key already existed
}
/*
* Even though the key is set in Redis, we are not going to flag the current thread as having had the
* session persisted since the session isn't actually serialized to Redis yet. This ensures that the
* save(session) at the end of the request will serialize the session into Redis with 'set' instead of
* 'setnx'.
*/
error = false;
if (null != sessionId) {
session = (RedisSession) createEmptySession();
session.setNew(true);
session.setValid(true);
session.setCreationTime(System.currentTimeMillis());
session.setMaxInactiveInterval(getSessionTimeout());
session.setId(sessionId);
session.tellNew();
}
currentSession.set(session);
currentSessionId.set(sessionId);
currentSessionIsPersisted.set(false);
currentSessionSerializationMetadata.set(new SessionSerializationMetadata());
if (null != session) {
try {
error = saveInternal(jedis, session, true);
} catch (IOException ex) {
log.error("Error saving newly created session: " + ex.getMessage());
currentSession.set(null);
currentSessionId.set(null);
session = null;
}
}
} finally {
if (jedis != null) {
returnConnection(jedis, error);
}
}
return session;
}