Spring Redis 缓存中的 ClassCastException

T. *_*. Ç 3 redis spring-data redis-cache spring-boot spring-cache

我正在使用 Spring Boot 版本 2.1.8.RELEASE 开发 Spring Boot 应用程序。我需要构建自定义 RedisCacheManager。

RedisCacheManager如下。

@EnableCaching
@Configuration
class CacheConfig {
    @Bean
    fun redisCacheManager(lettuceConnectionFactory: RedisConnectionFactory): RedisCacheManager? {
        val redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
            .entryTtl(Duration.ofHours(1))

        return RedisCacheManager.RedisCacheManagerBuilder
            .fromConnectionFactory(lettuceConnectionFactory)
            .cacheDefaults(redisCacheConfiguration)
            .build()
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的服务中,我使用 @Cacheble 缓存响应。看:

@Cacheable(cacheNames = ["cached_sample"])
    fun getAllSample(): List<SampleRecord> {
        return auditableRepository.findAll()
    }
Run Code Online (Sandbox Code Playgroud)

模型我缓存:

data class SampleRecord(
    @ApiModelProperty(readOnly = true)
    val id: Long? = null,
    @ApiModelProperty(readOnly = true)
    val active: Boolean? = null,
    @ApiModelProperty(readOnly = true)
    val createdDate: Instant? = null,
    val param: String
): Serializable
Run Code Online (Sandbox Code Playgroud)

当我第二次调用函数时,出现以下异常

引起原因:java.lang.ClassCastException:com.cryptocurrency.exchange.sample.model.SampleRecord无法转换为com.cryptocurrency.exchange.sample.model.SampleRecord

这个异常的原因是什么?

Mat*_*ttC 11

如果您在依赖树中使用 Spring 开发工具,则会出现此问题。有一个相当简单的解决方案,但没有很好的记录。您需要设置 Redis 缓存配置以在反序列化对象时引用 Context 类加载器。对于您的代码,它看起来像:

redisCacheConfiguration = RedisCacheConfiguration
.defaultCacheConfig(Thread.currentThread().getContextClassLoader())
.entryTtl(Duration.ofHours(1))
Run Code Online (Sandbox Code Playgroud)

.defaultCacheConfig(Thread.currentThread().getContextClassLoader()) 确保 Redis 在反序列化时具有对 Context 类加载器的引用。Spring 在“已知问题”部分中概述了这一点:https ://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using-boot-devtools-known-restart-limitations