This annotation indicates that the result of a
method invocation should be cached so that
subsequent invocations with the same
arguments can return the result from the
cache.
1. Before the method execution, Spring
generates a cache key based on the
method arguments and the specified
cache name.
2. Spring checks if the cache contains a
value associated with the generated
cache key.
3. If a cached value is found, it is returned
directly, and the method is not executed.
4. The method is executed if no cached
value is found, and the result is stored in
the cache with the generated key.
5. The result of the method is returned to the
caller.
xxxxxxxxxx
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
@Service
public class MyCacheService {
@Cacheable(value = ”healthCareCache", key = "#name")
public String getGreeting(String name) {
/
// Simulating an expensive operation
try {
Thread.sleep(7000);
} catch (InterruptedException e) { e.printStackTrace(); }
return "Hello, " + name + "! How are you today?";
}
}