-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
61 changed files
with
1,876 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
dependencyResolutionManagement { | ||
versionCatalogs { | ||
libs { | ||
from(files("../gradle/libs.versions.toml")) | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
cache-caffeine/src/test/groovy/io/micronaut/cache/ConditionalCacheSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package io.micronaut.cache | ||
|
||
import io.micronaut.context.annotation.Property | ||
import io.micronaut.test.extensions.spock.annotation.MicronautTest | ||
import jakarta.inject.Inject | ||
import spock.lang.Specification | ||
|
||
@MicronautTest | ||
@Property(name = "spec.name", value = "ConditionalCacheSpec") | ||
class ConditionalCacheSpec extends Specification { | ||
|
||
@Inject | ||
ConditionalService service | ||
|
||
void "test conditional cache"() { | ||
when: | ||
List<String> results = (1..10).collect { service.get(it) } | ||
sleep(10) | ||
List<String> secondResults = (1..10).collect { service.get(it) } | ||
|
||
then: "condition results in the last 5 results being cached" | ||
results.drop(5) == secondResults.drop(5) | ||
|
||
and: "the first 5 results are not cached" | ||
results.take(5).every {!secondResults.contains(it) } | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
cache-caffeine/src/test/java/io/micronaut/cache/ConditionalService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package io.micronaut.cache; | ||
|
||
import io.micronaut.cache.annotation.CacheConfig; | ||
import io.micronaut.cache.annotation.Cacheable; | ||
import io.micronaut.context.annotation.Requires; | ||
import jakarta.inject.Singleton; | ||
|
||
@Singleton | ||
@CacheConfig(cacheNames = {"conditional"}) | ||
@Requires(property = "spec.name", value = "ConditionalCacheSpec") | ||
public class ConditionalService { | ||
|
||
DocRepo repository = new DocRepo(); | ||
|
||
// tag::conditional[] | ||
@Cacheable(condition = "#{id > 5}") | ||
public String get(Integer id) { | ||
return repository.get(id); | ||
} | ||
// end::conditional[] | ||
|
||
// here to make the docs look nice when we extract the function above | ||
private static class DocRepo { | ||
|
||
String get(Integer id) { | ||
return "test " + id + " " + System.currentTimeMillis(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.