List of usage examples for org.springframework.data.redis.core ValueOperations setIfAbsent
@Nullable Boolean setIfAbsent(K key, V value);
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*/ }