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;
    }

}

这里写图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值