Example usage for org.springframework.data.redis.core ValueOperations setIfAbsent

List of usage examples for org.springframework.data.redis.core ValueOperations setIfAbsent

Introduction

In this page you can find the example usage for org.springframework.data.redis.core ValueOperations setIfAbsent.

Prototype

@Nullable
Boolean setIfAbsent(K key, V value);

Source Link

Document

Set key to hold the string value if key is absent.

Usage

From source file:com.miko.demo.mongo.service.RedisInitTest.java

@Test
public void testRedisTemplate() {
    RedisTemplate<String, Long> redis = new RedisTemplate<>();
    redis.setConnectionFactory(connectionFactory);
    redis.setKeySerializer(RedisStringSerializer.INSTANCE);
    redis.setValueSerializer(RedisLongSerializer.INSTANCE);
    redis.afterPropertiesSet();//from   w w  w .  j a v  a  2  s. co  m

    ValueOperations<String, Long> ops = redis.opsForValue();

    String key1 = "miko-spring-mongo:counter-test";
    String key2 = "miko-spring-mongo:counter-test-2";
    logger.debug("redistTemplate ops = " + ops);

    ops.setIfAbsent(key1, 1L);
    ops.setIfAbsent(key2, 22L);
    Long l = ops.increment(key1, 1);

    logger.debug("redistTemplate L = " + l);

    assertThat(l, is(greaterThan(0L)));
}

From source file:com.miko.demo.postgresql.service.RedisInitTest.java

@Test
public void testRedisTemplate() {
    RedisTemplate<String, Long> redis = new RedisTemplate<>();
    redis.setConnectionFactory(connectionFactory);
    redis.setKeySerializer(RedisStringSerializer.INSTANCE);
    redis.setValueSerializer(RedisLongSerializer.INSTANCE);
    redis.afterPropertiesSet();//w ww  .j  a va2 s .  c  o m

    ValueOperations<String, Long> ops = redis.opsForValue();

    String key1 = "miko-spring-postgresql:counter-test";
    String key2 = "miko-spring-postgresql:counter-test-2";
    logger.debug("redistTemplate ops = " + ops);

    ops.setIfAbsent(key1, 1L);
    ops.setIfAbsent(key2, 22L);
    Long l = ops.increment(key1, 1);

    logger.debug("redistTemplate L = " + l);

    assertThat(l, is(greaterThan(0L)));
}

From source file:com.company.project.service.dao.RedisDao.java

public void setIfAbsent(String key, Object value) {
    ValueOperations opsForValue = redisTemplate.opsForValue();
    boolean isAbsent = opsForValue.setIfAbsent(key, value);
    if (isAbsent) {
        long sub = opsForValue.increment(key, 11);
    }/*from   ww  w.  ja v a 2s.c om*/
}