前些天發現了一個巨牛的人工智能學習網站,通俗易懂,風趣幽默,忍不住分享一下給大家。點擊跳轉到教程。
步驟:
1. pom文件中加 maven jar包:
?
<!-- ehcache 緩存 --><dependency><groupId>net.sf.ehcache</groupId><artifactId>ehcache</artifactId></dependency>
?
<!--開啟緩存支持--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId></dependency>
?
2. 新增一個配置文件 ehcache.xml,放在resource 下面,springboot會自動掃描 :
?
<?xml version="1.0" encoding="UTF-8"?>
<ehcache><!--ehcache 緩存--><cache name="department" maxElementsInMemory="1000" />
</ehcache>
更多配置參考:
<ehcache><!--ehcache 緩存--><cache name="department"maxElementsInMemory="100000"eternal="true"overflowToDisk="true"maxElementsOnDisk="10000000"diskPersistent="true"memoryStoreEvictionPolicy="LRU"/><defaultCacheeternal="false"maxElementsInMemory="10000"overflowToDisk="false"diskPersistent="false"timeToIdleSeconds="0"timeToLiveSeconds="600"memoryStoreEvictionPolicy="LRU"/></ehcache>
?
3. ?在 main 方法上加上注解 @EnableCaching,開啟緩存的使用:
@EnableCaching // 開啟緩存使用
@SpringBootApplication
public class Application { // extends SpringBootServletInitializerpublic static void main(String[] args) {SpringApplication application = new SpringApplication(Application.class, "classpath*:/spring/security-*.xml");application.setWebEnvironment(true);application.run(args);}/*** HOW TO MAKE A SPRING BOOT JAR INTO A WAR TO DEPLOY ON TOMCAT*/
// @Override
// protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
// // Customize the application or call application.sources(...) to add sources
//
// application.sources(Application.class);
// return application;
// }}
4. 在方法中運用注解,實現緩存的 增、刪、改、查
?
只要在方法上加上對應注解就可以了。
@Cacheable 查:?如果有就直接緩存中取 沒有就數據庫查并放入緩存。加上這個注解,調用這個方法就可以取到緩存中的值。
@CacheEvict?新增、刪除、修改 :會自動清除緩存中內容。加上這注解,對數據庫的update、add、delete操作都會清除對應緩存。
?
如:緩存名為“ department ”,當調用此方法時會先判斷是否有緩存。有則不進入方法,直接返回緩存中的值。無緩存名為 “department” 的緩存才會進入方法內部,執行數據庫查詢。
@Override@Cacheable(value = "department")public List<Department> getDepartmentList() {System.out.println("--------------------------------緩存查詢:查查查-樹樹樹\n");List<Department> departmentList = departmentRepository.findAll(new Sort(Sort.Direction.ASC, "id"));return (List<Department>) buildDepartmentTree(departmentList, null);}
?
可以給緩存中的具體項起個鍵值:key
@Override@Cacheable(value = "department", key = "#departmentId")public Object findOne(String departmentId) {return departmentRepository.findOne(departmentId);}
?
當緩存key沒有全部命中時,要確保緩存全部清除,就要加上
allEntries = true ,默認為false
@Override@Transactional(readOnly = false)@CacheEvict(value = "department", allEntries = true) // 新增、刪除、修改 all is it.public Object updateDepartmentById(String id, Department departmentDto) {Department department = departmentRepository.findOne(id);if (department == null) {return "不存在該部門";}BeanHelper.mapPartOverrider(department, departmentDto);departmentRepository.save(department);return department;}
?
?
?
spring提供了4個注解來聲明緩存規則(又是使用注解式的AOP的一個生動例子),如表。
?
事實上,新增、刪除、修改都可以用@CacheEvict ,不建議使用 @ CachePut ,用法完全如上查的方法,只是注解名字不一樣。
?
// 查:存key為cache_department 的數據緩存到departmentList中,如果沒有指定key則方法參數作為key保存到緩存中。department只是緩存的名字。
//不指定 key 會默認使用參數名或者方法名,作為緩存的key。
?
?
?
5. 測試
第一次訪問是沒有緩存的,執行sql從數據庫查,執行了查詢方法,輸出寫在方法中的輸出語句。
?
?
?
第二次訪問,已有緩存,不進入方法,直接從緩存得數據并作為方法的返回值,不運行sql。如下:
?
?
?