Related documents:
Embed {% url = “docs. Spring. IO/spring/docs…” Embed %} {% url = “www.ibm.com/developerwo…” %}
Example:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
@EnableCaching // Enable caching
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); }}Copy the code
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.stream.Collectors;
@Slf4j
@Service
@CacheConfig(cacheNames = "Demo")
public class DemoService {
/ / cache
@Cacheable(key = "'DEMO'")
public String demoCacheable(String name) {
log.info("demoCacheable: {}", name);
return "Hello " + name;
}
/ / update
@CachePut(key = "'DEMO'")
public String demoCachePut(String name) {
log.info("demoCachePut: {}", name);
return "Hello " + name;
}
/ / remove
@CacheEvict(key = "'DEMO'")
public String demoCacheEvict(String name) {
log.info("demoCacheEvict: {}", name);
return "Hello "+ name; }}Copy the code