Spring 整合EhCache一注解方式

    • 前言
    • maven 配置
    • ehcachexml
    • 实体类
    • service接口
    • service实现类
    • 配置
    • 测试
    • 测试结果分析

前言

本文spring4通过注解方式 整合EhCache。同时提供xml注解方式和java配置的方式整合的源码。
主要了解对数据缓存的增加、缓存的更新和缓存的删除。
阅读之前需要先了解spring cache相关注解的介绍。请移步http://blog.csdn.net/poorCoder_/article/details/55258253

maven 配置

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0modelVersion>
  <groupId>com.testgroupId>
  <artifactId>ehcahe2artifactId>
  <version>0.0.1-SNAPSHOTversion>
  <packaging>jarpackaging>
  <build/>

  <properties>  
          
        <spring.version>4.3.5.RELEASEspring.version>  
        <junit.version>4.12junit.version>
  properties> 
    <dependencies>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-coreartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-contextartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-context-supportartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-aopartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>  
            <groupId>org.springframeworkgroupId>  
            <artifactId>spring-jdbcartifactId>  
            <version>${spring.version}version>  
        dependency>
        <dependency>  
            <groupId>org.springframeworkgroupId>  
            <artifactId>spring-webartifactId>  
            <version>${spring.version}version>  
        dependency>
        <dependency>  
            <groupId>org.springframeworkgroupId>  
            <artifactId>spring-webmvcartifactId>  
            <version>${spring.version}version>  
        dependency> 
         <dependency>  
            <groupId>org.springframeworkgroupId>  
            <artifactId>spring-txartifactId>  
            <version>${spring.version}version>  
        dependency>  


        
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>${junit.version}version>
            <scope>testscope>
        dependency>

        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-testartifactId>
            <version>${spring.version}version>
            <scope>testscope>
        dependency>

        
        <dependency>
            <groupId>net.sf.ehcachegroupId>
            <artifactId>ehcacheartifactId>
            <version>2.7.5version>
        dependency>

           
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.2.8version>
        dependency> 
          
        <dependency>  
            <groupId>org.mybatisgroupId>  
            <artifactId>mybatis-springartifactId>  
            <version>1.2.2version>  
        dependency>  

          
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.27version>
            <type>jartype>
            <scope>compilescope>
        dependency>

          
        <dependency>  
            <groupId>commons-dbcpgroupId>  
            <artifactId>commons-dbcpartifactId>  
            <version>1.2.2version>  
        dependency>

            

         
            <dependency>
                <groupId>org.slf4jgroupId>
                <artifactId>slf4j-apiartifactId>
                <version>1.7.18version>
            dependency>
            

            
            <dependency>
                <groupId>org.slf4jgroupId>
                <artifactId>slf4j-log4j12artifactId>
                <version>1.7.18version>
            dependency>
            

            
            <dependency>
                <groupId>org.apache.logging.log4jgroupId>
                <artifactId>log4j-coreartifactId>
                <version>2.5version>
            dependency>
            

    dependencies> 

project>

ehcache.xml


<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="ehcache.xsd"
         updateCheck="false" monitoring="autodetect"
         dynamicConfig="true">
         
   <diskStore path="java.io.tmpdir"/>    
    <defaultCache 
        maxElementsInMemory="10000" 
        eternal="true"  
        timeToIdleSeconds="120" 
        timeToLiveSeconds="120" 
        overflowToDisk="true"  
        maxElementsOnDisk="10000000" 
        diskPersistent="false"  
        diskExpiryThreadIntervalSeconds="120" 
        memoryStoreEvictionPolicy="LRU" />

   <cache name="myCache" 
        maxElementsInMemory="10000"  
        maxElementsOnDisk="1000" 
        eternal="false" 
        overflowToDisk="true"  
        diskSpoolBufferSizeMB="20" 
        timeToIdleSeconds="300" 
        timeToLiveSeconds="600"  
        memoryStoreEvictionPolicy="LFU" />  
ehcache>

实体类

实体类必须要序列化

public class User implements Serializable {

