Spring Boot整合ehcache
阿新 • • 發佈:2018-11-24
1.pom檔案引入
<!-- caching --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> </dependency>
2.新建ehcache.xml 檔案
<?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"> <cache name="contentsCache" eternal="false" maxEntriesLocalHeap="0" timeToIdleSeconds="200"/> </ehcache>
3.yml配置
server:
port: 80
spring:
# 快取配置
cache:
type: ehcache
ehcache:
config: classpath:ehcache.xml
4.使用Cacheable
啟動類開啟快取@EnableCaching
import com.javasvip.model.vo.TContents; import org.apache.ibatis.annotations.Select; import org.springframework.cache.annotation.CacheConfig; import org.springframework.cache.annotation.Cacheable; import java.util.List; @CacheConfig(cacheNames = "contentsCache") public interface TContentsMapper { @Select("SELECT * FROM t_contents where 1 = 1 --") @Cacheable List<TContents> findTContentsList(); }
5.啟動加入快取
import com.alibaba.druid.pool.DruidDataSource;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import javax.sql.DataSource;
import java.util.concurrent.Executor;
@SpringBootApplication
@EnableCaching
public class BlogApplication {
public static void main(String[] args) {
SpringApplication.run(BlogApplication.class, args);
}
}
6.測試
(1)第一次請求時長
(2)第二次請求時長