Java – spring boot devtools gets ClassCastException from the cache

I'm having trouble getting value from the cache

java.lang.RuntimeException: java.lang.ClassCastException: com.mycom.admin.domain.User cannot be cast to com.mycom.admin.domain.User

Cache configuration

@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;
    }

}

With notes

@Cacheable(value = "apiCache#86400",key = "'User-'.concat(#login)")

I'm using COM google. code. simple-spring-memcached 3.5. 0

The value is being cached, but a class conversion error is thrown when getting the application What are the possible problems

Full stack trace

Solution

This is a known limitation of devtools When deserializing a cache entry, the object was not attached to the correct class loader

You can solve this problem in several ways:

>Disable caching when running an application in Development > use a different cache manager (if you are using spring boot 1.3, you can use the spring.cache.type attribute in application-dev.properties to force the use of a simple cache manager and enable the dev configuration file in the IDE) > configure memcached (and cached things) to run in the application classloader I don't recommend this option because the above two are easier to implement

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>