    private static final long serialVersionUID = 5272436524920090884L;
    private Integer id;
    private String name;
    //setter()  getter()...
    }

service接口:

package com.test.service;

import com.test.model.User;

public interface UserService {
    public User findUserById(int id);
    public void addUser(User user);
    public User updateUser(User user);
    public int countUser();

    public String getTimestamp(String param);
}

service实现类:

package com.test.service.impl;

import javax.annotation.Resource;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.test.dao.UserDao;
import com.test.model.User;
import com.test.service.UserService;

@Service("userService")
public class UserServiceImpl implements UserService{
    @Resource
    private UserDao userDao;

    /**
     * @Cacheable 将结果加入缓存。下次调用时直接从缓存中读取
     * cacheNames指定缓存,要与ehcache.xml中的cache明显相同
     */
    @Override
    @Cacheable(cacheNames="myCache",key="#id")
    public User findUserById(int id) {
        return userDao.findById(id);
    }

    /**
     * @CacheEvict 删除缓存
     * beforeInvocation=true 表示在方法执行之前删除缓存
     * 默认的 key=0 与下面 countUser()方法中key 相同
     */
    @Override
    @CacheEvict(cacheNames="myCache",key="0",beforeInvocation=true)
    public void addUser(User user) {
        userDao.add(user);
    }

    /**
     * @CachePut更新缓存
     * 将结果根据key 更新缓存中的内容
     */
    @Override
    @CachePut(cacheNames="myCache",key="#user.id")
    public User updateUser(User user) {
        userDao.update(user);
        return userDao.findById(user.getId());
    }

    @Override
    @Cacheable(cacheNames="myCache",key="0")
    public int countUser() {
        return userDao.count();
    }

    @Cacheable(cacheNames="myCache",key="#param")
    @Override
    public String getTimestamp(String param) {
        Long timestamp = System.currentTimeMillis();
        return timestamp.toString();
    }

}

配置

xml 配置形式


    <cache:annotation-driven cache-manager="cacheManager"/>  
     
    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">  
        <property name="cacheManager" ref="ehcache">property>  
    bean>  

    <bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">  
        
        <property name="configLocation" value="classpath:ehcache.xml">property>
    bean>  

java类配置形式:

package com.test.config;

import org.apache.log4j.Logger;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;

@Configuration  
@EnableCaching// "cacheManager" />
public class CachingConfig {

     private static final Logger logger = Logger.getLogger(CachingConfig.class);  

    @Bean  
    public EhCacheManagerFactoryBean ehCacheManagerFactoryBean() {  
        EhCacheManagerFactoryBean ehCacheManagerFactoryBean = new EhCacheManagerFactoryBean();  
        ehCacheManagerFactoryBean.setConfigLocation(new ClassPathResource(  
                "ehcache.xml"));  
        return ehCacheManagerFactoryBean;  
    }  

    @Bean  
    public CacheManager cacheManager() {  
        logger.info("EhCacheCacheManager"); 
        EhCacheCacheManager cacheManager = new EhCacheCacheManager();  
        cacheManager.setCacheManager(ehCacheManagerFactoryBean().getObject());  
        return cacheManager;  
    }  
}

测试

基类:

package com.test.baseTest;

import org.junit.runner.RunWith;  
import org.springframework.test.context.ContextConfiguration;  
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;  
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;  

import com.test.config.AppConfig;
import com.test.config.DatabaseConfig;
//XML配置
//@ContextConfiguration(locations = { "classpath:applicationContext.xml" })  
//java类配置
@ContextConfiguration(classes = {AppConfig.class})
@RunWith(SpringJUnit4ClassRunner.class)  
public class SpringTestCase extends AbstractJUnit4SpringContextTests {

}

测试类:

package com.test.testService;

import javax.annotation.Resource;

import org.junit.Test;

import com.test.baseTest.SpringTestCase;
import com.test.model.User;
import com.test.service.UserService;

public class TestService extends SpringTestCase {

    @Resource
    private UserService userService;

    @Test
    public void addUser(){
        User user = new User();
        user.setName("李四");
        userService.addUser(user);
    }

