spring-boot-devtools从缓存中获取ClassCastException.

tit*_*geo 8 java memcached spring-boot spring-cache jhipster

我从缓存中获取价值时遇到问题.

java.lang.RuntimeException: java.lang.ClassCastException: com.mycom.admin.domain.User cannot be cast to com.mycom.admin.domain.User
Run Code Online (Sandbox Code Playgroud)

缓存配置

@Configuration
@EnableCaching
@AutoConfigureAfter(value = { MetricsConfiguration.class, DatabaseConfiguration.class })
@Profile("!" + Constants.SPRING_PROFILE_FAST)
public class MemcachedCacheConfiguration extends CachingConfigurerSupport {

    private final Logger log = LoggerFactory.getLogger(MemcachedCacheConfiguration.class);

    @Override
    @Bean
    public CacheManager cacheManager() {
        ExtendedSSMCacheManager cacheManager = new ExtendedSSMCacheManager();
        try {
            List<SSMCache> list = new ArrayList<>();
            list.add(new SSMCache(defaultCache("apiCache"), 86400, false));
            cacheManager.setCaches(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return cacheManager;
    }


    @Override
    public CacheResolver cacheResolver() {
        return null;
    }

    @Override
    public CacheErrorHandler errorHandler() {
        return null;
    }

    private Cache defaultCache(String cacheName) throws Exception {
        CacheFactory cacheFactory = new CacheFactory();
        cacheFactory.setCacheName(cacheName);
        cacheFactory.setCacheClientFactory(new MemcacheClientFactoryImpl());
        String serverHost = "127.0.0.1:11211";
        cacheFactory.setAddressProvider(new DefaultAddressProvider(serverHost));
        cacheFactory.setConfiguration(cacheConfiguration());
        return cacheFactory.getObject();
    }

    @Bean
    public CacheConfiguration cacheConfiguration() {
        CacheConfiguration cacheConfiguration = new CacheConfiguration();
        cacheConfiguration.setConsistentHashing(true);
        return cacheConfiguration;
    }

}
Run Code Online (Sandbox Code Playgroud)

并附有注释

@Cacheable(value = "apiCache#86400", key = "'User-'.concat(#login)")
Run Code Online (Sandbox Code Playgroud)

我正在使用com.google.code.simple-spring-memcached 3.5.0

值正在缓存但是在获取应用程序时会抛出类转换错误.什么是可能的问题.

完整堆栈跟踪

Ste*_*oll 9

这是Devtools的已知限制.反序列化高速缓存条目时,该对象未附加到正确的类加载器.

您可以通过多种方式解决此问题:

  1. 在开发中运行应用程序时禁用缓存
  2. 使用不同的缓存管理器(如果您使用的是Spring Boot 1.3,则可以simple使用该spring.cache.type属性强制缓存管理器application-dev.properties并在IDE中启用dev配置文件)
  3. 配置memcached(和缓存的东西)以在应用程序类加载器中运行.我不推荐这个选项,因为上面的两个更容易实现