{"record":{"id":"36b4dd33db46776d","repo":"quarkusio/quarkus","slug":"the-access-based-expiration-policy-can-only-be-cha","errorCode":null,"errorMessage":"The access-based expiration policy can only be changed if the cache was constructed with an expire-after-access configuration value","messagePattern":"The access-based expiration policy can only be changed if the cache was constructed with an expire-after-access 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":313,"sourceCode":"    public void setExpireAfterWrite(Duration duration) {\n        Optional<FixedExpiration<Object, Object>> fixedExpiration = cache.synchronous().policy().expireAfterWrite();\n        if (fixedExpiration.isPresent()) {\n            fixedExpiration.get().setExpiresAfter(duration);\n            cacheInfo.expireAfterWrite = duration;\n        } else {\n            throw new IllegalStateException(\"The write-based expiration policy can only be changed if the cache was \" +\n                    \"constructed with an expire-after-write configuration value\");\n        }\n    }\n\n    @Override\n    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() {","sourceCodeStart":295,"sourceCodeEnd":331,"githubUrl":"https://github.com/quarkusio/quarkus/blob/e1c734241f34c7919086ceb4c9262b4a58f6de44/extensions/cache/runtime/src/main/java/io/quarkus/cache/runtime/caffeine/CaffeineCacheImpl.java#L295-L331","documentation":"CaffeineCacheImpl.setExpireAfterAccess requires that the cache was constructed with an expire-after-access configuration value, since Caffeine only exposes the access-based FixedExpiration policy in that case. Otherwise an IllegalStateException is thrown.","triggerScenarios":"Calling setExpireAfterAccess (e.g. from testConfig) on a cache configured without quarkus.cache.caffeine.<name>.expire-after-access.","commonSituations":"Attempting to enable access-based expiry at runtime on a cache configured with only expire-after-write or maximum-size.","solutions":["Set quarkus.cache.caffeine.<name>.expire-after-access in configuration","Guard the call by checking the policy is present before mutating","Recreate the cache with the desired configuration"],"exampleFix":"// before\n# no expire-after-access configured\ncache.setExpireAfterAccess(Duration.ofMinutes(5));\n// after\n# application.properties\nquarkus.cache.caffeine.myCache.expire-after-access=5M","handlingStrategy":"validation","validationCode":"if (cache.getCacheInfo().expireAfterAccess == null) {\n    throw new IllegalStateException(\"configure expire-after-access before calling setExpireAfterAccess\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    cache.setExpireAfterAccess(duration);\n} catch (IllegalStateException e) {\n    log.warn(\"cache lacks access expiration policy; update application.properties\", e);\n}","preventionTips":["Declare expire-after-access in config for caches tuned at runtime","Keep a checklist of which caches use which policies"],"tags":["cache","caffeine","configuration","expiration"],"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"}