Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
75 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
afb09fe
fix(hibernate5): release what preHandle acquired when it fails partway
codeconsole Nov 20, 2026
07e9c87
fix(hibernate7): release what preHandle acquired when it fails partway
codeconsole Nov 20, 2026
24e9f70
Remember the cache manager a cache-aware bean was given, as the injec…
codeconsole Nov 20, 2026
a55e538
fix(hibernate5): close a session whose setup fails in preHandle
codeconsole Nov 20, 2026
0d5988c
fix(hibernate7): close a session whose setup fails in preHandle
codeconsole Nov 20, 2026
dc451a9
Forget a bean's remembered cache manager when it is registered again
codeconsole Nov 20, 2026
0a78b9b
fix(hibernate5): close the default session when applying its flush mo…
codeconsole Nov 20, 2026
9a35796
fix(hibernate7): close the default session when applying its flush mo…
codeconsole Nov 20, 2026
5637ca3
Remember the cache manager by the injection point Spring passes, as S…
codeconsole Nov 20, 2026
f2e7ee4
fix(hibernate7): close the default session when the parent's flush mo…
codeconsole Nov 21, 2026
7d814e4
fix(hibernate7): release preHandle's sessions on a checked exception too
codeconsole Nov 22, 2026
79329f2
Merge branch '8.0.x' into fix/cache-users-cache-manager-8.0.x
codeconsole Nov 22, 2026
c1a74cd
Keep the trait's field empty where its resolution found nothing
codeconsole Nov 22, 2026
c0b60c0
fix(hibernate7): match a joined session's factory by identity, not by…
codeconsole Nov 22, 2026
bc878b2
fix(hibernate7): attach a failure to close a session to the failure b…
codeconsole Nov 22, 2026
d8c38dd
Leave the trait's field to Spring where it cannot fall back
codeconsole Nov 22, 2026
eb8ac24
test(hibernate7): register the fake session factories through setHibe…
codeconsole Nov 22, 2026
407595b
fix(hibernate7): keep applyFlushMode's contract, and never bind a ses…
codeconsole Nov 22, 2026
097c0d0
fix(hibernate7): release every session past a failure, and report eac…
codeconsole Nov 22, 2026
8182ed6
fix(hibernate7): share the default session's participation name among…
codeconsole Nov 22, 2026
4438cb6
Inject the trait's field as Spring injects a field, falling back wher…
codeconsole Nov 22, 2026
acd6ebd
Read the cache manager once a call, and report each cache manager by …
codeconsole Nov 22, 2026
3e588ab
Describe the cache tags and the cache annotations apart
codeconsole Nov 22, 2026
8229d7b
Inject a cache-aware bean's members in Spring's order around the trai…
codeconsole Nov 22, 2026
40b69c8
Fall back in the injection that Spring's AOT processing generates
codeconsole Nov 22, 2026
5400a9e
Fall back in each copy of the trait's field a class's hierarchy has
codeconsole Nov 22, 2026
459bd82
Merge branch '8.0.x' into fix/osiv-prehandle-failure-8.0.x
codeconsole Nov 22, 2026
7429671
test(hibernate7): a failed flush as a request goes asynchronous leave…
codeconsole Nov 22, 2026
f4384f3
Keep the code the cache annotations generate clear of the method's pa…
codeconsole Nov 22, 2026
3799fde
Report a missing cache once when its first calls come at once
codeconsole Nov 22, 2026
ecf2871
Take over from Spring's injection what it would inject, on every path…
codeconsole Nov 22, 2026
3e830f9
Say that two primary cache managers still stop startup
codeconsole Nov 22, 2026
5a04c09
Merge branch '7.0.x' of apache/grails-core into fix/osiv-prehandle-fa…
codeconsole Nov 22, 2026
f74d298
test(hibernate5): give the interceptor a datastore that refuses a ses…
codeconsole Nov 22, 2026
eb8a3a3
fix(hibernate5): close the default session when the flush mode the pa…
codeconsole Nov 22, 2026
9e50bc2
fix(hibernate7): take a factory's proxy and its target as one factory…
codeconsole Nov 22, 2026
5d9ff5d
fix(hibernate7): close the default session Spring takes off the threa…
codeconsole Nov 22, 2026
8ea1cb8
fix(hibernate5): release the OSIV sessions past every failure, and ma…
codeconsole Nov 22, 2026
f020847
Merge branch '8.0.x' into fix/osiv-prehandle-failure-8.0.x
codeconsole Nov 22, 2026
8fab66a
Merge pull request #17137 from codeconsole/merge/7.0.x-into-7.1.x
codeconsole Nov 22, 2026
230092b
Merge pull request #17138 from codeconsole/merge/7.1.x-into-7.2.x
codeconsole Nov 22, 2026
2614152
Merge pull request #17139 from codeconsole/merge/7.2.x-into-8.0.x
codeconsole Nov 22, 2026
ec443fc
Merge pull request #17086 from codeconsole/fix/osiv-prehandle-failure…
codeconsole Nov 23, 2026
6d3dd8d
Merge branch '7.0.x' into 7.1.x
codeconsole Nov 23, 2026
2b14999
Merge branch '7.1.x' into 7.2.x
codeconsole Nov 23, 2026
e91bfc4
Merge branch '7.2.x' into 8.0.x
codeconsole Nov 23, 2026
b43638f
Merge pull request #17148 from codeconsole/merge/7.2.x-into-8.0.x
codeconsole Nov 23, 2026
5bdcbc2
Merge pull request #17087 from codeconsole/fix/osiv-prehandle-failure…
codeconsole Nov 23, 2026
1d5162a
Go back to the alias of grailsCacheManager
codeconsole Nov 23, 2026
1f36e90
Read grailsCacheManager in the generated code as round 2 did
codeconsole Nov 23, 2026
b7af0a1
Check inherited @Inject, @Value and @Resource methods against 8.0.0
codeconsole Nov 23, 2026
fca95de
Merge pull request #16980 from codeconsole/fix/cache-users-cache-mana…
codeconsole Nov 23, 2026
310c95f
Merge branch '8.0.x' into 8.1.x
codeconsole Nov 23, 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,104 @@
/*
* 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.lang.ref.Reference
import java.lang.ref.ReferenceQueue
import java.lang.ref.WeakReference
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. A cache manager is told apart from
* another by its identity, as two may be equal, and is not kept from being collected.</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<CacheManagerKey, Set<String>> REPORTED = new ConcurrentHashMap<CacheManagerKey, Set<String>>()

private static final ReferenceQueue<CacheManager> COLLECTED = new ReferenceQueue<CacheManager>()

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) {
Reference<?> collected
while ((collected = COLLECTED.poll()) != null) {
REPORTED.remove(collected)
}
Set<String> reported = REPORTED.get(new CacheManagerKey(cacheManager, null))
if (reported == null) {
Set<String> created = ConcurrentHashMap.<String> newKeySet()
// the set another call published first, empty as it may still be, is the one to add to
Set<String> published = REPORTED.putIfAbsent(new CacheManagerKey(cacheManager, COLLECTED), created)
reported = published != null ? published : created
}
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)
}
}

/** A cache manager by its identity, held weakly. */
@CompileStatic
private static final class CacheManagerKey extends WeakReference<CacheManager> {

private final int hash

CacheManagerKey(CacheManager cacheManager, ReferenceQueue<CacheManager> queue) {
super(cacheManager, queue)
this.hash = System.identityHashCode(cacheManager)
}

@Override
int hashCode() {
hash
}

@Override
boolean equals(Object other) {
if (this.is(other)) {
return true
}
if (!(other instanceof CacheManagerKey)) {
return false
}
CacheManager cacheManager = get()
cacheManager != null && cacheManager.is(((CacheManagerKey) other).get())
}
}
}
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.GrailsCacheManagerAliasRegistrar

/**
* 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(GrailsCacheManagerAliasRegistrar).staticMethod()

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