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

The following examples show how to use org.springframework.data.redis.connection.RedisStringCommands.SetOption. 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: RedisLeaderManager.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@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 File: RedisLockProvider.java    From ShedLock with 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 File: RedisAuthorizationCodeServices.java    From cloud-service with 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 File: RedisLockProvider.java    From ShedLock with 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 File: RedisLockProvider.java    From ShedLock with 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:%s@%s", toIsoString(ClockProvider.now()), getHostname()));
        return connection.set(serializedKey, serializedValue, expiration, option);
    }, false);
}
 
Example #6
Source File: RedissonConnectionTest.java    From redisson with 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
Source File: RedisDistributedMapCacheClientService.java    From nifi with Apache License 2.0 5 votes vote down vote up
@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 File: RedissonConnectionTest.java    From redisson with 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 File: RedissonConnectionTest.java    From redisson with 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 File: RedissonConnectionTest.java    From redisson with 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());
}