{"record":{"id":"0071ba3fa311ab8f","repo":"quarkusio/quarkus","slug":"an-existing-cached-value-type-does-not-match-the-t","errorCode":null,"errorMessage":"An existing cached value type does not match the type returned by the value loading function","messagePattern":"An existing cached value type does not match the type returned by the value loading function","errorType":"exception","errorClass":"CacheException","httpStatus":null,"severity":"error","filePath":"extensions/cache/runtime/src/main/java/io/quarkus/cache/runtime/caffeine/CaffeineCacheImpl.java","lineNumber":344,"sourceCode":"                    \"maximum-size configuration value\");\n        }\n    }\n\n    // For testing purposes only.\n    public CaffeineCacheInfo getCacheInfo() {\n        return cacheInfo;\n    }\n\n    public long getSize() {\n        return cache.synchronous().estimatedSize();\n    }\n\n    @SuppressWarnings(\"unchecked\")\n    private <T> T cast(Object value) {\n        try {\n            return (T) value;\n        } catch (ClassCastException e) {\n            throw new CacheException(\n                    \"An existing cached value type does not match the type returned by the value loading function\", e);\n        }\n    }\n\n    @SuppressWarnings(\"unchecked\")\n    private <V> Function<V, V> fromCacheValue() {\n        return (Function<V, V>) FROM_CACHE_VALUE;\n    }\n\n    private interface StatsRecorder {\n\n        void onValueAbsent();\n\n        <K> void doRecord(K key);\n\n    }\n\n    private static class NoopStatsRecorder implements StatsRecorder {","sourceCodeStart":326,"sourceCodeEnd":362,"githubUrl":"https://github.com/quarkusio/quarkus/blob/e1c734241f34c7919086ceb4c9262b4a58f6de44/extensions/cache/runtime/src/main/java/io/quarkus/cache/runtime/caffeine/CaffeineCacheImpl.java#L326-L362","documentation":"Thrown by CaffeineCacheImpl.cast when a value produced by the value loading function (or retrieved from the cache) cannot be cast to the generic type T expected by the caller of get(). The cache stores Object values, so mixing value types in one cache triggers a ClassCastException wrapped in a CacheException.","triggerScenarios":"Calling get(key, loadingFunction) where the loading function returns a different type than the entries already cached under that key, or where callers use different generic types for the same cache.","commonSituations":"A loader whose return type was changed; two application methods sharing a cache name but returning different types; dev-mode hot reload retaining old entries.","solutions":["Ensure the loading function's return type matches the cache's declared value type","Use a separate cache name per value type","Clear the cache to drop stale entries of the old type"],"exampleFix":"// before\nCache cache = cacheManager.getCache(\"shared\");\nString s = cache.get(\"k\", id -> new User(id)); // loader returns User, caller wants String\n// after\nCache userCache = cacheManager.getCache(\"userCache\");\nUser u = userCache.get(\"k\", id -> new User(id));","handlingStrategy":"type-guard","validationCode":"// ensure loader output matches the cache's value type\nV loaded = loader.apply(key);\nif (!(loaded instanceof ExpectedType)) {\n    throw new IllegalArgumentException(\"loader returned wrong type\");\n}","typeGuard":"boolean loaderReturnsExpectedType(Function<K, ?> loader, K key) {\n    return loader.apply(key) instanceof ExpectedType;\n}","tryCatchPattern":"try {\n    T value = cache.get(key, loader);\n} catch (CacheException e) {\n    if (e.getCause() instanceof ClassCastException) {\n        cache.invalidate(key).await().indefinitely();\n        value = cache.get(key, loader);\n    } else { throw e; }\n}","preventionTips":["Align the loading function's return type with the cache's generic type","Use dedicated cache names per type","Clear caches after type-changing refactors"],"tags":["cache","caffeine","classcast","loader"],"backgroundTag":"cache-value-type-mismatch","analyzedSha":"e1c734241f34c7919086ceb4c9262b4a58f6de44","analyzedAt":"2026-09-05T17:01:29.979Z","contentChangedAt":"2026-09-05T17:01:29.979Z","schemaVersion":2},"datasetVersion":"2026-09-12T22:17:10.623Z"}