Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
621a2a8
Find the Grails cache manager when the context has another one
codeconsole Nov 13, 2026
210ee4d
Use the cache manager Spring would have injected, where it can choose…
codeconsole Nov 13, 2026
695525a
Take the cache manager named grailsCacheManager before a primary one
codeconsole Nov 14, 2026
ef8a898
Look up the cache manager without creating the others
codeconsole Nov 14, 2026
4aaa670
Resolve the cache manager as Spring injected it, checked against the …
codeconsole Nov 14, 2026
bba6ff2
Choose the cache manager when the bean is created, keeping the trait'…
codeconsole Nov 14, 2026
bf0feaa
Fail on an inactive scope, inject in a bare bean factory, report prio…
codeconsole Nov 14, 2026
12b57c2
Never fail a bean over the report, and name the parameters as no bean is
codeconsole Nov 14, 2026
39d3d5e
Say what the report and the parameter names guard against exactly
codeconsole Nov 14, 2026
fd973ec
Run a cached method uncached where its cache does not exist
codeconsole Nov 14, 2026
134856b
Compile the 8.0.0 compatibility fixture without writing class files
codeconsole Nov 14, 2026
d5fd4e8
Resolve the cache manager by an alias of grailsCacheManager instead
codeconsole Nov 17, 2026
7b358da
Merge branch '8.0.x' into fix/cache-users-cache-manager-8.0.x
codeconsole Nov 17, 2026
918c5dd
Merge branch '8.0.x' into fix/cache-users-cache-manager-8.0.x
codeconsole Nov 18, 2026
3b3a79c
Check an application's own grailsCacheManager beside another one
codeconsole Nov 18, 2026
4d4da50
Leave the alias out beside a primary cache manager or one with a prio…
codeconsole Nov 19, 2026
eebd2bb
See the priority of a cache manager's object before the first injection
codeconsole Nov 19, 2026
f88b3ea
Merge branch '8.0.x' into fix/cache-users-cache-manager-8.0.x
codeconsole Nov 19, 2026
1047ebb
Fall back to grailsCacheManager where Spring cannot choose, in place …
codeconsole Nov 20, 2026
b039894
Merge remote-tracking branch 'upstream/8.0.x' into fix/cache-users-ca…
codeconsole Nov 20, 2026
5b6bfc7
Resolve the trait's field once, for the bean being injected
codeconsole Nov 20, 2026
510091c
Merge remote-tracking branch 'upstream/8.0.x' into fix/cache-users-ca…
codeconsole Nov 20, 2026
24e9f70
Remember the cache manager a cache-aware bean was given, as the injec…
codeconsole Nov 20, 2026
dc451a9
Forget a bean's remembered cache manager when it is registered again
codeconsole Nov 20, 2026
5637ca3
Remember the cache manager by the injection point Spring passes, as S…
codeconsole Nov 20, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions grails-cache/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ dependencies {

testImplementation project(':grails-testing-support-datamapping')
testImplementation project(':grails-testing-support-web')
testImplementation testFixtures(project(':grails-core')) // LogCapture
testImplementation 'org.spockframework:spock-core'

testCompileOnly 'org.slf4j:slf4j-nop' // Prevents warning about missing slf4j implementation during compilation
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.grails.plugin.cache

import java.util.concurrent.ConcurrentHashMap

import groovy.transform.CompileStatic
import groovy.util.logging.Slf4j

import org.springframework.cache.CacheManager

/**
* Reports a cache the cache manager does not have, for the code the cache annotations generate.
*
* <p>A cache manager answers null for a name it has no cache of, as {@code JCacheCacheManager} does
* for a cache it was not configured with. The annotated method then runs as it does without a cache
* manager, and this says so once for each cache manager and name.</p>
*
* @since 8.0.1
*/
@Slf4j
@CompileStatic
final class MissingCacheReporter {

/** The names of the caches reported missing, for each cache manager. */
private static final Map<CacheManager, Set<String>> REPORTED = Collections.synchronizedMap(
new WeakHashMap<CacheManager, Set<String>>())

private MissingCacheReporter() {
}

/**
* @param cacheManager the cache manager without the cache
* @param cacheName the name the annotation gives
* @param owner the class of the annotated method
*/
static void report(CacheManager cacheManager, String cacheName, Class<?> owner) {
Set<String> reported = REPORTED.computeIfAbsent(cacheManager) { CacheManager ignored ->
ConcurrentHashMap.<String> newKeySet()
}
if (reported.add(cacheName)) {
log.warn('{} does not cache with {}: its cache manager, {}, has no cache of that name, so the methods ' +
'run uncached. Configure the cache in that cache manager.', owner.name, cacheName, cacheManager.getClass().name)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import org.grails.datastore.gorm.transform.AbstractMethodDecoratingTransformatio
import org.grails.datastore.gorm.transform.AbstractTraitApplyingGormASTTransformation
import org.grails.datastore.mapping.model.config.GormProperties
import org.grails.plugin.cache.GrailsCacheManagerAware
import org.grails.plugin.cache.MissingCacheReporter

import static org.codehaus.groovy.ast.ClassHelper.CLOSURE_TYPE
import static org.codehaus.groovy.ast.ClassHelper.MAP_TYPE
Expand All @@ -59,15 +60,19 @@ import static org.codehaus.groovy.ast.ClassHelper.STRING_TYPE
import static org.codehaus.groovy.ast.ClassHelper.int_TYPE
import static org.codehaus.groovy.ast.ClassHelper.make
import static org.codehaus.groovy.ast.tools.GeneralUtils.args
import static org.codehaus.groovy.ast.tools.GeneralUtils.block
import static org.codehaus.groovy.ast.tools.GeneralUtils.callX
import static org.codehaus.groovy.ast.tools.GeneralUtils.classX
import static org.codehaus.groovy.ast.tools.GeneralUtils.constX
import static org.codehaus.groovy.ast.tools.GeneralUtils.declS
import static org.codehaus.groovy.ast.tools.GeneralUtils.ifElseS
import static org.codehaus.groovy.ast.tools.GeneralUtils.ifS
import static org.codehaus.groovy.ast.tools.GeneralUtils.notNullX
import static org.codehaus.groovy.ast.tools.GeneralUtils.notX
import static org.codehaus.groovy.ast.tools.GeneralUtils.param
import static org.codehaus.groovy.ast.tools.GeneralUtils.params
import static org.codehaus.groovy.ast.tools.GeneralUtils.returnS
import static org.codehaus.groovy.ast.tools.GeneralUtils.stmt
import static org.codehaus.groovy.ast.tools.GeneralUtils.varX
import static org.grails.datastore.gorm.transform.AstMethodDispatchUtils.callD

Expand Down Expand Up @@ -102,6 +107,10 @@ abstract class AbstractCacheTransformation extends AbstractMethodDecoratingTrans
param(MAP_TYPE, 'methodParams')))
private static final ClassNode CACHE_MANAGER_CLASS_NODE = make(CacheManager)
private static final MethodNode GET_CACHE_METHOD_NODE = CACHE_MANAGER_CLASS_NODE.getMethod('getCache', [new Parameter(STRING_TYPE, 'name')] as Parameter[])
private static final ClassNode MISSING_CACHE_REPORTER_CLASS_NODE = make(MissingCacheReporter)
private static final MethodNode REPORT_MISSING_CACHE_METHOD_NODE = MISSING_CACHE_REPORTER_CLASS_NODE.getMethod('report',
[new Parameter(CACHE_MANAGER_CLASS_NODE, 'cacheManager'), new Parameter(STRING_TYPE, 'cacheName'),
new Parameter(make(Class), 'owner')] as Parameter[])
private static final MethodNode MAP_PUT_METHOD = MAP_TYPE.getMethod('put', [new Parameter(OBJECT_TYPE, 'key'), new Parameter(OBJECT_TYPE, 'value')] as Parameter[])
public static final String CACHE_ORIGINAL_METHOD_RETURN_VALUE_LOCAL_VARIABLE_NAME = '$_cache_originalMethodReturnValue'

Expand Down Expand Up @@ -171,6 +180,29 @@ abstract class AbstractCacheTransformation extends AbstractMethodDecoratingTrans
return cacheVariableExpression
}

/**
* Adds to the caching block what runs only where the cache manager has the cache the annotation
* names, and returns the block to add it to. A cache manager answers null for a name it has no
* cache of, as {@code JCacheCacheManager} does for a cache it was not configured with, and the
* method then runs as it does without a cache manager, which is reported once.
*
* @param annotationNode the cache annotation, whose {@code value} names the cache
* @param classNode the class the method is in, which the report names
* @param cacheVariableExpression the cache declared by {@link #declareCache}
* @param cacheBlock the caching block
* @return the block that runs where there is a cache
*/
protected BlockStatement whereCacheExists(AnnotationNode annotationNode, ClassNode classNode,
VariableExpression cacheVariableExpression, BlockStatement cacheBlock) {
BlockStatement whereCacheExists = block()
Object cacheName = ((ConstantExpression) annotationNode.getMember('value')).value
MethodCallExpression report = callX(classX(MISSING_CACHE_REPORTER_CLASS_NODE), 'report',
args(varX(GRAILS_CACHE_MANAGER_PROPERTY_NAME), constX(cacheName), classX(classNode.plainNodeReference)))
report.methodTarget = REPORT_MISSING_CACHE_METHOD_NODE
cacheBlock.addStatement(ifElseS(notNullX(cacheVariableExpression), whereCacheExists, stmt(report)))
return whereCacheExists
}

protected VariableExpression declareCacheKey(SourceUnit sourceUnit, AnnotationNode annotationNode, ClassNode classNode, MethodNode methodNode, BlockStatement cacheBlock) {
ArgumentListExpression createKeyArgs = args(
constX(classNode.getName()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ class CacheEvictTransformation extends AbstractCacheTransformation {
// Cache $_cache_cacheVariable = this.grailsCacheManager.getCache("...");
VariableExpression cacheDeclaration = declareCache(annotationNode, cacheManagerVariableExpression, cachingBlock)

// if($_cache_cacheVariable != null) { ... } else { report the missing cache }
BlockStatement cacheBlock = whereCacheExists(annotationNode, classNode, cacheDeclaration, cachingBlock)

Statement statement

boolean clearAllEntries = false
Expand All @@ -82,16 +85,16 @@ class CacheEvictTransformation extends AbstractCacheTransformation {
statement = stmt(callX(cacheDeclaration, 'clear'))
} else {
// def $_method_parameter_map = [name:value]
declareAndInitializeParameterValueMap(annotationNode, methodNode, cachingBlock)
declareAndInitializeParameterValueMap(annotationNode, methodNode, cacheBlock)

// def $_cache_cacheKey = customCacheKeyGenerator.generate(className, methodName, hashCode, $_method_parameter_map)
VariableExpression cacheKeyDeclaration = declareCacheKey(sourceUnit, annotationNode, classNode, methodNode , cachingBlock)
VariableExpression cacheKeyDeclaration = declareCacheKey(sourceUnit, annotationNode, classNode, methodNode , cacheBlock)

// $_cache_cacheVariable.evict($_cache_cacheKey)
statement = stmt(callX(cacheDeclaration, 'evict', cacheKeyDeclaration))
}

cachingBlock.addStatement(statement)
cacheBlock.addStatement(statement)

newMethodBody.addStatement(
// if(grailsCacheManager != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,17 @@ class CachePutTransformation extends AbstractCacheTransformation {
// Cache $_cache_cacheVariable = this.grailsCacheManager.getCache("...");
VariableExpression cacheDeclaration = declareCache(annotationNode, cacheManagerVariableExpression, cachingBlock)

// if($_cache_cacheVariable != null) { ... } else { report the missing cache }
BlockStatement cacheBlock = whereCacheExists(annotationNode, classNode, cacheDeclaration, cachingBlock)

// def $_method_parameter_map = [name:value]
declareAndInitializeParameterValueMap(annotationNode, methodNode, cachingBlock)
declareAndInitializeParameterValueMap(annotationNode, methodNode, cacheBlock)

// def $_cache_cacheKey = customCacheKeyGenerator.generate(className, methodName, hashCode, $_method_parameter_map)
VariableExpression cacheKeyDeclaration = declareCacheKey(sourceUnit, annotationNode, classNode, methodNode , cachingBlock)
VariableExpression cacheKeyDeclaration = declareCacheKey(sourceUnit, annotationNode, classNode, methodNode , cacheBlock)

VariableExpression originalValueExpr = varX(CACHE_ORIGINAL_METHOD_RETURN_VALUE_LOCAL_VARIABLE_NAME)
cachingBlock.addStatement(
cacheBlock.addStatement(
block(
declS(originalValueExpr, originalMethodCallExpr),
stmt(callD(cacheDeclaration, 'put', args(cacheKeyDeclaration, originalValueExpr))),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,18 @@ class CacheableTransformation extends AbstractCacheTransformation {
// Cache $_cache_cacheVariable = this.grailsCacheManager.getCache("...");
VariableExpression cacheDeclaration = declareCache(annotationNode, cacheManagerVariableExpression, cachingBlock)

// if($_cache_cacheVariable != null) { ... } else { report the missing cache }
BlockStatement cacheBlock = whereCacheExists(annotationNode, classNode, cacheDeclaration, cachingBlock)

// def $_method_parameter_map = [name:value]
declareAndInitializeParameterValueMap(annotationNode, methodNode, cachingBlock)
declareAndInitializeParameterValueMap(annotationNode, methodNode, cacheBlock)

// def $_cache_cacheKey = customCacheKeyGenerator.generate(className, methodName, hashCode, $_method_parameter_map)
VariableExpression cacheKeyDeclaration = declareCacheKey(sourceUnit, annotationNode, classNode, methodNode , cachingBlock)
VariableExpression cacheKeyDeclaration = declareCacheKey(sourceUnit, annotationNode, classNode, methodNode , cacheBlock)

// ValueWrapper $_cache_valueWrapper = $_cache_cacheVariable.get($_cache_cacheKey);
VariableExpression cacheValueWrapper = varX(CACHE_VALUE_WRAPPER_LOCAL_VARIABLE_NAME, make(Cache.ValueWrapper))
cachingBlock.addStatement(
cacheBlock.addStatement(
declS(cacheValueWrapper, callD(cacheDeclaration, 'get', cacheKeyDeclaration))
)

Expand All @@ -106,7 +109,7 @@ class CacheableTransformation extends AbstractCacheTransformation {
// return $_cache_originalMethodReturnValue;
// }
VariableExpression originalValueExpr = varX(CACHE_ORIGINAL_METHOD_RETURN_VALUE_LOCAL_VARIABLE_NAME)
cachingBlock.addStatement(
cacheBlock.addStatement(
ifElseS(notNullX(cacheValueWrapper),
returnS(callD(cacheValueWrapper, 'get')),
block(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import org.springframework.cache.Cache

import grails.plugins.Plugin
import org.grails.plugin.cache.GrailsCacheManager
import org.grails.plugin.cache.GrailsCacheManagerFallback

/**
* Configures the cache plugin.
Expand Down Expand Up @@ -80,6 +81,10 @@ class CacheGrailsPlugin extends Plugin {

bean(CustomCacheKeyGenerator).conditionalOnMissingBeanName()

// Spring takes grailsCacheManager for the field of a cache-aware class beside a second cache
// manager, such as the one cacheUsers defines, where it cannot otherwise choose.
bean(GrailsCacheManagerFallback).staticMethod()

bean(GrailsCacheManager).conditionalOnMissingBeanName() { CachePluginConfiguration grailsCacheConfiguration ->
if (cacheManagerType == 'GrailsConcurrentLinkedMapCacheManager') {
return new GrailsConcurrentLinkedMapCacheManager(configuration: grailsCacheConfiguration)
Expand Down
Loading
Loading