    @Test
    public void testQuery() throws InterruptedException{
        System.out.println("第一次调用");
        System.out.println( userService.findUserById(1));
        System.out.println("第二次调用");
        System.out.println(userService.findUserById(1));

    }
    @Test
    public void testUpdate(){
        User user = userService.findUserById(1);
        System.out.println( "第一次调用"+user);
        user.setName(user.getName()+"1");
        userService.updateUser(user);
        System.out.println("第二次调用"+userService.findUserById(1));
    }

    @Test
    public void testCount(){
        System.out.println("第一次查询数量:"+userService.countUser());
        addUser();
        System.out.println("第二次查询数量:"+userService.countUser());
    }

    @Test  
    public void getTimestampTest() throws InterruptedException{  
        System.out.println("第一次调用:" + userService.getTimestamp("param"));
        Thread.sleep(2000);
        System.out.println("2秒之后调用:" + userService.getTimestamp("param"));
    } 

}

测试结果分析

调用testQuery()方法时结果为:
Spring 整合EhCache一注解方式_第1张图片
从结果来看:第一次调用时访问了数据库 并且将数据放入了缓存。第二次调用时,没有访问数据库就直接返回了,缓存生效。

调用testCount()方法返回结果为:

Spring 整合EhCache一注解方式_第2张图片
从结果中可以看出,第一次查询数量访问了数据库。并且将数据放入缓存。但是执行了addUser()之后.缓存中的数据又被清除了。

调用testUpdate()返会结果:
第一次调用返回信息,并放入缓存。中间调用更新用户信息后,将新的返回信息更到缓存中。第二次调用时,没有访问数据库,但返回的数据是新的。

