mdoTomofumi Chiba
10/10/2025, 9:22:20 PM

Spring Cache: キャッシュの型が変わったらキャッシュを破棄する方法

@Configuration
@EnableCaching
public class CacheConfig implements CachingConfigurer {
    @Override
    public CacheErrorHandler errorHandler() {
        return new SimpleCacheErrorHandler() {
            @Override
            public void handleCacheGetError(
                    @NotNull RuntimeException exception,
                    @NotNull Cache cache,
                    @NotNull Object key) {

                if (exception instanceof SerializationException) {
                    cache.evict(key);
                    return;
                }
                throw exception;
            }
        };
    }
}
TweetBlueskyLike