当前位置: 首页>>代码示例>>Java>>正文


Java Jedis.setnx方法代码示例

本文整理汇总了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);
    }
}
 
开发者ID:TwoDragonLake,项目名称:tdl-seckill,代码行数:26,代码来源:RedisUtil.java

示例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;
}
 
开发者ID:qq1588518,项目名称:JRediClients,代码行数:31,代码来源:RedisService.java

示例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;

}
 
开发者ID:qq1588518,项目名称:JRediClients,代码行数:33,代码来源:RedisService.java

示例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;
  }
 
开发者ID:dehora,项目名称:outland,代码行数:25,代码来源:IdempotencyCheckerRedis.java

示例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;
}
 
开发者ID:yi-jun,项目名称:aaden-pay,代码行数:26,代码来源:JedisLock.java

示例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;
}
 
开发者ID:x7-framework,项目名称:x7,代码行数:22,代码来源:JedisConnector_Persistence.java

示例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);
    }
}
 
开发者ID:wxiaoqi,项目名称:ace-cache,代码行数:15,代码来源:RedisServiceImpl.java

示例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;
}
 
开发者ID:ctchengt,项目名称:tomcat8-redis-session-manager,代码行数:65,代码来源:RedisSessionManager.java

示例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;
}
 
开发者ID:appNG,项目名称:appng-tomcat-session,代码行数:66,代码来源:RedisSessionManager.java


注:本文中的redis.clients.jedis.Jedis.setnx方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。