Spring CacheManager 缓存管理踩坑记录

203 阅读1分钟

Spring boot 版本 1.5.20.RELEASE

返回值为 String, 无法反序列化。

返回值为 String 时,缓存下来的值类似如下:

原始为:

{"value:": [{"name":"chengxin"}]}

存储为

"\"{\\\"value\\\":[{\\\"name\\\":\\\"chengxin\\\"}]}\""

反序列化报错,信息为没有 @class 字段。

解决

将被缓存的 string 嵌套在类中。用时再取出。

同一个 service 中被间接调用的方法无法应用缓存。

形如:

class ServiceA {
    func a() {
        b()
    }

    @Cacheable...
    func b() {
    }
}

调用为:
serviceA.a()

如上,b 方法不是被直接调用,间接调用时,不会触发缓存功能。

可以将 b 移动到另个 service 中:修改为:

class ServiceA {
    func a() {
        b()
    }

}

class ServiceB {
    @Cacheable...
    func b() {
    }
}

调用为:
serviceA.a()

便可。