SpringMVC4集成ehcache


前言

     使用SpringMVC4集成ehcache来缓存服务器数据。

开发环境

    SpringMVC4、ehcache2.6、

项目结构

SpringMVC 集成ehcache

1、pom.xml

//除了SpringMVC 、sql server相关就是ehcache-core 

      net.sf.ehcache
      ehcache-core
      2.6.11
    

2、springmvc-servlet.xml

     主要就是开启cache注解,同时导入spring cache命名空间。

<?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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       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/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd
       http://www.springframework.org/schema/cache
       http://www.springframework.org/schema/cache/spring-cache.xsd">

    
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations" value="classpath:config/jdbc.properties"/>
        <property name="fileEncoding" value="UTF-8"/>
    bean>

    
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${sqlserver.driver}"/>
        <property name="url" value="${sqlserver.url}"/>
        <property name="username" value="${sqlserver.username}"/>
        <property name="password" value="${sqlserver.password}"/>
    bean>


    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.autohome.dao"/>
    bean>

    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="configLocation" value="classpath:springmvc-mybatis.xml"/>
        <property name="dataSource" ref="dataSource"/>
    bean>

    
    <cache:annotation-driven cache-manager="cacheManager"/>
    <bean id="cacheManagerFactory" 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="cacheManagerFactory"/>
    bean>

    
    <mvc:annotation-driven/>

    
    <mvc:default-servlet-handler/>

    
    <context:component-scan base-package="com.autohome.controller" />
    
    <context:component-scan base-package="com.autohome.service"/>

    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    bean>

beans>

3、ehcache.xml

    xsd我配置以后一直报红,不知道怎么回事,后来试着运行了下发现不影响程序运行,索性就这么用着

<?xml version="1.0" encoding="gbk"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.0.xsd">

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

      <defaultCache
        maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="120"
        timeToLiveSeconds="120"
        overflowToDisk="false"
    />

    <cache name="myCache"
           eternal="false"
           maxElementsInMemory="10000"
           overflowToDisk="false"
           timeToIdleSeconds="0"
           timeToLiveSeconds="0"
           memoryStoreEvictionPolicy="LFU"/>

ehcache>

3、Service层里调用Dao层时使用缓存注解

      在listAllUser方法上使用Cacheable注解,第一次运行时缓存里还没数据控制台会打印query from database...再次调用则直接读取缓存数据

@Cacheable(value = "myCache")
    public List listAllUser() {
        System.out.println("query from database...");
        return userDao.listAllUser();
    }

4、Controller里调用

@Controller
@RequestMapping("/User")
public class UserController {

    @Autowired
    UserService userService;

    @RequestMapping("/index")
    public ModelAndView index(){
        System.out.println("size:"+userService.listAllUser().size());

        ModelAndView mav =new ModelAndView("index");

        return mav;
    }
}