Example usage for org.springframework.data.redis.core RedisTemplate setValueSerializer

List of usage examples for org.springframework.data.redis.core RedisTemplate setValueSerializer

Introduction

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

Prototype

public void setValueSerializer(RedisSerializer<?> serializer) 

Source Link

Document

Sets the value serializer to be used by this template.

Usage

From source file:locksdemo.RedisUtils.java

static <K, V> RedisTemplate<K, V> createRedisTemplate(RedisConnectionFactory connectionFactory,
        Class<V> valueClass) {
    RedisTemplate<K, V> redisTemplate = new RedisTemplate<K, V>();
    redisTemplate.setKeySerializer(new StringRedisSerializer());
    redisTemplate.setValueSerializer(new GenericToStringSerializer<V>(valueClass));

    // avoids proxy
    redisTemplate.setExposeConnection(true);

    redisTemplate.setConnectionFactory(connectionFactory);
    redisTemplate.afterPropertiesSet();//  w  w  w. ja v  a 2  s. co m
    return redisTemplate;
}

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

@Test
public void removalKeyValueTest() {
    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 .  com*/

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

    String key1 = "miko-spring-mongo:counter-test";
    String key2 = "miko-spring-mongo:counter-test-2";

    Set<String> keys = redis.keys(key1);
    for (String foundKey : keys) {
        logger.debug("Found Key= " + foundKey + " value= " + ops.get(foundKey));
    }

    redis.delete(key2);

}

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

@Test
public void removalKeyValueTest() {
    RedisTemplate<String, Long> redis = new RedisTemplate<>();
    redis.setConnectionFactory(connectionFactory);
    redis.setKeySerializer(RedisStringSerializer.INSTANCE);
    redis.setValueSerializer(RedisLongSerializer.INSTANCE);
    redis.afterPropertiesSet();//from  w w w.  jav  a  2 s . c om

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

    String key1 = "miko-spring-postgresql:counter-test";
    String key2 = "miko-spring-postgresql:counter-test-2";

    Set<String> keys = redis.keys(key1);
    for (String foundKey : keys) {
        logger.debug("Found Key= " + foundKey + " value= " + ops.get(foundKey));
    }

    redis.delete(key2);

}

From source file:am.ik.categolj3.api.entry.redis.EntryRedisTemplateFactory.java

public RedisTemplate<Object, Object> create() {
    RedisTemplate<Object, Object> template = new RedisTemplate<>();
    template.setConnectionFactory(redisConnectionFactory);
    template.setKeySerializer(new JdkSerializationRedisSerializer());
    template.setValueSerializer(new RedisSerializer<Entry>() {
        @Override/*  w  ww .  ja  va2  s .  com*/
        public byte[] serialize(Entry entry) throws SerializationException {
            if (entry == null) {
                return new byte[0];
            }
            try {
                return objectMapper.writeValueAsBytes(entry);
            } catch (JsonProcessingException e) {
                throw new SerializationException("Cannot serialize " + entry, e);
            }
        }

        @Override
        public Entry deserialize(byte[] bytes) throws SerializationException {
            if (bytes == null || bytes.length == 0) {
                return null;
            }
            try {
                return objectMapper.readValue(bytes, Entry.class);
            } catch (IOException e) {
                throw new SerializationException("Cannot deserialize " + Arrays.toString(bytes), e);
            }
        }
    });
    return template;
}

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

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

    ValueOperations<String, Long> ops = redis.opsForValue();
    String key = "spring-postgresql:counter-test";

    Long value = ops.get(key);
    logger.debug("VALUE = " + value);

    assertThat(value, notNullValue());
}

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

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

    ValueOperations<String, Long> ops = redis.opsForValue();
    String key = "miko-spring-postgresql:counter-test";

    Long value = ops.get(key);
    logger.debug("VALUE = " + value);

    assertThat(value, notNullValue());
}

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();/*  www .  j  ava2  s.c  o  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();//from   w  ww .  ja  v  a 2 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:io.gravitee.repository.redis.management.ManagementRepositoryConfiguration.java

@Bean(name = "managementRedisTemplate")
public RedisTemplate redisTemplate(
        org.springframework.data.redis.connection.RedisConnectionFactory redisConnectionFactory) {
    RedisTemplate redisTemplate = new RedisTemplate();
    redisTemplate.setConnectionFactory(redisConnectionFactory);
    redisTemplate.setKeySerializer(redisTemplate.getStringSerializer());
    redisTemplate.setHashKeySerializer(redisTemplate.getStringSerializer());
    redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(Object.class));
    redisTemplate.setDefaultSerializer(new Jackson2JsonRedisSerializer<>(Object.class));
    return redisTemplate;
}

From source file:lab.home.mvn_tomcat_spring_redis_rest_api.config.SpringRedisConfig.java

@Bean
RedisTemplate<String, Object> redisTemplate() {
    final RedisTemplate<String, Object> template = new RedisTemplate<>();
    template.setConnectionFactory(jedisConnectionFactory());
    template.setKeySerializer(new StringRedisSerializer());
    template.setHashValueSerializer(new GenericToStringSerializer<>(Object.class));
    template.setValueSerializer(new GenericToStringSerializer<>(Object.class));
    return template;
}