SpringBoot进阶(六)SpringBoot整合Ehcache

前言

       本章讲解SpringBoot整合Ehcache的相关知识

方法

1.概念

EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider。

由于我们整合SpringData JPA也是基于Hibernate实现的,所以Hibernate的缓存机制想必大家都应有所了解,其使用的缓存器就是这个Ehcache,引入缓存处理可以大大降低数据库压力,实现快速查询的目的。

2.SpringBoot整合Ehcache步骤

1)新建相应项目

可以将上个章节的项目做一个拷贝,方便我们进行修改。

2)修改pom文件,增加缓存支持的坐标

<!-- 配置springBoot 缓存支持的启动器 -->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!-- 配置ehcache缓存器 -->
<dependency>
        <groupId>javax.cache</groupId> <!-- JSR-107 API-->
        <artifactId>cache-api</artifactId>
      </dependency>
<dependency>
    <groupId>org.ehcache</groupId>
    <artifactId>ehcache</artifactId>
</dependency>

3)创建ehcache.xml文件,该文件用来描述使用ehcache做缓存时的缓存策略

注意:本次使用的是ehcache3做缓存,其配置文件已经和ehcache2完全不同,这一点需要大家注意!!

我们拿取官方文档的配置文件做例子,其内部标签的详解也请移步ehcahe官网https://www.ehcache.org/查看详情

文件位置:

<config
    xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
    xmlns='http://www.ehcache.org/v3'
    xsi:schemaLocation="http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core.xsd">

  <cache alias="foo"> 
    <key-type>java.lang.String</key-type> 
    <value-type>java.lang.String</value-type> 
    <resources>
      <heap unit="entries">20</heap> 
      <offheap unit="MB">10</offheap> 
    </resources>
  </cache>

  <cache-template name="myDefaults"> 
    <key-type>java.lang.Long</key-type>
    <value-type>java.lang.String</value-type>
    <heap unit="entries">200</heap>
  </cache-template>

  <cache alias="bar" uses-template="myDefaults"> 
    <key-type>java.lang.Number</key-type>
  </cache>

  <cache alias="simpleCache" uses-template="myDefaults" /> 

</config>

4)修改启动类文件,添加允许缓存的注解

package cn.edu.ccut;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;

@SpringBootApplication
@EnableCaching//允许缓存
public class App {

	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}

}

5)在需要缓存的service方法上加入@Cacheable注解,表示缓存。

注意:需要指定配置文件中所涉及到的缓存策略,如:foo

package cn.edu.ccut.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.edu.ccut.bo.Users;
import cn.edu.ccut.dao.UserDAO;
import cn.edu.ccut.service.UserService;

@Service
@Transactional
public class UserServiceImpl implements UserService{
	
	@Autowired
	private UserDAO userDao;

	@Override
	@Cacheable("foo")
	public List<Users> getAllUser() {
		return userDao.findAll();
	}

}

6)最后还需要在application.properties进行修改

#Mysql JDBC
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/jwang
spring.datasource.username=root
spring.datasource.password=root
#Druid Pool
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#SpringData JPA
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
#Ehcache
spring.cache.ehcache.config=ehcache.xml

3.编写测试用例进行测试

编写测试方法:

package cn.edu.ccut.test;

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

import cn.edu.ccut.App;
import cn.edu.ccut.service.UserService;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes={App.class})
public class UserTest {

	@Autowired
	private UserService userService;

	@Test
	public void testFindAllUser() {
		//第一次查询
		userService.getAllUser();
		//第二次查询
		userService.getAllUser();
		
	}
}

在没有使用缓存的情况下,控制台输出是这样的:

我们可以看到:没有缓存的条件下,向数据库发送了两次请求,产生了两次SQL语句。

加入缓存后:

由此可见,我们的缓存成功起到了效果!

附录:

我们可以使用@CacheEvict注解来清除缓存,通常用在注入非数据查询(增加、删除等)业务方法上。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值