Learn more about Caffeine configuration. Its JCache adapter uses default settings ( JCache ), which indicate that records never expire and are stored by value (copied when lost / removed from the cache). ( link here ).
When the cache is set to copy instances, you must choose the appropriate Copier that will handle this. Therefore, you can simply write:
caffeineConfiguration.setCopierFactory(JavaSerializationCopier::new);
or
caffeineConfiguration.setCopierFactory(Copier::identity);
It depends on whether you want to be safe in case of cache / value mutations.
However, the recommended option is to use the default settings , where the store-by-value parameter is disabled and a copier is required.
Config config = ConfigFactory.load(); CaffeineConfiguration<String, List<Product>> caffeineConfiguration = TypesafeConfigurator.defaults(config); caffeineConfiguration.setExpiryPolicyFactory(factoryOf(new AccessedExpiryPolicy(new Duration( TimeUnit.MINUTES, 60))));
Alternatively, you can simply disable the store-by-value setting:
caffeineConfiguration.setStoreByValue(false);
source share