quarkusio/quarkus · error · IllegalStateException

This cache is not an instance of

Error message

This cache is not an instance of 

What it means

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.

Source

Thrown at extensions/cache/runtime/src/main/java/io/quarkus/cache/runtime/AbstractCache.java:26

    public static final String NULL_KEYS_NOT_SUPPORTED_MSG = "Null keys are not supported by the Quarkus application data cache";

    private Object defaultKey;

    @Override
    public Object getDefaultKey() {
        if (defaultKey == null) {
            defaultKey = new DefaultCacheKey(getName());
        }
        return defaultKey;
    }

    @Override
    @SuppressWarnings("unchecked")
    public <T extends Cache> T as(Class<T> type) {
        if (type.isInstance(this)) {
            return (T) this;
        } else {
            throw new IllegalStateException("This cache is not an instance of " + type.getName());
        }
    }

}

View on GitHub (pinned to e1c734241f)

Solutions

  1. Check quarkus.cache.type config matches the concrete type you request in as().
  2. Guard with `if (type.isInstance(cache))` before calling as(), or use as() only when the provider is guaranteed.
  3. Update the code to use the generic Cache API instead of the implementation-specific type.

Example fix

// before
CaffeineCache c = cacheManager.getCache("x").as(CaffeineCache.class);

// after
Cache cache = cacheManager.getCache("x");
if (cache instanceof CaffeineCache) {
    CaffeineCache c = (CaffeineCache) cache;
}
Defensive patterns

Strategy: type-guard

Validate before calling

if (!(cache instanceof CaffeineCache)) {
    throw new IllegalStateException("Expected Caffeine cache, got " + cache.getClass().getName());
}

Type guard

static <T extends Cache> java.util.Optional<T> tryAs(Cache cache, Class<T> type) {
    return type.isInstance(cache) ? java.util.Optional.of(type.cast(cache)) : java.util.Optional.empty();
}

Try / catch

try {
    CaffeineCache c = cache.as(CaffeineCache.class);
} catch (IllegalStateException e) {
    // provider mismatch: use generic Cache API instead
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of quarkusio/quarkus@e1c734241f (2026-09-05). Data as JSON: /api/errors/b569091da685bfb6. Report an issue: GitHub.