在项目中,我们经常会用到缓存,合理的利用缓存可以提高代码工作的效率,减少不必要的重复工作。这篇文章主要是介绍Ehcache在Java项目中的简单上手。以下为步骤:
1. 引入jar包:
① 可以通过maven引入,传送门:http://mvnrepository.com/artifact/org.ehcache/ehcache
② 也可以直接下载jar包,放到项目中使用,传送门:https://github.com/ehcache/ehcache3/releases
2. 配置Ehcache(下面图片是目录结构,可参考)
① 添加ehcache-setting.xml
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<!-- 指定一个文件目录,当EhCache把数据写到硬盘上时,将把数据写到这个文件目录下 -->
<diskStore path="${java.io.tmpdir}/ehcache/sq" />
<!-- 设定缓存的默认数据过期策略 -->
<defaultCache maxElementsInMemory="10000" eternal="false"
overflowToDisk="true" timeToIdleSeconds="10" timeToLiveSeconds="20"
diskPersistent="false" diskExpiryThreadIntervalSeconds="120" />
<!-- 配置说明: timeToIdleSeconds:如果客户的访问授权15分钟内没有被使用过,则从缓存中清空。 -->
<cache name="test_cache" maxElementsInMemory="1000" eternal="false"
timeToIdleSeconds="900" overflowToDisk="true" />
</ehcache>
② 添加cache-context.xml
<?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: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.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.2.xsd">
<!-- 自动扫描注解的bean -->
<cache:annotation-driven cache-manager="cacheManager" />
<bean id="ehcacheManagerFactory"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="/WEB-INF/resource/ehcache-setting.xml"></property>
</bean>
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="ehcacheManagerFactory"></property>
</bean>
</beans>
③ context.xml中引入cache-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop//spring-aop-4.3.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"
xmlns:tx="http://www.springframework.org/schema/tx">
<import resource="resource/app-context.xml" />
<!-- <import resource="resource/spring-security.xml" /> -->
<import resource="resource/db-context.xml" />
<import resource="resource/bean-context.xml" />
<import resource="resource/cache-context.xml" />
</beans>
3. 使用Ehcache:
Ehcache的使用方式可以通过手动将值put进cache,也可以通过注解的方式自动put缓存内容。下面介绍一个简单的应用场景。
在ProjectService.java中,我们有个方法,需要获取项目的详细信息,getProjectDetailById(Integer projectId),这时候如果很多人查询项目,我们不想频繁操作数据库,就可以将第一次查询的结果,放在cache中,后面调用的人不需要再执行一次查询,而是从cache中直接取结果。代码如下:
@Cacheable(value="test_cache", key="#projectId")
public Project getProjectDetailById(Integer projectId){
Project project = null;
project = em.find(Project.class, projectId);
return project;
}
方法内是hibernate的查询,不需要管。这里使用的是Cacheable注解,test_cache即我们在ehcache-setting.xml中定义的一个缓存。这里就是将projectId为key,方法调用之后return的结果为value,存到缓存中。下一次,在缓存未过期时,用相同的参数再次调用此方法,缓存就派上用场了。
@Cacheable注解还有其他的用法,有空再补充吧。