### Custom Cache Key Generation with @Cacheable Annotation Source: https://context7.com/apache/grails-cache/llms.txt Enables custom cache key generation using SpEL expressions, allowing dynamic keys based on method arguments. This example uses the second argument (`#argTwo`) as the cache key for the 'demo' cache. Requires `grails.plugin.cache.Cacheable` and `groovy.transform.CompileStatic`. ```groovy import grails.plugin.cache.Cacheable import groovy.transform.CompileStatic @CompileStatic class DemoService { @Cacheable(value='demo', key='#argTwo') def methodTwo(String argOne, String argTwo) { 2112 } } ``` -------------------------------- ### Conditional Caching with @Cacheable Annotation Source: https://context7.com/apache/grails-cache/llms.txt Implements conditional caching using SpEL expressions in the `condition` attribute. The method result is only cached if the condition evaluates to true. This example caches the result of `multiply` only when the input `x` is less than 10. Requires `grails.plugin.cache.Cacheable` and `groovy.transform.CompileStatic`. ```groovy import grails.plugin.cache.Cacheable import groovy.transform.CompileStatic @CompileStatic class DemoService { @Cacheable(value = 'basic', condition='#x < 10') def multiply(int x, int y) { x * y } } ``` -------------------------------- ### Annotate Methods for Compile-Time Caching in Groovy Source: https://github.com/apache/grails-cache/wiki/AST-Based-Implementation Demonstrates how to use the @Cacheable annotation in Groovy to configure caching behavior for methods. Supports specifying cache names, keys, and conditional caching based on method arguments. This code is processed at compile time. ```groovy import grails.plugin.cache.Cacheable import groovy.transform.CompileStatic @CompileStatic class DemoService { @Cacheable('demo') def methodOne() { 42 } @Cacheable(value='demo', key='#argTwo') def methodTwo(String argOne, String argTwo) { 2112 } @Cacheable(value = 'basic', condition='#x < 10') def multiply(int x, int y) { x * y } } ``` -------------------------------- ### Basic Method Caching with @Cacheable Annotation Source: https://context7.com/apache/grails-cache/llms.txt Marks a method for caching. The plugin checks the cache for existing results before executing the method. If not found, it executes the method and stores the result in the cache named 'demo'. Requires `grails.plugin.cache.Cacheable` and `groovy.transform.CompileStatic`. ```groovy import grails.plugin.cache.Cacheable import groovy.transform.CompileStatic @CompileStatic class DemoService { @Cacheable('demo') def methodOne() { 42 } } ``` -------------------------------- ### Java: Grails Cache Configuration Properties Source: https://github.com/apache/grails-cache/wiki/AST-Based-Implementation This Java code defines getter and setter methods for custom cache key generators and the Grails cache manager. It allows for programmatic configuration of caching behavior within a Grails application. ```java public class GrailsCacheConfiguration { private GrailsCacheKeyGenerator customCacheKeyGenerator; private CacheManager grailsCacheManager; public GrailsCacheKeyGenerator getCustomCacheKeyGenerator() { return this.customCacheKeyGenerator; } public void setCustomCacheKeyGenerator(GrailsCacheKeyGenerator var1) { this.customCacheKeyGenerator = var1; } public CacheManager getGrailsCacheManager() { return this.grailsCacheManager; } public void setGrailsCacheManager(CacheManager var1) { this.grailsCacheManager = var1; } } ``` -------------------------------- ### Generated Java Code for Compile-Time Caching in Grails Source: https://github.com/apache/grails-cache/wiki/AST-Based-Implementation Illustrates the Java code generated by the Grails compiler for methods annotated with @Cacheable. It includes logic for cache lookup, retrieval, and storage using GrailsCacheManager and GrailsCacheKeyGenerator. This code represents the compile-time transformation. ```java public class DemoService { @Autowired( required = false ) private GrailsCacheManager grailsCacheManager; @Cacheable({"demo"}) public Object methodOne() { if(this.grailsCacheManager != null) { LinkedHashMap $_method_parameter_map = new LinkedHashMap(); Cache $_cache_cacheVariable = this.grailsCacheManager.getCache("demo"); Object $_cache_cacheKey = this.customCacheKeyGenerator.generate("com.demo.DemoService", "methodOne", this.hashCode(), $_method_parameter_map, (String)null); ValueWrapper $_cache_valueWrapper = $_cache_cacheVariable.get($_cache_cacheKey); if($_cache_valueWrapper != null) { return $_cache_valueWrapper.get(); } else { Object $_cache_originalMethodReturnValue = this.$_cache_methodOne(); $_cache_cacheVariable.put($_cache_cacheKey, $_cache_originalMethodReturnValue); return $_cache_originalMethodReturnValue; } } else { return this.$_cache_methodOne(); } } @Cacheable( key = "#argTwo", value = {"demo"} ) public Object methodTwo(String argOne, String argTwo) { if(this.grailsCacheManager != null) { LinkedHashMap $_method_parameter_map = new LinkedHashMap(); $_method_parameter_map.put("argOne", argOne); $_method_parameter_map.put("argTwo", argTwo); Cache $_cache_cacheVariable = this.grailsCacheManager.getCache("demo"); Object $_cache_cacheKey = this.customCacheKeyGenerator.generate("com.demo.DemoService", "methodTwo", this.hashCode(), $_method_parameter_map, (String)"#argTwo"); ValueWrapper $_cache_valueWrapper = $_cache_cacheVariable.get($_cache_cacheKey); if($_cache_valueWrapper != null) { return $_cache_valueWrapper.get(); } else { Object $_cache_originalMethodReturnValue = this.$_cache_methodTwo(argOne, argTwo); $_cache_cacheVariable.put($_cache_cacheKey, $_cache_originalMethodReturnValue); return $_cache_originalMethodReturnValue; } } else { return this.$_cache_methodTwo(argOne, argTwo); } } @Cacheable( condition = "#x < 10", value = {"basic"} ) public Object multiply(int x, int y) { if(this.grailsCacheManager != null) { LinkedHashMap $_method_parameter_map = new LinkedHashMap(); $_method_parameter_map.put("x", Integer.valueOf(x)); $_method_parameter_map.put("y", Integer.valueOf(y)); Cache $_cache_cacheVariable = this.grailsCacheManager.getCache("basic"); if(DefaultTypeTransformation.booleanUnbox(ScriptBytecodeAdapter.invokeStaticMethodN(DemoService.class, CacheConditionUtils.class, "shouldCache", new Object[]{this, $_method_parameter_map, "#x < 10"}))) { Object $_cache_cacheKey = this.customCacheKeyGenerator.generate("com.demo.DemoService", "multiply", this.hashCode(), $_method_parameter_map, (String)null); ValueWrapper $_cache_valueWrapper = $_cache_cacheVariable.get($_cache_cacheKey); if($_cache_valueWrapper != null) { return $_cache_valueWrapper.get(); } else { Object $_cache_originalMethodReturnValue = this.$_cache_multiply(x, y); $_cache_cacheVariable.put($_cache_cacheKey, $_cache_originalMethodReturnValue); return $_cache_originalMethodReturnValue; } } else { return this.$_cache_multiply(x, y); } } else { return this.$_cache_multiply(x, y); } } protected Object $_cache_methodOne() { return Integer.valueOf(42); } protected Object $_cache_methodTwo(String argOne, String argTwo) { return Integer.valueOf(2112); } protected Object $_cache_multiply(int x, int y) { return Integer.valueOf(x * y); } public GrailsCacheKeyGenerator getCustomCacheKeyGenerator() { ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.