zoukankan      html  css  js  c++  java
  • 踩坑,springboot2.x整合redis序列化

    package com.ws.config;
    
    import com.fasterxml.jackson.annotation.JsonAutoDetect;
    import com.fasterxml.jackson.annotation.PropertyAccessor;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.ws.pojo.Employee;
    import org.springframework.cache.CacheManager;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.redis.cache.RedisCacheConfiguration;
    import org.springframework.data.redis.cache.RedisCacheManager;
    import org.springframework.data.redis.connection.RedisConnectionFactory;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.serializer.*;
    import org.springframework.util.Assert;
    
    import java.net.UnknownHostException;
    import java.time.Duration;
    
    @Configuration
    public class MyRedisConfig {
        @Bean
        public RedisTemplate<Object, Employee> myredisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
            RedisTemplate<Object, Employee> template = new RedisTemplate();
            template.setConnectionFactory(redisConnectionFactory);
            Jackson2JsonRedisSerializer<Employee> serializer = new Jackson2JsonRedisSerializer<>(Employee.class);
            template.setDefaultSerializer(serializer);
            return template;
        }
    
         @Bean
        RedisCacheManager employeeRedisCacheManager(RedisConnectionFactory redisConnectionFactory) {
    
            RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
                    .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new Jackson2JsonRedisSerializer(Employee.class)))
                    .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new Jackson2JsonRedisSerializer(Employee.class)))
                    .disableCachingNullValues();      
    RedisCacheManager redisCacheManager
    = RedisCacheManager.builder(redisConnectionFactory) .cacheDefaults(config) .transactionAware() .build(); return redisCacheManager; } }

    教学资源:https://home.cnblogs.com/u/hellokuangshen

  • 相关阅读:
    Jqeury ajax 调用C#的后台程序
    电商设计1
    智能分类
    网络爬虫框架Webmagic
    Spring Cloud 微服务架构解决方案
    网站流量日志数据分析系统1
    Shell 编程
    Zookeeper
    网站流量日志数据自定义采集
    分库分表的面试题5
  • 原文地址:https://www.cnblogs.com/ws-sharecode/p/13423241.html
Copyright © 2011-2022 走看看