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;
    }
}

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值