1. 程式人生 > >Spring MVC 4.+ 使用 Ehcache 超簡單配置!!!

Spring MVC 4.+ 使用 Ehcache 超簡單配置!!!

First

pom.xml

<dependency>
    <groupId>net.sf.ehcache</groupId>
    <artifactId>ehcache-core</artifactId>
    <version>2.6.11</version>
</dependency>


<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId
>
<version>4.2.3.RELEASE</version> </dependency>

Second

create new file cache-config.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd ">
<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>

Third

create new file 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"
         updateCheck="false">

    <diskStore path="java.io.tmpdir"/>

    <defaultCache eternal="false"
                  maxEntriesLocalHeap="1000"
                  overflowToDisk="false"
                  diskPersistent="false"
                  timeToIdleSeconds="3600"
                  timeToLiveSeconds="3600"/>

    <cache name="baseCache"
           eternal="false"
           maxEntriesLocalHeap="200"
           overflowToDisk="false"
           diskPersistent="false"
           timeToIdleSeconds="600"
           statistics="true"
           timeToLiveSeconds="600"/>

    <!--
        eternal="false"   // 元素是否永恆,如果是就永不過期(必須設定)
        maxElementsInMemory="1000" // 快取容量的記憶體最大值(必須設定)
        overflowToDisk="false"  // 當快取達到maxElementsInMemory值是,是否允許溢位到磁碟(必須設定)
        diskPersistent="false"  // 磁碟快取在VM重新啟動時是否保持(預設為false)
        timeToIdleSeconds="0" // 導致元素過期的訪問間隔(秒為單位). 0表示可以永遠空閒,預設為0
        timeToLiveSeconds="600" // 元素在快取裡存在的時間(秒為單位). 0 表示永遠存在不過期
        memoryStoreEvictionPolicy="LFU" // 當達到maxElementsInMemory時,如何強制進行驅逐預設使用"最近使用(LRU)"策略,其它還有先入先出FIFO,最少使用LFU,較少使用LRU
    -->
</ehcache>

Forth

add cache-config.xml to main spring.xml

<import resource="cache-config.xml"/>

or
web.xml

<context-param>
 <param-name>contextConfigLocation</param-name>
  <param-value>classpath*:applicationContext.xml,classpath:cache-config.xml</param-value>
</context-param>

Finally

import org.springframework.cache.annotation.Cacheable;

import java.util.List;

public class UserDao extends BaseDao {

    //這裡
    @Cacheable(value = "baseCache")
    public UserTable getUserByDeviceId(String deviceId) {
        if (deviceId != null) {
            List<UserTable> list = (List<UserTable>) getHibernateTemplate()
                    .find("From UserTable u where u.deviceId='" + deviceId
                            + "'");
            if (list != null && list.size() > 0) {
                return list.get(0);
            }
        }
        return null;
    }

}

這裡寫圖片描述