{"record":{"id":"b4f2f44194c6f070","repo":"quarkusio/quarkus","slug":"the-maximum-size-can-only-be-changed-if-the-cache","errorCode":null,"errorMessage":"The maximum size can only be changed if the cache was constructed with a maximum-size configuration value","messagePattern":"The maximum size can only be changed if the cache was constructed with a maximum-size configuration value","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"extensions/cache/runtime/src/main/java/io/quarkus/cache/runtime/caffeine/CaffeineCacheImpl.java","lineNumber":325,"sourceCode":"    public void setExpireAfterAccess(Duration duration) {\n        Optional<FixedExpiration<Object, Object>> fixedExpiration = cache.synchronous().policy().expireAfterAccess();\n        if (fixedExpiration.isPresent()) {\n            fixedExpiration.get().setExpiresAfter(duration);\n            cacheInfo.expireAfterAccess = duration;\n        } else {\n            throw new IllegalStateException(\"The access-based expiration policy can only be changed if the cache was \" +\n                    \"constructed with an expire-after-access configuration value\");\n        }\n    }\n\n    @Override\n    public void setMaximumSize(long maximumSize) {\n        Optional<Policy.Eviction<Object, Object>> eviction = cache.synchronous().policy().eviction();\n        if (eviction.isPresent()) {\n            eviction.get().setMaximum(maximumSize);\n            cacheInfo.maximumSize = maximumSize;\n        } else {\n            throw new IllegalStateException(\"The maximum size can only be changed if the cache was constructed with a \" +\n                    \"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) {","sourceCodeStart":307,"sourceCodeEnd":343,"githubUrl":"https://github.com/quarkusio/quarkus/blob/e1c734241f34c7919086ceb4c9262b4a58f6de44/extensions/cache/runtime/src/main/java/io/quarkus/cache/runtime/caffeine/CaffeineCacheImpl.java#L307-L343","documentation":"CaffeineCacheImpl.setMaximumSize can only change the bound if the cache was built with a maximum-size configuration value, because Caffeine's Policy.Eviction is only available for size-bounded caches. Without it, an IllegalStateException is thrown.","triggerScenarios":"Calling setMaximumSize (e.g. from testConfig) on a cache created without quarkus.cache.caffeine.<name>.maximum-size.","commonSituations":"Trying to add a size cap at runtime to an unbounded cache (e.g. configured only with expiration settings).","solutions":["Add quarkus.cache.caffeine.<name>.maximum-size to configuration so the cache is size-bounded at build time","Only call setMaximumSize on caches known to be size-bounded","Recreate the cache with a maximum-size value"],"exampleFix":"// before\n# cache without maximum-size\ncache.setMaximumSize(1000);\n// after\n# application.properties\nquarkus.cache.caffeine.myCache.maximum-size=1000","handlingStrategy":"validation","validationCode":"if (cache.getCacheInfo().maximumSize == null) {\n    throw new IllegalStateException(\"configure maximum-size before calling setMaximumSize\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    cache.setMaximumSize(1000);\n} catch (IllegalStateException e) {\n    log.warn(\"cache is not size-bounded; set maximum-size in config\", e);\n}","preventionTips":["Set maximum-size at build time for caches you resize at runtime","Only adjust eviction on size-bounded caches"],"tags":["cache","caffeine","configuration","eviction"],"backgroundTag":"cache-policy-not-configured","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"}