Ehcache缓存入门实战(附源码)

原文地址:点击打开链接

参看文章:点击打开链接

Ehcache缓存入门实战(附源码)

Ehcache是一个强大、成熟的Java缓存框架,可以非常容易与其他库和框架集成。官网声称Ehcache可以作为TB级别的数据缓存,并且具有高扩展性。目前Ehcache的最新版本是3.1。

本文不会深究缓存背后的原理,只是作为一个使用Ehcache的例子,由于最近需要做一个类似二级缓存的功能,特地学习了下Ehcache。发现Ehcache作为一个本地缓存框架使用还是非常合适的,但是如果扩展到分布式缓存就需要考虑使用Redis这样的分布式缓存框架了。

为了快速入门,这里使用配置文件的方式整合Ehcache。

创建Maven工程,添加pom.xml依赖如下:

<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.rhwayfungroupId>
    <artifactId>ehcache-demoartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>ehcache-demoname>
    <description>ehcache-demodescription>

    <properties>
        
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        
        <junit.version>4.12junit.version>
        
        <spring.version>3.2.8.RELEASEspring.version>
    properties>

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

        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-coreartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
            <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-aspectsartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-txartifactId>
            <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-testartifactId>
            <version>${spring.version}version>
            <scope>testscope>
        dependency>

        
        <dependency>
            <groupId>net.sf.ehcachegroupId>
            <artifactId>ehcacheartifactId>
            <version>2.8.2version>
        dependency>
    dependencies>
project>

添加Spring配置:application.xml


<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"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="  
           http://www.springframework.org/schema/beans  
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
           http://www.springframework.org/schema/aop  
           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
           http://www.springframework.org/schema/context  
           http://www.springframework.org/schema/context/spring-context-3.0.xsd
           http://www.springframework.org/schema/cache 
           http://www.springframework.org/schema/cache/spring-cache-3.1.xsd">

    
    <context:component-scan base-package="com.rhwayfun.ehcache.service.impl" />

    <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>
beans>

还需要Ehcache的配置文件:ehcache.xml


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

    
    
    <defaultCache
            maxElementsInMemory="10000" 
            eternal="false" 
            overflowToDisk="true"
            timeToIdleSeconds="10"
            timeToLiveSeconds="20"
            diskPersistent="false"
            diskExpiryThreadIntervalSeconds="120"/>

    <cache name="cacheTest"
        maxElementsInMemory="1000"
        eternal="false"
        overflowToDisk="true"
        timeToIdleSeconds="10"
        timeToLiveSeconds="20"/>

ehcache>

在代码中已经对具体的参数含义做了说明,这里不再赘述。这里要注意的是name属性,在后面的测试代码需要使用到它。

编写测试接口:

package com.rhwayfun.ehcache.service;

/**
 * 
 * @ClassName: EhcacheService 
 * @Description: TODO
 * @author ZhongCB
 * @date 2016年8月11日 上午11:53:52 
 *
 */
public interface EhcacheService {

    String getTime(String param);
}

接口实现类:

package com.rhwayfun.ehcache.service.impl;

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

import com.rhwayfun.ehcache.service.EhcacheService;

@Service
public class EhcacheServiceImpl implements EhcacheService {

    @Cacheable(value = "cacheTest", key="#param")
    public String getTime(String param) {
        return String.valueOf(System.currentTimeMillis());
    }

}

注意@Cacheable注解的value属性的值就是ehcache.xml配置文件中的name属性的值。key表示方法的参数param代表的就是缓存的key,通过这个key可以获取到value。

最后编写测试代码:

package com.rhwayfun.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.rhwayfun.ehcache.service.EhcacheService;

@ContextConfiguration(locations = {"classpath:application.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class EhcacheTest extends AbstractJUnit4SpringContextTests{

    @Autowired
    private EhcacheService ehcacheService;

    @Test
    public void testEhcache() throws InterruptedException{
        System.out.println("第一次调用:" + ehcacheService.getTime("param"));
        Thread.sleep(2000);
        System.out.println("第二次调用(2秒后):" + ehcacheService.getTime("param"));
        Thread.sleep(10000);
        System.out.println("第三次调用(10秒后):" + ehcacheService.getTime("param"));
    }
}

运行该测试类,结果如下:

Ehcache缓存入门实战(附源码)_第1张图片

至此,Ehcache的入门实战就结束了,如果想要更详细了解Ehcache的资料,请访问Ehcache官网。


你可能感兴趣的:(ehcache)