gpt4 book ai didi

java - JCache:指定了不兼容的缓存键类型,需要类 java.lang.Object,但类 java.lang.String

转载 作者:行者123 更新时间:2023-12-02 10:52:10 26 4
gpt4 key购买 nike

我在 Spring boot 中配置缓存时遇到问题。它工作正常,经过一些不相关的更改后,它停止工作。

我有以下缓存配置:

@Configuration
public class UserMappingCacheConfig {
public static final String USER_MAPPING_CACHE = "userMappingCache";

@Bean(name = "userMappingCache")
public Cache<String, String> getUserMappingCache(JCacheCacheManager cacheManager) {
CacheManager cm = cacheManager.getCacheManager();
Cache<String, String> cache = cm.getCache(USER_MAPPING_CACHE, String.class, String.class);
if (cache == null)
cache = cm.createCache(USER_MAPPING_CACHE, getUserMappingCacheConfiguration());
return cache;
}

private MutableConfiguration<String, String> getUserMappingCacheConfiguration() {
MutableConfiguration<String, String> configuration =
new MutableConfiguration<String, String>()
.setStoreByValue(true)
.setExpiryPolicyFactory( FactoryBuilder.factoryOf(
new CreatedExpiryPolicy( Duration.ONE_DAY)
));
return configuration;
}

我通过以下方式使用缓存:

@CacheResult(cacheName = "userMappingCache")
public String getPayrollUserName(@CacheKey String userName, String description) {...}

但是,运行服务时出现以下异常:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userMappingCache' defined in class path resource [UserMappingCacheConfig.class]: 
Bean instantiation via factory method failed;
nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.cache.Cache]: Factory method 'getUserMappingCache' threw exception;
nested exception is java.lang.ClassCastException: Incompatible cache key types specified, expected class java.lang.Object but class java.lang.String was specified
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE]
....
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.cache.Cache]: Factory method 'getUserMappingCache' threw exception;
nested exception is java.lang.ClassCastException: Incompatible cache key types specified, expected class java.lang.Object but class java.lang.String was specified
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE]
....
Caused by: java.lang.ClassCastException: Incompatible cache key types specified, expected class java.lang.Object but class java.lang.String was specified
at com.hazelcast.cache.impl.AbstractHazelcastCacheManager.getCache(AbstractHazelcastCacheManager.java:200) ~[hazelcast-3.10.jar:3.10]
at com.hazelcast.cache.impl.AbstractHazelcastCacheManager.getCache(AbstractHazelcastCacheManager.java:67) ~[hazelcast-3.10.jar:3.10]

我用谷歌搜索了一下,发现了一些条目,这些条目大多与键/值的序列化相关。即使对于原始类,这也是原因吗?我该如何修复它?提前致谢。

最佳答案

从我的评论来看,答案似乎是 MutableConfiguration 没有使用

setType(Class<K>,Class<V>)

这将配置缓存的键和值类型。如果没有它,它将默认为 Object,并且这与 @CacheKey/@ 的 (隐含) String 类型不兼容CacheResult 配对指令。

关于java - JCache:指定了不兼容的缓存键类型,需要类 java.lang.Object,但类 java.lang.String,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52086423/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com