springboot 缓存ehcache的简单使用
前些天發(fā)現(xiàn)了一個(gè)巨牛的人工智能學(xué)習(xí)網(wǎng)站,通俗易懂,風(fēng)趣幽默,忍不住分享一下給大家。點(diǎn)擊跳轉(zhuǎn)到教程。
步驟:
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. 新增一個(gè)配置文件 ehcache.xml,放在resource 下面,springboot會(huì)自動(dòng)掃描 :
?
<?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. 在方法中運(yùn)用注解,實(shí)現(xiàn)緩存的 增、刪、改、查
?
只要在方法上加上對(duì)應(yīng)注解就可以了。
@Cacheable 查:?如果有就直接緩存中取 沒(méi)有就數(shù)據(jù)庫(kù)查并放入緩存。加上這個(gè)注解,調(diào)用這個(gè)方法就可以取到緩存中的值。
@CacheEvict?新增、刪除、修改 :會(huì)自動(dòng)清除緩存中內(nèi)容。加上這注解,對(duì)數(shù)據(jù)庫(kù)的update、add、delete操作都會(huì)清除對(duì)應(yīng)緩存。
?
如:緩存名為“ department ”,當(dāng)調(diào)用此方法時(shí)會(huì)先判斷是否有緩存。有則不進(jìn)入方法,直接返回緩存中的值。無(wú)緩存名為 “department” 的緩存才會(huì)進(jìn)入方法內(nèi)部,執(zhí)行數(shù)據(jù)庫(kù)查詢。
@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);}?
可以給緩存中的具體項(xiàng)起個(gè)鍵值:key
@Override@Cacheable(value = "department", key = "#departmentId")public Object findOne(String departmentId) {return departmentRepository.findOne(departmentId);}?
當(dāng)緩存key沒(méi)有全部命中時(shí),要確保緩存全部清除,就要加上
allEntries = true ,默認(rèn)為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個(gè)注解來(lái)聲明緩存規(guī)則(又是使用注解式的AOP的一個(gè)生動(dòng)例子),如表。
?
事實(shí)上,新增、刪除、修改都可以用@CacheEvict ,不建議使用 @ CachePut ,用法完全如上查的方法,只是注解名字不一樣。
?
// 查:存key為cache_department 的數(shù)據(jù)緩存到departmentList中,如果沒(méi)有指定key則方法參數(shù)作為key保存到緩存中。department只是緩存的名字。 //不指定 key 會(huì)默認(rèn)使用參數(shù)名或者方法名,作為緩存的key。?
?
?
5. 測(cè)試
第一次訪問(wèn)是沒(méi)有緩存的,執(zhí)行sql從數(shù)據(jù)庫(kù)查,執(zhí)行了查詢方法,輸出寫在方法中的輸出語(yǔ)句。
?
?
?
第二次訪問(wèn),已有緩存,不進(jìn)入方法,直接從緩存得數(shù)據(jù)并作為方法的返回值,不運(yùn)行sql。如下:
?
?
?
總結(jié)
以上是生活随笔為你收集整理的springboot 缓存ehcache的简单使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 寻找 ASP.NET 2.0 老师
- 下一篇: 设置 git pull 无需输入账号和密