Search in sources :

Example 16 with StringRedisSerializer

use of org.springframework.data.redis.serializer.StringRedisSerializer in project WTFDYUM by jchampemont.

the class RedisConfiguration method featureRedisTemplate.

@Bean
public RedisTemplate<String, Feature> featureRedisTemplate() {
    final RedisTemplate<String, Feature> template = new RedisTemplate<>();
    template.setConnectionFactory(redisConnectionFactory());
    template.setKeySerializer(new StringRedisSerializer());
    template.setHashKeySerializer(new EnumRedisSerializer<>(Feature.class));
    template.setValueSerializer(new EnumRedisSerializer<>(Feature.class));
    return template;
}
Also used : StringRedisSerializer(org.springframework.data.redis.serializer.StringRedisSerializer) RedisTemplate(org.springframework.data.redis.core.RedisTemplate) Feature(com.jeanchampemont.wtfdyum.dto.Feature) Bean(org.springframework.context.annotation.Bean)

Example 17 with StringRedisSerializer

use of org.springframework.data.redis.serializer.StringRedisSerializer in project dhis2-core by dhis2.

the class RedisConfiguration method redisTemplate.

@Bean(name = "redisTemplate")
@Primary
public RedisTemplate<?, ?> redisTemplate() {
    RedisTemplate<?, ?> redisTemplate = new RedisTemplate<>();
    redisTemplate.setConnectionFactory(lettuceConnectionFactory());
    redisTemplate.setKeySerializer(new StringRedisSerializer());
    return redisTemplate;
}
Also used : StringRedisSerializer(org.springframework.data.redis.serializer.StringRedisSerializer) StringRedisTemplate(org.springframework.data.redis.core.StringRedisTemplate) RedisTemplate(org.springframework.data.redis.core.RedisTemplate) Primary(org.springframework.context.annotation.Primary) ConfigurationPropertyFactoryBean(org.hisp.dhis.external.conf.ConfigurationPropertyFactoryBean) Bean(org.springframework.context.annotation.Bean)

Example 18 with StringRedisSerializer

use of org.springframework.data.redis.serializer.StringRedisSerializer in project cas by apereo.

the class RedisObjectFactory method newRedisTemplate.

/**
 * New redis template.
 *
 * @param <K>               the type parameter
 * @param <V>               the type parameter
 * @param connectionFactory the connection factory
 * @return the redis template
 */
public static <K, V> CasRedisTemplate<K, V> newRedisTemplate(final RedisConnectionFactory connectionFactory) {
    val template = new DefaultCasRedisTemplate<K, V>();
    val string = new StringRedisSerializer();
    val jdk = new JdkSerializationRedisSerializer();
    template.setKeySerializer(string);
    template.setValueSerializer(jdk);
    template.setHashValueSerializer(jdk);
    template.setHashKeySerializer(string);
    template.setConnectionFactory(connectionFactory);
    return template;
}
Also used : lombok.val(lombok.val) StringRedisSerializer(org.springframework.data.redis.serializer.StringRedisSerializer) JdkSerializationRedisSerializer(org.springframework.data.redis.serializer.JdkSerializationRedisSerializer)

Example 19 with StringRedisSerializer

use of org.springframework.data.redis.serializer.StringRedisSerializer in project cu-kfs by CU-CommunityApps.

the class CacheConfiguration method redisTemplate.

@Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory connectionFactory) {
    final RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
    redisTemplate.setConnectionFactory(connectionFactory);
    redisTemplate.setKeySerializer(new StringRedisSerializer());
    return redisTemplate;
}
Also used : StringRedisSerializer(org.springframework.data.redis.serializer.StringRedisSerializer) RedisTemplate(org.springframework.data.redis.core.RedisTemplate) Bean(org.springframework.context.annotation.Bean)

Example 20 with StringRedisSerializer

use of org.springframework.data.redis.serializer.StringRedisSerializer in project topcom-cloud by 545314690.

the class RedisConfig method redisTemplate.

/**
 * RedisTemplate缓存操作类,类似于jdbcTemplate的一个类;
 * <p>
 * 虽然CacheManager也能获取到Cache对象,但是操作起来没有那么灵活;
 * <p>
 * 这里在扩展下:RedisTemplate这个类不见得很好操作,我们可以在进行扩展一个我们
 * <p>
 * 自己的缓存类,比如:RedisStorage类;
 *
 * @param factory : 通过Spring进行注入,参数在application.properties进行配置;
 * @return
 */
@Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
    RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>();
    redisTemplate.setConnectionFactory(factory);
    // key序列化方式;(不然会出现乱码;),但是如果方法上有Long等非String类型的话,会报类型转换错误;
    // 所以在没有自己定义key生成策略的时候,以下这个代码建议不要这么写,可以不配置或者自己实现ObjectRedisSerializer
    // 或者JdkSerializationRedisSerializer序列化方式;
    // Long类型不可以会出现异常信息;
    RedisSerializer<String> redisSerializer = new StringRedisSerializer();
    redisTemplate.setKeySerializer(redisSerializer);
    redisTemplate.setHashKeySerializer(redisSerializer);
    // 定义value的序列化方式
    Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
    ObjectMapper om = new ObjectMapper();
    om.setVisibility(PropertyAccessor.SETTER, JsonAutoDetect.Visibility.ANY);
    om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    jackson2JsonRedisSerializer.setObjectMapper(om);
    redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
    redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
    return redisTemplate;
}
Also used : StringRedisSerializer(org.springframework.data.redis.serializer.StringRedisSerializer) RedisTemplate(org.springframework.data.redis.core.RedisTemplate) Jackson2JsonRedisSerializer(org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Bean(org.springframework.context.annotation.Bean)

Aggregations

StringRedisSerializer (org.springframework.data.redis.serializer.StringRedisSerializer)46 RedisTemplate (org.springframework.data.redis.core.RedisTemplate)30 Bean (org.springframework.context.annotation.Bean)17 StringRedisTemplate (org.springframework.data.redis.core.StringRedisTemplate)16 Test (org.junit.Test)14 JdkSerializationRedisSerializer (org.springframework.data.redis.serializer.JdkSerializationRedisSerializer)14 RedisAvailable (org.springframework.integration.redis.rules.RedisAvailable)13 DataAccessException (org.springframework.dao.DataAccessException)7 RedisConnection (org.springframework.data.redis.connection.RedisConnection)7 RedisCallback (org.springframework.data.redis.core.RedisCallback)7 BeanFactory (org.springframework.beans.factory.BeanFactory)5 Date (java.util.Date)4 QueueChannel (org.springframework.integration.channel.QueueChannel)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Jackson2JsonRedisSerializer (org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer)3 Message (org.springframework.messaging.Message)3 PollableChannel (org.springframework.messaging.PollableChannel)3 ErrorMessage (org.springframework.messaging.support.ErrorMessage)3 HashMap (java.util.HashMap)2