在這裏springMVC+mybatis如何集成我暫時不多說,之後有時間會更新。
集成ehcache只要分爲四步:
第一步: 編寫ehcache配置文件ehcache.xml 當然名字自己可以隨便起,只要保證在後面引用配置文件的時候能夠書寫正確就OK。
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"
monitoring="autodetect" dynamicConfig="true">
<diskStore path="java.io.tmpdir"/>
<!-- <diskStore path="java.io.tmpdir" /> -->
<defaultCache maxElementsInMemory="10000" eternal="false"
timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"
diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000"
diskPersistent="false" diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU" />
<cache name="baseCache" maxElementsInMemory="10000" eternal="false"
timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"
diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000"
diskPersistent="false" diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LFU" />
</ehcache>
<!-- 1.必須要有的屬性: name: cache的名字,用來識別不同的cache,必須惟一。 maxElementsInMemory: 內存管理的緩存元素數量最大限值。
maxElementsOnDisk: 硬盤管理的緩存元素數量最大限值。默認值爲0,就是沒有限制。 eternal: 設定元素是否持久話。若設爲true,則緩存元素不會過期。
overflowToDisk: 設定是否在內存填滿的時候把數據轉到磁盤上。 2.下面是一些可選屬性: timeToIdleSeconds: 設定元素在過期前空閒狀態的時間,只對非持久性緩存對象有效。默認值爲0,值爲0意味着元素可以閒置至無限長時間。
timeToLiveSeconds: 設定元素從創建到過期的時間。其他與timeToIdleSeconds類似。 diskPersistent:
設定在虛擬機重啓時是否進行磁盤存儲,默認爲false.(我的直覺,對於安全小型應用,宜設爲true)。 diskExpiryThreadIntervalSeconds:
訪問磁盤線程活動時間。 diskSpoolBufferSizeMB: 存入磁盤時的緩衝區大小,默認30MB,每個緩存都有自己的緩衝區。 memoryStoreEvictionPolicy:
元素逐出緩存規則。共有三種,Recently Used (LRU)最近最少使用,爲默認。 First In First Out (FIFO),先進先出。Less
Frequently Used(specified as LFU)最少使用 -->
第二步:編寫ehcache整合spring配置文件 applicationContext-ehcache.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:aop="http://www.springframework.org/schema/aop" xmlns:c="http://www.springframework.org/schema/c"
xmlns:cache="http://www.springframework.org/schema/cache" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:lang="http://www.springframework.org/schema/lang" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<!-- 開啓spring緩存 -->
<cache:annotation-driven cache-manager="cacheManager"/>
<bean id="ehcacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml"/>
</bean>
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="ehcacheManager"/>
<property name="transactionAware" value="true"/>
</bean>
</beans>
第三步 將配置文件引入mvc的配置文件中 mvc-servlet.xml 因個人項目而定名字只是在我代碼中是這樣命名的<import resource="applicationContext-ehcache.xml" />
此時文件都在同一級如果配置文件在項目管理上存在分包分文件夾的情況要特別注意
第四步 使用註解的方式在dao層或者service的實現層中對應的添加 @Cacheable(value="baseCache",key="'Login'")
示例代碼:
import javax.annotation.Resource;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import com.cy.ssm.mapper.UserMapper;
import com.cy.ssm.beans.UserBean;
import com.cy.ssm.service.ILoginService;
@Service
public class LoginServiceImpl implements ILoginService {
// private static Logger logger = LoggerFactory
// .getLogger(LoginServiceImpl.class);
@Resource
private UserMapper um;
@Override
@Cacheable(value="baseCache",key="'Login'")
public UserBean Login(String username, String password) {
return um.login(username, password);
}
}
注:ehcache註解用法
//將查詢到的數據緩存到myCache中,並使用方法名稱加上參數中的userNo作爲緩存的關鍵
//通常更新操作只需刷新緩存中的某個值,所以爲了準確的清除特定的緩存,故定義了這個唯一的密鑰,從而不會影響其它緩存值
@Cacheable (value = "myCache" ,key = "'get'+#userNo" )
@CacheEvict (value ="myCache" ,key ="'get'+#userNo") 更新操作根絕用戶編號更新緩存中的數據
@CacheEvict (value = “myCache” ,allEntries = true ) 清除緩存中所有數據