Java Code Examples for org.springframework.data.redis.connection.RedisStringCommands.SetOption

The following examples show how to use org.springframework.data.redis.connection.RedisStringCommands.SetOption. These examples are extracted from open source projects. 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
@Override
public void electLeader()
{
    log.debug( "Election attempt by nodeId:" + this.nodeId );
    redisTemplate.getConnectionFactory().getConnection().set( key.getBytes(), nodeId.getBytes(),
        Expiration.from( timeToLiveSeconds, TimeUnit.SECONDS ), SetOption.SET_IF_ABSENT );
    if ( isLeader() )
    {
        renewLeader();
        Calendar calendar = Calendar.getInstance();
        calendar.add( Calendar.SECOND, (int) (this.timeToLiveSeconds / 2) );
        log.debug( "Next leader renewal job nodeId:" + this.nodeId + " set at " + calendar.getTime().toString() );
        JobConfiguration leaderRenewalJobConfiguration = new JobConfiguration( CLUSTER_LEADER_RENEWAL,
            JobType.LEADER_RENEWAL, null, true );
        leaderRenewalJobConfiguration.setLeaderOnlyJob( true );
        schedulingManager.scheduleJobWithStartTime( leaderRenewalJobConfiguration, calendar.getTime() );
    }
}
 
Example 2
Source Project: ShedLock   Source File: RedisLockProvider.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void unlock() {
    Expiration keepLockFor = getExpiration(lockConfiguration.getLockAtLeastUntil());
    RedisConnection redisConnection = null;
    // lock at least until is in the past
    if (keepLockFor.getExpirationTimeInMilliseconds() <= 0) {
        try {
            redisConnection = redisConnectionFactory.getConnection();
            redisConnection.del(key.getBytes());
        } catch (Exception e) {
            throw new LockException("Can not remove node", e);
        } finally {
            close(redisConnection);
        }
    } else {
        try {
            redisConnection = redisConnectionFactory.getConnection();
            redisConnection.set(key.getBytes(), buildValue(lockConfiguration.getLockAtMostUntil()), keepLockFor, SetOption.SET_IF_PRESENT);
        } finally {
            close(redisConnection);
        }
    }
}
 
Example 3
Source Project: cloud-service   Source File: RedisAuthorizationCodeServices.java    License: MIT License 5 votes vote down vote up
/**
 * 存储code到redis,并设置过期时间,10分钟<br>
 * value为OAuth2Authentication序列化后的字节<br>
 * 因为OAuth2Authentication没有无参构造函数<br>
 * redisTemplate.opsForValue().set(key, value, timeout, unit);
 * 这种方式直接存储的话,redisTemplate.opsForValue().get(key)的时候有些问题,
 * 所以这里采用最底层的方式存储,get的时候也用最底层的方式获取
 */
@Override
protected void store(String code, OAuth2Authentication authentication) {
	redisTemplate.execute(new RedisCallback<Long>() {

		@Override
		public Long doInRedis(RedisConnection connection) throws DataAccessException {
			connection.set(codeKey(code).getBytes(), SerializationUtils.serialize(authentication),
					Expiration.from(10, TimeUnit.MINUTES), SetOption.UPSERT);
			return 1L;
		}
	});
}
 
Example 4
Source Project: ShedLock   Source File: RedisLockProvider.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void doUnlock() {
    Expiration keepLockFor = getExpiration(lockConfiguration.getLockAtLeastUntil());
    // lock at least until is in the past
    if (keepLockFor.getExpirationTimeInMilliseconds() <= 0) {
        try {
            redisTemplate.delete(key);
        } catch (Exception e) {
            throw new LockException("Can not remove node", e);
        }
    } else {
        tryToSetExpiration(this.redisTemplate, key, keepLockFor, SetOption.SET_IF_PRESENT);
    }
}
 
Example 5
Source Project: ShedLock   Source File: RedisLockProvider.java    License: Apache License 2.0 5 votes vote down vote up
private static Boolean tryToSetExpiration(StringRedisTemplate template, String key, Expiration expiration, SetOption option) {
    return template.execute(connection -> {
        byte[] serializedKey = ((RedisSerializer<String>) template.getKeySerializer()).serialize(key);
        byte[] serializedValue = ((RedisSerializer<String>) template.getValueSerializer()).serialize(String.format("ADDED:%[email protected]%s", toIsoString(ClockProvider.now()), getHostname()));
        return connection.set(serializedKey, serializedValue, expiration, option);
    }, false);
}
 
Example 6
Source Project: redisson   Source File: RedissonConnectionTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testSetExpiration2() {
    assertThat(connection.set("key".getBytes(), "value".getBytes(), Expiration.milliseconds(10), SetOption.SET_IF_ABSENT)).isTrue();
    assertThat(connection.set("key".getBytes(), "value".getBytes(), Expiration.milliseconds(10), SetOption.SET_IF_ABSENT)).isFalse();
    assertThat(connection.set("key".getBytes(), "value".getBytes(), Expiration.milliseconds(10), SetOption.SET_IF_ABSENT)).isFalse();
    assertThat(connection.get("key".getBytes())).isEqualTo("value".getBytes());
}
 
Example 7
@Override
public <K, V> void put(final K key, final V value, final Serializer<K> keySerializer, final Serializer<V> valueSerializer) throws IOException {
    withConnection(redisConnection -> {
        final Tuple<byte[],byte[]> kv = serialize(key, value, keySerializer, valueSerializer);
        redisConnection.set(kv.getKey(), kv.getValue(), Expiration.seconds(ttl), SetOption.upsert());
        return null;
    });
}
 
Example 8
Source Project: redisson   Source File: RedissonConnectionTest.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void testSetExpiration() {
    assertThat(connection.set("key".getBytes(), "value".getBytes(), Expiration.milliseconds(111122), SetOption.SET_IF_ABSENT)).isTrue();
    assertThat(connection.get("key".getBytes())).isEqualTo("value".getBytes());
}
 
Example 9
Source Project: redisson   Source File: RedissonConnectionTest.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void testSetExpiration() {
    assertThat(connection.set("key".getBytes(), "value".getBytes(), Expiration.milliseconds(111122), SetOption.SET_IF_ABSENT)).isTrue();
    assertThat(connection.get("key".getBytes())).isEqualTo("value".getBytes());
}
 
Example 10
Source Project: redisson   Source File: RedissonConnectionTest.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void testSetExpiration() {
    assertThat(connection.set("key".getBytes(), "value".getBytes(), Expiration.milliseconds(111122), SetOption.SET_IF_ABSENT)).isTrue();
    assertThat(connection.get("key".getBytes())).isEqualTo("value".getBytes());
}