Skip to content

Commit cdddfe0

Browse files
authored
Add ExpressionLanguage condition to cache annotations (#692)
This adds a conditional field to `@Cacheable`, `@CachePut` and `@CacheInvalidate`. Whenever the cache interceptor is invoked via a method annotated with one of these, the conditional is evaluated to a boolean as to whether caching should be performed. As this would slow down existing apps, or those not using conditional, we check to see if the annotations contain a conditional, and if not the additional processing is shortcircuited to go back to the old behavior. Closes #572
1 parent 492bab2 commit cdddfe0

File tree

61 files changed

+1876
-141
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+1876
-141
lines changed

buildSrc/build.gradle

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ plugins {
33
}
44

55
repositories {
6+
gradlePluginPortal()
67
mavenCentral()
78
}
89

910
dependencies {
10-
implementation "org.graalvm.buildtools.native:org.graalvm.buildtools.native.gradle.plugin:0.9.28"
11+
implementation(libs.gradle.graal)
12+
implementation(libs.gradle.kotlin)
1113
}

buildSrc/settings.gradle

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
dependencyResolutionManagement {
2+
versionCatalogs {
3+
libs {
4+
from(files("../gradle/libs.versions.toml"))
5+
}
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package io.micronaut.cache
2+
3+
import io.micronaut.context.annotation.Property
4+
import io.micronaut.test.extensions.spock.annotation.MicronautTest
5+
import jakarta.inject.Inject
6+
import spock.lang.Specification
7+
8+
@MicronautTest
9+
@Property(name = "spec.name", value = "ConditionalCacheSpec")
10+
class ConditionalCacheSpec extends Specification {
11+
12+
@Inject
13+
ConditionalService service
14+
15+
void "test conditional cache"() {
16+
when:
17+
List<String> results = (1..10).collect { service.get(it) }
18+
sleep(10)
19+
List<String> secondResults = (1..10).collect { service.get(it) }
20+
21+
then: "condition results in the last 5 results being cached"
22+
results.drop(5) == secondResults.drop(5)
23+
24+
and: "the first 5 results are not cached"
25+
results.take(5).every {!secondResults.contains(it) }
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package io.micronaut.cache;
2+
3+
import io.micronaut.cache.annotation.CacheConfig;
4+
import io.micronaut.cache.annotation.Cacheable;
5+
import io.micronaut.context.annotation.Requires;
6+
import jakarta.inject.Singleton;
7+
8+
@Singleton
9+
@CacheConfig(cacheNames = {"conditional"})
10+
@Requires(property = "spec.name", value = "ConditionalCacheSpec")
11+
public class ConditionalService {
12+
13+
DocRepo repository = new DocRepo();
14+
15+
// tag::conditional[]
16+
@Cacheable(condition = "#{id > 5}")
17+
public String get(Integer id) {
18+
return repository.get(id);
19+
}
20+
// end::conditional[]
21+
22+
// here to make the docs look nice when we extract the function above
23+
private static class DocRepo {
24+
25+
String get(Integer id) {
26+
return "test " + id + " " + System.currentTimeMillis();
27+
}
28+
}
29+
}

cache-core/src/main/java/io/micronaut/cache/annotation/CacheInvalidate.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2020 original authors
2+
* Copyright 2017-2023 original authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -89,4 +89,14 @@
8989
* @return True if should be done asynchronously
9090
*/
9191
boolean async() default false;
92+
93+
/**
94+
* Evaluated expression that can be used to indicate whether the value should be cached.
95+
* Will be evaluated each time the method is called, and if the condition evaluates to false the cache will not be used.
96+
*
97+
* @see <a href="https://docs.micronaut.io/latest/guide/#evaluatedExpressions">Evaluated Expressions</a>.
98+
* @return The condition
99+
* @since 4.2.0
100+
*/
101+
String condition() default "";
92102
}

cache-core/src/main/java/io/micronaut/cache/annotation/CachePut.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2020 original authors
2+
* Copyright 2017-2023 original authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -97,4 +97,13 @@
9797
* @return True if cache writes should be done asynchronously
9898
*/
9999
boolean async() default false;
100+
101+
/**
102+
* Evaluated expression that can be used to indicate whether the value should be cached.
103+
* Will be evaluated each time the method is called, and if the condition evaluates to false the cache will not be used.
104+
* @see <a href="https://docs.micronaut.io/latest/guide/#evaluatedExpressions">Evaluated Expressions</a>.
105+
* @return The condition
106+
* @since 4.2.0
107+
*/
108+
String condition() default "";
100109
}

cache-core/src/main/java/io/micronaut/cache/annotation/Cacheable.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2020 original authors
2+
* Copyright 2017-2023 original authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -87,4 +87,13 @@
8787
* @return True if an atomic operation should be attempted
8888
*/
8989
boolean atomic() default false;
90+
91+
/**
92+
* Evaluated expression that can be used to indicate whether the value should be cached.
93+
* Will be evaluated each time the method is called, and if the condition evaluates to false the cache will not be used.
94+
* @see <a href="https://docs.micronaut.io/latest/guide/#evaluatedExpressions">Evaluated Expressions</a>.
95+
* @return The condition
96+
* @since 4.2.0
97+
*/
98+
String condition() default "";
9099
}

0 commit comments

Comments
 (0)