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"));
}
}
运行:
附代码地址:https://download.csdn.net/detail/poorcoder_/9751430
Java程序员升职加薪必备技术——分布式
nacos gateway动态路由
史上最简单的 Spring 源码导读
史上最全的企业级定时任务框架 Quartz 介绍
利用 Spring 自动类型转换与回调模式写出优雅的代码
一定要关注公众号,资料随时更新,自助领取
点亮 ,告诉大家你也在看