相关文档:
{% embed url="docs.spring.io/spring/docs…" %} {% embed url="www.ibm.com/developerwo…" %}
示例:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
@EnableCaching // 开启缓存
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
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 {
// 缓存
@Cacheable(key = "'DEMO'")
public String demoCacheable(String name) {
log.info("demoCacheable: {}", name);
return "Hello " + name;
}
// 更新
@CachePut(key = "'DEMO'")
public String demoCachePut(String name) {
log.info("demoCachePut: {}", name);
return "Hello " + name;
}
// 清除
@CacheEvict(key = "'DEMO'")
public String demoCacheEvict(String name) {
log.info("demoCacheEvict: {}", name);
return "Hello " + name;
}
}