springboot 整合EHcache 实现缓存技术
2023-09-27 14:22:13 时间
一 ehcache的概述
1.@Cacheable注解的作用
@Cacheable 对当前的对象做缓存处理
@Cacheable
public List<Users> findUserAll() {
return this.userRepository.findAll();
}
@Cacheable 作用:把方法的返回值添加到 Ehcache 中做缓存
Value 属性:指定一个 Ehcache 配置文件中的缓存策略,如果没有给定 value,那么则表示使用默认的缓存策略
@Cacheable(value = "users")
public List<Users> findUserAll() {
return this.userRepository.findAll();
}
key的作用:给存储的值起个名称,在查询时如果有名称相同时,则从已知的名称中取值。 否则从数据库中查询数据。
配置文件设置缓存策略:
注意事项:缓存的对象必须实现序列化
二 操作案例
2.1.新建项目配置pom文件
加配置依赖:
<!-- Spring Boot缓存支持启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!-- Ehcache坐标 -->
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.1.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
<!-- springBoot的启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- springBoot的thymeleaf启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- springBoot的jpa启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- 测试工具的启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- druid连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.9</version>
</dependency>
<!-- Spring Boot缓存支持启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!-- Ehcache坐标 -->
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.1.2</version>
</dependency>
2.2 dao层
package com.ljf.spring.boot.demo.dao;
import com.ljf.spring.boot.demo.model.Users;
import org.springframework.data.jpa.repository.JpaRepository;
/**
* 参数一 T :当前需要映射的实体
* 参数二 ID :当前映射的实体中的OID的类型
*
*/
public interface UserRepository extends JpaRepository<Users,Integer> {
}
2.3.model层
注意users要实现序列化
package com.ljf.spring.boot.demo.model;
import javax.persistence.*;
import java.io.Serializable;
/**
* @ClassName: Users
* @Description: TODO
* @Author: liujianfu
* @Date: 2020/09/02 08:50:18
* @Version: V1.0
**/
@Entity
@Table(name="tb_users_tb")
public class Users implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Integer id;
@Column(name = "name")
private String name;
@Column(name = "age")
private Integer age;
@Column(name = "address")
private String address;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Users [id=" + id + ", name=" + name + ", age=" + age + ", address=" + address + "]";
}
}
2.4.service层
public interface UserService {
List<Users> findUserAll();
}
package com.ljf.spring.boot.demo.service.impl;
import com.ljf.spring.boot.demo.dao.UserRepository;
import com.ljf.spring.boot.demo.model.Users;
import com.ljf.spring.boot.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @ClassName: UserServiceImpl
* @Description: TODO
* @Author: liujianfu
* @Date: 2020/09/02 08:57:11
* @Version: V1.0
**/
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserRepository userRepository;
@Override
//@Cacheable 对当前的对象做缓存处理
//@Cacheable(value = "users")
public List<Users> findUserAll() {
return this.userRepository.findAll();
}
}
2.5.配置文件
2.5.1 application配置文件
在配置文件中引入缓存的配置文件:
spring.cache.ehcache.cofnig=ehcache.xml
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test_db
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.cache.ehcache.cofnig=ehcache.xml
2.5.2 ehcache.xml配置文件
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
<diskStore path="java.io.tmpdir"/>
<!--defaultCache:echcache的默认缓存策略 -->
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
maxElementsOnDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
<persistence strategy="localTempSwap"/>
</defaultCache>
<!-- 自定义缓存策略 -->
<cache name="users"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
maxElementsOnDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
<persistence strategy="localTempSwap"/>
</cache>
</ehcache>
2.6 启动类和测试类
启动类上添加,开启缓存的注解@EnableCaching
package com.ljf.spring.boot.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
/**
* Hello world!
*
*/
@SpringBootApplication
@EnableCaching
public class StartApp
{
public static void main( String[] args )
{
SpringApplication.run(StartApp.class, args);
System.out.println( "Hello World!" );
}
}
package com.ljf.spring.boot.demo;
import static org.junit.Assert.assertTrue;
import com.ljf.spring.boot.demo.model.Users;
import com.ljf.spring.boot.demo.service.UserService;
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 java.util.List;
/**
* Unit test for simple App.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes={StartApp.class}) //springboot的启动类
public class AppTest
{
@Autowired
private UserService us;
/**
* Rigorous Test :-)
*/
@Test
public void query(){
List<Users> list=us.findUserAll();
System.out.println("第一次:"+list.get(0).getAddress());
List<Users> list2=us.findUserAll();
System.out.println("第2次:"+list2.get(0).getAddress());
}
}
2.7.不加缓存执行
2.8.开启缓存执行
通过console打印的日志可以看到:只打印了一次数据库的日志,查询了一次数据库。
相关文章
- 微服务技术系列教程(48)-SpringBoot WebMvcConfigurer接口
- 微服务技术系列教程(47)-SpringBoot多配置文件总结
- 微服务技术系列教程(46)-SpringBoot整合MongoDB(文章评论案例)
- 微服务技术系列教程(39)- SpringBoot -RBAC权限模型
- 微服务技术系列教程(13) - SpringBoot - 其它
- 微服务技术系列教程(08) - SpringBoot - 热部署(IDEA版)
- 微服务技术系列教程(03) - SpringBoot - 整合各种数据源(JdbcTemplate、MyBatis、JPA)
- 基于Java(SSM+SpringBoot+Thymeleaf)+MySQL 开发的论坛社区网站【100010072】
- SpringBoot自定义starter
- SpringBoot application.properties (application.yml)优先级从高到低
- springboot之mybatis别名的设置
- vue实战入门后台篇三:springboot+mybatis实现网站后台-实体及基础框架搭建
- springboot使用undertow假死的原因
- 推荐一款基于 SpringBoot + Vue 的前后端分离实战项目!技术主流,文档很全!
- SpringBoot入门七(整合之事务和连接池)
- SpringBoot启动过程解析(简化)
- springboot+rocketmq(3):实现广播消息
- 将springboot单体项目部署到腾讯云服务器上