SpringBoot进阶(六)SpringBoot整合Ehcache

前言

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

方法

1.概念

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

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

2.SpringBoot整合Ehcache步骤

1)新建相应项目

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

SpringBoot进阶(六)SpringBoot整合Ehcache_第1张图片

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



	org.springframework.boot
	spring-boot-starter-cache



        javax.cache 
        cache-api
      

    org.ehcache
    ehcache

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

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

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

文件位置:



   
    java.lang.String 
    java.lang.String 
    
      20 
      10 
    
  

   
    java.lang.Long
    java.lang.String
    200
  

   
    java.lang.Number
  

   

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 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语句。

加入缓存后:

SpringBoot进阶(六)SpringBoot整合Ehcache_第2张图片

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

附录:

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

你可能感兴趣的:(SpringBoot)