This is an issue when a caching annotation designed to store method return values is applied to a method that returns no value (void). Since there is no value to cache, the annotation serves no purpose and indicates a misunderstanding of the caching mechanism.
Return value caching annotations are designed to cache the output of a method. When a method annotated for result caching is invoked, the framework computes a cache key and checks if the result has been previously cached. If found, the cached value is returned without executing the method body. If not found, the method executes and its return value is stored in the cache.
This caching mechanism fundamentally requires a return value to function. A void method, by definition, does not return a value - it
only produces side effects. Since there is no value to cache or retrieve, applying result caching annotations to a void method is meaningless and will
not provide any caching benefits.
This misuse typically indicates one of the following:
Framework documentation explicitly states that result caching annotations cannot be used on methods returning void.
While this issue does not cause runtime errors or security vulnerabilities, it can lead to:
Remove the @CacheResult annotation from the void method if caching is not needed, or refactor the method to return a value that can be
meaningfully cached.
@CacheResult(cacheName = "my-cache")
public void processData(String key) { // Noncompliant
// Process expensive data
expensiveService.process(key);
}
@CacheResult(cacheName = "my-cache")
public String processData(String key) {
// Process expensive data and return result
String result = expensiveService.process(key);
return result;
}