Today, I try to do a bit springboot integrated springcache cache, springcache is based on the annotation (comment) is a caching technology
Features are summarized as follows:
- By a small amount of configuration annotation comments to make existing code caching support
- Out of the box support for Out-Of-The-Box, that is, without installation and deployment of additional third-party components can use the cache
- Support Spring Express Language, any property or method can be used to define the object cache key and condition
- Support for AspectJ, and through its implementing any method of caching support
- Support for custom and custom cache key managers have considerable flexibility and scalability
Also very simple to use
The first step: add maven dependent
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency>
Step Two: Create a new class TestTime
import java.util.Date; public class TestTime { private Date time; public TestTime(Date time){ this.time=time; } public Date getTime() { return time; } public void setTime(Date time) { this.time = time; } @Override public String toString() { return "{ 时间='" + time + '\'' + '}'; } }
The third step is to add a TestTime Interface
import hello.entity.TestTime; public interface TestTimeService { TestTime getTestTime(); }
The fourth step, the main function is added:
@EnableCaching
open the cache, this is very important
The fifth step, to implement the interface:
import hello.Service.TestTimeService; import hello.entity.TestTime; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Component; import java.util.Date; @Component public class TestTimeController implements TestTimeService { @Override @Cacheable(value = "time") public TestTime getTestTime(){ return new TestTime(new Date()); } }
Use @Cachable you can use caching, very simple.
About introduction carefully annotated cache can refer to the article: https://www.cnblogs.com/fashflying/p/6908028.html
The sixth step, create a test class TestRun:
import hello.Service.TestTimeService; import org.junit.Test; import org.junit.runner.RunWith; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest public class TestRun { private static final Logger log = LoggerFactory.getLogger(TestRun.class); @Autowired TestTimeService testTimeService; @Test public void getTime() throws InterruptedException { int i = 1; while (i <= 20) { log.info("" + "第" + i + "次获取时间" + testTimeService.getTestTime()); i++; Thread.sleep(1000); } } }
Directory created as follows:
Finally: Run getTime test methods:
It can be seen 20 times to call gettesttime, not the latest acquisition time, but time in the cache.
This article Source: https://gitee.com/Hiro-D/Java/tree/master/Spring-cache
直接使用springcahce是无法设置缓存有效时间的,是要使用ehcache或者redis 或者guava设置,下篇讲一下springcahce设置过期时间。