在項目中,我們經常會用到緩存,合理的利用緩存可以提高代碼工作的效率,減少不必要的重複工作。這篇文章主要是介紹Ehcache在Java項目中的簡單上手。以下爲步驟:
1. 引入jar包:
① 可以通過maven引入,傳送門:http://mvnrepository.com/artifact/org.ehcache/ehcache
② 也可以直接下載jar包,放到項目中使用,傳送門:https://github.com/ehcache/ehcache3/releases
2. 配置Ehcache(下面圖片是目錄結構,可參考)
① 添加ehcache-setting.xml
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<!-- 指定一個文件目錄,當EhCache把數據寫到硬盤上時,將把數據寫到這個文件目錄下 -->
<diskStore path="${java.io.tmpdir}/ehcache/sq" />
<!-- 設定緩存的默認數據過期策略 -->
<defaultCache maxElementsInMemory="10000" eternal="false"
overflowToDisk="true" timeToIdleSeconds="10" timeToLiveSeconds="20"
diskPersistent="false" diskExpiryThreadIntervalSeconds="120" />
<!-- 配置說明: timeToIdleSeconds:如果客戶的訪問授權15分鐘內沒有被使用過,則從緩存中清空。 -->
<cache name="test_cache" maxElementsInMemory="1000" eternal="false"
timeToIdleSeconds="900" overflowToDisk="true" />
</ehcache>
② 添加cache-context.xml<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.2.xsd">
<!-- 自動掃描註解的bean -->
<cache:annotation-driven cache-manager="cacheManager" />
<bean id="ehcacheManagerFactory"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="/WEB-INF/resource/ehcache-setting.xml"></property>
</bean>
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="ehcacheManagerFactory"></property>
</bean>
</beans>
③ context.xml中引入cache-context.xml<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop//spring-aop-4.3.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"
xmlns:tx="http://www.springframework.org/schema/tx">
<import resource="resource/app-context.xml" />
<!-- <import resource="resource/spring-security.xml" /> -->
<import resource="resource/db-context.xml" />
<import resource="resource/bean-context.xml" />
<import resource="resource/cache-context.xml" />
</beans>
3. 使用Ehcache:
Ehcache的使用方式可以通過手動將值put進cache,也可以通過註解的方式自動put緩存內容。下面介紹一個簡單的應用場景。
在ProjectService.java中,我們有個方法,需要獲取項目的詳細信息,getProjectDetailById(Integer projectId),這時候如果很多人查詢項目,我們不想頻繁操作數據庫,就可以將第一次查詢的結果,放在cache中,後面調用的人不需要再執行一次查詢,而是從cache中直接取結果。代碼如下:
@Cacheable(value="test_cache", key="#projectId")
public Project getProjectDetailById(Integer projectId){
Project project = null;
project = em.find(Project.class, projectId);
return project;
}
方法內是hibernate的查詢,不需要管。這裏使用的是Cacheable註解,test_cache即我們在ehcache-setting.xml中定義的一個緩存。這裏就是將projectId爲key,方法調用之後return的結果爲value,存到緩存中。下一次,在緩存未過期時,用相同的參數再次調用此方法,緩存就派上用場了。@Cacheable註解還有其他的用法,有空再補充吧。