JDBC Connection [jdbc:mysql://localhost:3306/mytest?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&autoReconnect=true&failOverReadOnly=false, UserName=root@localhost, MySQL Connector Java] will not be managed by Spring
[DEBUG] 2017-02-16 15:42:01,118 method:org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:139)
==>  Preparing: SELECT id,name FROM t_user WHERE id = ? 
[DEBUG] 2017-02-16 15:42:01,137 method:org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:139)
==> Parameters: 1(Integer)
[DEBUG] 2017-02-16 15:42:01,150 method:org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:139)
<==      Total: 1
[DEBUG] 2017-02-16 15:42:01,151 method:org.mybatis.spring.SqlSessionUtils.closeSqlSession(SqlSessionUtils.java:168)
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7463e563]
[DEBUG] 2017-02-16 15:42:01,151 method:org.springframework.jdbc.datasource.DataSourceUtils.doReleaseConnection(DataSourceUtils.java:327)
Returning JDBC Connection to DataSource
[DEBUG] 2017-02-16 15:42:01,152 method:net.sf.ehcache.store.disk.Segment.put(Segment.java:432)
put added 0 on heap
第一次调用User [id=1, name=张三]
[DEBUG] 2017-02-16 15:42:01,156 method:org.springframework.cache.interceptor.AbstractFallbackCacheOperationSource.getCacheOperations(AbstractFallbackCacheOperationSource.java:97)
Adding cacheable method 'updateUser' with attribute: [Builder[public com.test.model.User com.test.service.impl.UserServiceImpl.updateUser(com.test.model.User)] caches=[myCache] | key='#user.id' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='' | unless='']
[DEBUG] 2017-02-16 15:42:01,156 method:org.mybatis.spring.SqlSessionUtils.getSqlSession(SqlSessionUtils.java:104)
Creating a new SqlSession
[DEBUG] 2017-02-16 15:42:01,156 method:org.mybatis.spring.SqlSessionUtils.getSqlSession(SqlSessionUtils.java:140)
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1fd4340c] was not registered for synchronization because synchronization is not active
[DEBUG] 2017-02-16 15:42:01,156 method:org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:110)
Fetching JDBC Connection from DataSource
[DEBUG] 2017-02-16 15:42:01,157 method:net.sf.ehcache.store.disk.Segment.faultInternal(Segment.java:762)
fault removed 0 from heap
[DEBUG] 2017-02-16 15:42:01,157 method:org.mybatis.spring.transaction.SpringManagedTransaction.openConnection(SpringManagedTransaction.java:86)
JDBC Connection [jdbc:mysql://localhost:3306/mytest?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&autoReconnect=true&failOverReadOnly=false, UserName=root@localhost, MySQL Connector Java] will not be managed by Spring
[DEBUG] 2017-02-16 15:42:01,157 method:net.sf.ehcache.store.disk.Segment.faultInternal(Segment.java:774)
fault added 0 on disk
[DEBUG] 2017-02-16 15:42:01,157 method:org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:139)
==>  Preparing: UPDATE t_user SET name = ? WHERE id = ? 
[DEBUG] 2017-02-16 15:42:01,158 method:org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:139)
==> Parameters: 张三1(String), 1(Integer)
[DEBUG] 2017-02-16 15:42:01,186 method:org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:139)
<==    Updates: 1
[DEBUG] 2017-02-16 15:42:01,186 method:org.mybatis.spring.SqlSessionUtils.closeSqlSession(SqlSessionUtils.java:168)
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1fd4340c]
[DEBUG] 2017-02-16 15:42:01,187 method:org.springframework.jdbc.datasource.DataSourceUtils.doReleaseConnection(DataSourceUtils.java:327)
Returning JDBC Connection to DataSource
[DEBUG] 2017-02-16 15:42:01,187 method:org.mybatis.spring.SqlSessionUtils.getSqlSession(SqlSessionUtils.java:104)
Creating a new SqlSession
[DEBUG] 2017-02-16 15:42:01,187 method:org.mybatis.spring.SqlSessionUtils.getSqlSession(SqlSessionUtils.java:140)
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@215b011c] was not registered for synchronization because synchronization is not active
[DEBUG] 2017-02-16 15:42:01,187 method:org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:110)
Fetching JDBC Connection from DataSource
[DEBUG] 2017-02-16 15:42:01,188 method:org.mybatis.spring.transaction.SpringManagedTransaction.openConnection(SpringManagedTransaction.java:86)
JDBC Connection [jdbc:mysql://localhost:3306/mytest?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&autoReconnect=true&failOverReadOnly=false, UserName=root@localhost, MySQL Connector Java] will not be managed by Spring
[DEBUG] 2017-02-16 15:42:01,188 method:org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:139)
==>  Preparing: SELECT id,name FROM t_user WHERE id = ? 
[DEBUG] 2017-02-16 15:42:01,188 method:org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:139)
==> Parameters: 1(Integer)
[DEBUG] 2017-02-16 15:42:01,189 method:org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:139)
<==      Total: 1
[DEBUG] 2017-02-16 15:42:01,189 method:org.mybatis.spring.SqlSessionUtils.closeSqlSession(SqlSessionUtils.java:168)
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@215b011c]
[DEBUG] 2017-02-16 15:42:01,189 method:org.springframework.jdbc.datasource.DataSourceUtils.doReleaseConnection(DataSourceUtils.java:327)
Returning JDBC Connection to DataSource
[DEBUG] 2017-02-16 15:42:01,193 method:net.sf.ehcache.store.disk.Segment.put(Segment.java:432)
put added 0 on heap
[DEBUG] 2017-02-16 15:42:01,195 method:net.sf.ehcache.store.disk.Segment.put(Segment.java:460)
put updated, deleted 0 on heap
[DEBUG] 2017-02-16 15:42:01,195 method:net.sf.ehcache.store.disk.Segment.put(Segment.java:464)
put updated, deleted 0 on disk
[DEBUG] 2017-02-16 15:42:01,195 method:net.sf.ehcache.store.disk.Segment.faultInternal(Segment.java:762)
fault removed 0 from heap
[DEBUG] 2017-02-16 15:42:01,195 method:net.sf.ehcache.store.disk.Segment.faultInternal(Segment.java:774)
fault added 0 on disk
第二次调用User [id=1, name=张三1]

最后附上源码例子:
http://download.csdn.net/detail/poorcoder_/9755967

你可能感兴趣的:(ehcache)