鍍金池/ 問答/人工智能  Java/ @Cacheable 緩存值亂碼如何解決?

@Cacheable 緩存值亂碼如何解決?

已經(jīng)配置過RedisTemplate

@Bean
public RedisTemplate redisTemplateInit() {
    //設置序列化Key的實例化對象
    redisTemplate.setKeySerializer(new StringRedisSerializer());
    //設置序列化Value的實例化對象
    redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
    return redisTemplate;
}

現(xiàn)在通過RedisTemplate直接設置值是沒有問題的。

但是通過在方法上使用@Cacheable注解的方式緩存數(shù)據(jù),key是正常的,value會出現(xiàn)一定的亂碼,如下:
"xacxedx00x05tx00x1fStudent{name='test', age=22}"

是在redis中看是亂碼,直接用代碼取是正常的

請問有人遇到過這個問題嗎?該如何解決

回答
編輯回答
爛人

是第二次請求接口使返回的數(shù)據(jù)亂碼嗎?

還是查詢redis中的值時亂碼。

我也在springboot的一個小項目中測試了一下,不過功能正常。

jpa:
    show-sql: true
  jackson:
    default-property-inclusion: non_null
  redis:
    host: 192.168.1.111
    port: 6379

對于使用StringRedisTemplate去存儲token的AOP是正常的沒我并沒有配置什么,而是直接使用springboot的集成,

import org.springframework.data.redis.core.StringRedisTemplate;

然后我再一個請求商品的list時加了@Cacheable進行商品信息的緩存。

@GetMapping("/list")
    @Cacheable(cacheNames = "product",key = "1")
//    @Cacheable(cacheNames = "product", key = "#sellerId", condition = "#sellerId.length() > 3", unless = "#result.getCode() != 0")
    public ResultVO list(){
        
        ...
        ...
        ...
        return ResultVOUtil.success(productVOList);
    }

再頁面與postman都是正常的顯示數(shù)據(jù),且控制臺也跑出了sql(我這里執(zhí)行兩個sql操作)
請求結果
控制臺信息
當我再次刷新時,sql并沒有執(zhí)行,而是去請求redis。
我去查看了redis,也正常存儲進去了

redis可視化工具

你能具體說下你的bug問題還有復現(xiàn)下它的發(fā)生環(huán)境嗎?

2017年5月3日 10:06