{"record":{"id":"b569091da685bfb6","repo":"quarkusio/quarkus","slug":"this-cache-is-not-an-instance-of","errorCode":null,"errorMessage":"This cache is not an instance of ","messagePattern":"This cache is not an instance of ","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"extensions/cache/runtime/src/main/java/io/quarkus/cache/runtime/AbstractCache.java","lineNumber":26,"sourceCode":"    public static final String NULL_KEYS_NOT_SUPPORTED_MSG = \"Null keys are not supported by the Quarkus application data cache\";\n\n    private Object defaultKey;\n\n    @Override\n    public Object getDefaultKey() {\n        if (defaultKey == null) {\n            defaultKey = new DefaultCacheKey(getName());\n        }\n        return defaultKey;\n    }\n\n    @Override\n    @SuppressWarnings(\"unchecked\")\n    public <T extends Cache> T as(Class<T> type) {\n        if (type.isInstance(this)) {\n            return (T) this;\n        } else {\n            throw new IllegalStateException(\"This cache is not an instance of \" + type.getName());\n        }\n    }\n\n}\n","sourceCodeStart":8,"sourceCodeEnd":31,"githubUrl":"https://github.com/quarkusio/quarkus/blob/e1c734241f34c7919086ceb4c9262b4a58f6de44/extensions/cache/runtime/src/main/java/io/quarkus/cache/runtime/AbstractCache.java#L8-L31","documentation":"Cache.as(Type) is a safe downcast helper on the cache abstraction. It throws IllegalStateException instead of returning null/ClassCastException when the underlying cache implementation does not extend the requested type, so callers can detect wrong assumptions explicitly.","triggerScenarios":"Calling `cache.as(CaffeineCache.class)` (or another concrete type) on a cache whose runtime implementation is different — e.g. the cache was configured with a different provider (infinispan vs caffeine) or is a NOOP/test cache.","commonSituations":"Code written against the Caffeine extension later run with Infinispan as the active cache type; tests using a mock/NOOP cache; refactors changing quarkus.cache.type without updating as() calls.","solutions":["Check quarkus.cache.type config matches the concrete type you request in as().","Guard with `if (type.isInstance(cache))` before calling as(), or use as() only when the provider is guaranteed.","Update the code to use the generic Cache API instead of the implementation-specific type."],"exampleFix":"// before\nCaffeineCache c = cacheManager.getCache(\"x\").as(CaffeineCache.class);\n\n// after\nCache cache = cacheManager.getCache(\"x\");\nif (cache instanceof CaffeineCache) {\n    CaffeineCache c = (CaffeineCache) cache;\n}","handlingStrategy":"type-guard","validationCode":"if (!(cache instanceof CaffeineCache)) {\n    throw new IllegalStateException(\"Expected Caffeine cache, got \" + cache.getClass().getName());\n}","typeGuard":"static <T extends Cache> java.util.Optional<T> tryAs(Cache cache, Class<T> type) {\n    return type.isInstance(cache) ? java.util.Optional.of(type.cast(cache)) : java.util.Optional.empty();\n}","tryCatchPattern":"try {\n    CaffeineCache c = cache.as(CaffeineCache.class);\n} catch (IllegalStateException e) {\n    // provider mismatch: use generic Cache API instead\n}","preventionTips":["Check quarkus.cache.type before relying on implementation-specific APIs","Use the generic Cache interface unless you truly need provider features","Keep test and prod cache providers aligned"],"tags":["quarkus","cache","downcast","illegal-state"],"backgroundTag":"unsupported-cache-type-cast","analyzedSha":"e1c734241f34c7919086ceb4c9262b4a58f6de44","analyzedAt":"2026-09-05T17:01:29.979Z","contentChangedAt":"2026-09-05T17:01:29.979Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}