我从缓存中获取价值时遇到问题.
java.lang.RuntimeException: java.lang.ClassCastException: com.mycom.admin.domain.User cannot be cast to com.mycom.admin.domain.User
缓存配置
@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; } }
并附有注释
@Cacheable(value = "apiCache#86400",key = "'User-'.concat(#login)")
我正在使用com.google.code.simple-spring-memcached 3.5.0
解决方法
这是
a known limitation of Devtools.当反序列化高速缓存条目时,该对象没有附加到正确的类加载器.
您可以通过多种方式解决此问题:
>在开发中运行应用程序时禁用缓存
>使用不同的缓存管理器(如果您使用的是Spring Boot 1.3,则可以使用application-dev.properties中的spring.cache.type属性强制使用简单的缓存管理器,并在IDE中启用dev配置文件)
>将memcached(和缓存的东西)配置为run in the application classloader.我不建议使用该选项,因为上面的两个更容易实现