Java Code Examples for redis.clients.jedis.Transaction#expire()

The following examples show how to use redis.clients.jedis.Transaction#expire() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TaskLock.java    From javabase with Apache License 2.0 6 votes vote down vote up
/**
 * 方案一的坏处:
 * 假如在极端情况下,可能出现集群各个服务器同时执行到taskLockVersionOne,或者 doTask执行时间过长,
 * 在redis事物还没提交的时候,会出现同时有多台服务器执行doTask。
 * @param id
 */
private static void taskLockVersionOne(String id) {
	String key = "default_task_id";
	String value = jedis.get(key);
	// 用redis 事物是防止 在set成功之后 在执行doTask或者其他情况导致程序终止没有执行到transaction.expire()
	// 导致单台机器一直占着锁会有单点事故
	Transaction transaction = null;
	try {
		transaction = jedis.multi();
		if (value == null) {
			transaction.set(key, id);
			doTask(id);
			// 设置过期是防止单点错误
			transaction.expire(key, 30);
		} else {
			if (value.equals(id)) {
				doTask(id);
			}
		}
	} catch (Exception e) {
		log.error("e" + e);
	} finally {
		transaction.exec();
	}
}
 
Example 2
Source File: TaskLock.java    From javabase with Apache License 2.0 6 votes vote down vote up
/**
 * 方案二:
 * 会重复两次回去jedis.get(key);
 * 同时如果expire时间小于doTask时间,也会出现同时有两个任务执行doTask情况。
 * @param id
 */
private static void taskLockVersionTwo(String id) {
	String key = "default_task_id";
	String value = jedis.get(key);
	// 用redis 事物是防止 在set成功之后 在执行doTask或者其他情况导致程序终止没有执行到transaction.expire()
	// 导致单台机器一直占着锁会有单点事故
	Transaction transaction = null;
	try {
		transaction = jedis.multi();
		if (value == null) {
			transaction.set(key, id);
			transaction.expire(key, 30);
		}
	} catch (Exception e) {
		log.error("e" + e);
	} finally {
		transaction.exec();
	}
	value = jedis.get(key);
	if (value.equals(id)) {
		doTask(id);
	}
}
 
Example 3
Source File: RedisSessionIdStore.java    From datashare with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void put(final String sessionId, final String login) {
    try (Jedis jedis = redis.getResource()) {
        Transaction transaction = jedis.multi();
        transaction.set(sessionId, login);
        transaction.expire(sessionId, this.ttl);
        transaction.exec();
    }
}
 
Example 4
Source File: RedisUsers.java    From datashare with GNU Affero General Public License v3.0 5 votes vote down vote up
void createUser(HashMapUser user) {
    try (Jedis jedis = redis.getResource()) {
        Transaction transaction = jedis.multi();
        transaction.set(user.login(), user.toJson());
        transaction.expire(user.login(), this.ttl);
        transaction.exec();
    }
}