ehcache使用_Spring 整合EhCache一 初体验

ehcache简介

EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider。(百度百科) 
主要的特性有: 
1. 快速 
2. 简单 
3. 多种缓存策略 
4. 缓存数据有两级:内存和磁盘,因此无需担心容量问题 
5. 缓存数据会在虚拟机重启的过程中写入磁盘 
6. 可以通过RMI、可插入API等方式进行分布式缓存 
7. 具有缓存和缓存管理器的侦听接口 
8. 支持多缓存管理器实例,以及一个实例的多个缓存区域 
9. 提供Hibernate的缓存实现

配置文件的介绍

ehcache.xml

<?xml version="1.0" encoding="UTF-8"?><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>

本文主要讲解spring整合EhCache的一个简单例子。

spring整合

1.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>ehcahe1artifactId>
  <version>0.0.1-SNAPSHOTversion>
  <packaging>warpackaging>

  <properties>  
          
        <spring.version>4.0.2.RELEASEspring.version>  
        <junit.version>4.10junit.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>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>
    dependencies> 
  <build/>project>

maven配置中,3.0版本之后的maven配置使用以下配置,之前的使用上面的配置。

<dependency>
  <groupId>org.ehcachegroupId>
  <artifactId>ehcacheartifactId>
  <version>3.2.0version>dependency>

2.spring 配置

    
    <context:component-scan base-package="com.test.service" />
    
    <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>  

3.service 接口及实现

接口:

package com.test.service;public interface EhCacheTestService {     public String getTimestamp(String param);
}

实现类:

package com.test.service.impl;import org.springframework.cache.annotation.Cacheable;import org.springframework.stereotype.Service;import com.test.service.EhCacheTestService;@Servicepublic class EhCacheTestServiceImpl implements EhCacheTestService {

    /**
     * @Cacheable 支持如下几个参数:
     *  value:缓存位置名称,不能为空,如果使用EHCache,就是ehcache.xml中声明的cache的name
     *  key:缓存的key,默认为空,既表示使用方法的参数类型及参数值作为key,支持SpEL
     *  condition:触发条件,只有满足条件的情况才会加入缓存,默认为空,既表示全部都加入缓存,支持SpEL
     */
    @Cacheable(value="cacheTest",key="#param")    @Override
    public String getTimestamp(String param) {
        Long timestamp = System.currentTimeMillis();        return timestamp.toString();
    }

}

4.测试 
测试基类:

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;  

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

功能测试类:

package com.test.service;import org.junit.Test;import org.springframework.beans.factory.annotation.Autowired;import com.test.baseTest.SpringTestCase;public class EhCacheTestServiceTest extends SpringTestCase {

    @Autowired  
    private EhCacheTestService ehCacheTestService;    @Test  
    public void getTimestampTest() throws InterruptedException{  
        System.out.println("第一次调用:" + ehCacheTestService.getTimestamp("param"));
        Thread.sleep(2000);
        System.out.println("2秒之后调用:" + ehCacheTestService.getTimestamp("param"));
        Thread.sleep(11000);
        System.out.println("再过11秒之后调用:" + ehCacheTestService.getTimestamp("param"));
    } 
}

运行: 27da0550a910d7101942fc98e5907794.png

附代码地址:https://download.csdn.net/detail/poorcoder_/9751430

Java程序员升职加薪必备技术——分布式

nacos gateway动态路由

史上最简单的 Spring 源码导读

史上最全的企业级定时任务框架 Quartz 介绍

利用 Spring 自动类型转换与回调模式写出优雅的代码

a0be27b4e0cd400ec822d36bd85a1ed1.png

一定要关注公众号,资料随时更新,自助领取

点亮 33a43c473e5170e5883802422b5abc66.png,告诉大家你也在看970710e6a803ea172de4cf444896a552.gif

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值