SpringBoot2.2.3在整合redis作为缓存中间件时遇到的问题

主启动启用缓存配置

@SpringBootApplication
@EnableCaching //主启动配置启动缓存
public class SysApplication {

    public static void main(String[] args) {
        SpringApplication.run(SysApplication.class, args);
    }

}

 

1.不能序列化

@JsonIgnoreProperties(value = { "hibernateLazyInitializer", "handler" })
public class User implements  Serializable{

2.反序列化出错

3.自定义json序列化

     我们注入一个@Configuration

package com.baoo.sys.config;

import com.baoo.sys.entity.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
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.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;


@Configuration
public class MyRedisConfig {
    /**
     * 默认序列化
     * @return
     */
    @Bean
    public RedisCacheConfiguration objRedisCacheConfiguration() {
        RedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(
                Object.class);
        RedisSerializationContext.SerializationPair<Object> serializationPair = RedisSerializationContext.SerializationPair
                .fromSerializer(jackson2JsonRedisSerializer);
        return RedisCacheConfiguration.defaultCacheConfig().serializeValuesWith(serializationPair);
    }

    /**
     * 用户序列化
     * @return
     */
    @Bean
    public RedisCacheConfiguration userRedisCacheConfiguration() {
        RedisSerializer<User> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<User>(
                User.class);
        RedisSerializationContext.SerializationPair<User> serializationPair = RedisSerializationContext.SerializationPair
                .fromSerializer(jackson2JsonRedisSerializer);
        return RedisCacheConfiguration.defaultCacheConfig().serializeValuesWith(serializationPair);
    }

    /**
     * 指定默认
     * @param redisConnectionFactory
     * @return
     */
    @Primary
    @Bean
    public RedisCacheManager objCacheManager(RedisConnectionFactory redisConnectionFactory) {
        RedisCacheManager.RedisCacheManagerBuilder builder = RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(redisConnectionFactory)
                .cacheDefaults(objRedisCacheConfiguration());
        RedisCacheManager cm = builder.build();
        return cm;
    }
    @Bean
    public RedisCacheManager userCacheManager(RedisConnectionFactory redisConnectionFactory) {
        RedisCacheManager.RedisCacheManagerBuilder builder = RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(redisConnectionFactory)
                .cacheDefaults(userRedisCacheConfiguration());
        RedisCacheManager cm = builder.build();
        return cm;
    }

}



 

Service 使用时

 

package com.baoo.sys.service;

import com.baoo.sys.dao.UserRepository;
import com.baoo.sys.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@CacheConfig(cacheNames = "user",cacheManager = "userCacheManager")
@Service
public class UserService {
    @Autowired
    UserRepository userRepository;

    @Cacheable(key = "#id")
    public User getUser(Integer id){
        System.out.println("查询"+id+"号用户");
        User user = userRepository.getOne(id);
        return user;
    }
    @CachePut(key = "#user.id")
    public User updateUser(User user){
        User saveUser = userRepository.saveAndFlush(user);

        return saveUser;
    }
}

 

 

 

 

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页