spring boot 数据缓存与性能优化
23.1 学习目标与重点提示
学习目标:掌握spring boot数据缓存与性能优化的核心概念与使用方法,包括数据缓存的定义与特点、spring boot与数据缓存的集成、spring boot与数据缓存的配置、spring boot与数据缓存的基本方法、spring boot的实际应用场景,学会在实际开发中处理数据缓存与性能优化问题。
重点:数据缓存的定义与特点、spring boot与数据缓存的集成、spring boot与数据缓存的配置、spring boot与数据缓存的基本方法、spring boot的实际应用场景。
23.2 数据缓存概述
数据缓存是java开发中的重要组件。
23.2.1 数据缓存的定义
定义:数据缓存是一种存储机制,用于将常用数据存储在高速存储设备中,以便快速访问。
作用:
- 提高应用程序的性能。
- 减少数据库的访问次数。
- 提高用户体验。
常见的数据缓存:
- ehcache:apache ehcache是一款开源的缓存库。
- caffeine:caffeine是一款高性能的缓存库。
- redis:redis是一款开源的缓存服务器。
✅ 结论:数据缓存是一种存储机制,作用是提高应用程序的性能、减少数据库的访问次数、提高用户体验。
23.2.2 数据缓存的特点
定义:数据缓存的特点是指数据缓存的特性。
特点:
- 高速访问:数据缓存提供高速访问。
- 数据一致性:数据缓存提供数据一致性。
- 可扩展性:数据缓存可以扩展到多个应用程序之间的缓存通信。
- 易用性:数据缓存提供易用的编程模型。
✅ 结论:数据缓存的特点包括高速访问、数据一致性、可扩展性、易用性。
23.3 spring boot与数据缓存的集成
spring boot与数据缓存的集成是java开发中的重要内容。
23.3.1 集成ehcache的步骤
定义:集成ehcache的步骤是指使用spring boot与ehcache集成的方法。
步骤:
- 创建spring boot项目。
- 添加所需的依赖。
- 配置ehcache。
- 创建数据访问层。
- 创建业务层。
- 创建控制器类。
- 测试应用。
示例:
pom.xml文件中的依赖:
<dependencies>
<!-- web依赖 -->
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-web</artifactid>
</dependency>
<!-- data jpa依赖 -->
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-data-jpa</artifactid>
</dependency>
<!-- h2数据库依赖 -->
<dependency>
<groupid>com.h2database</groupid>
<artifactid>h2</artifactid>
<scope>runtime</scope>
</dependency>
<!-- ehcache依赖 -->
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-cache</artifactid>
</dependency>
<dependency>
<groupid>net.sf.ehcache</groupid>
<artifactid>ehcache</artifactid>
</dependency>
<!-- 测试依赖 -->
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-test</artifactid>
<scope>test</scope>
</dependency>
</dependencies>application.properties文件中的ehcache配置:
# 服务器端口 server.port=8080 # 数据库连接信息 spring.datasource.url=jdbc:h2:mem:testdb spring.datasource.driver-class-name=org.h2.driver spring.datasource.username=sa spring.datasource.password=password # jpa配置 spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true # h2数据库控制台 spring.h2.console.enabled=true spring.h2.console.path=/h2-console # ehcache配置 spring.cache.type=ehcache spring.cache.ehcache.config=classpath:ehcache.xml
ehcache.xml配置文件:
<?xml version="1.0" encoding="utf-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
xsi:nonamespaceschemalocation="http://ehcache.org/ehcache.xsd">
<defaultcache
maxentrieslocalheap="10000"
eternal="false"
timetoidleseconds="120"
timetoliveseconds="120"
overflowtodisk="false"
diskpersistent="false"
diskexpirythreadintervalseconds="120">
</defaultcache>
<cache name="productcache"
maxentrieslocalheap="1000"
eternal="false"
timetoidleseconds="60"
timetoliveseconds="60"
overflowtodisk="false"
diskpersistent="false"
diskexpirythreadintervalseconds="120">
</cache>
</ehcache>实体类:
import javax.persistence.*;
@entity
@table(name = "product")
public class product {
@id
@generatedvalue(strategy = generationtype.identity)
private long id;
private string productid;
private string productname;
private double price;
private int sales;
public product() {
}
public product(string productid, string productname, double price, int sales) {
this.productid = productid;
this.productname = productname;
this.price = price;
this.sales = sales;
}
// getter和setter方法
public long getid() {
return id;
}
public void setid(long id) {
this.id = id;
}
public string getproductid() {
return productid;
}
public void setproductid(string productid) {
this.productid = productid;
}
public string getproductname() {
return productname;
}
public void setproductname(string productname) {
this.productname = productname;
}
public double getprice() {
return price;
}
public void setprice(double price) {
this.price = price;
}
public int getsales() {
return sales;
}
public void setsales(int sales) {
this.sales = sales;
}
@override
public string tostring() {
return "product{" +
"id=" + id +
", productid='" + productid + '\'' +
", productname='" + productname + '\'' +
", price=" + price +
", sales=" + sales +
'}';
}
}repository接口:
import org.springframework.data.jpa.repository.jparepository;
import org.springframework.stereotype.repository;
@repository
public interface productrepository extends jparepository<product, long> {
}service类:
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.cache.annotation.cacheevict;
import org.springframework.cache.annotation.cacheput;
import org.springframework.cache.annotation.cacheable;
import org.springframework.stereotype.service;
import org.springframework.transaction.annotation.transactional;
import java.util.list;
@service
public class productservice {
@autowired
private productrepository productrepository;
@transactional
@cacheput(value = "productcache", key = "#product.id")
public product addproduct(product product) {
return productrepository.save(product);
}
@transactional
@cacheput(value = "productcache", key = "#product.id")
public product updateproduct(product product) {
return productrepository.save(product);
}
@transactional
@cacheevict(value = "productcache", key = "#id")
public void deleteproduct(long id) {
productrepository.deletebyid(id);
}
@transactional(readonly = true)
@cacheable(value = "productcache", key = "#id")
public product getproductbyid(long id) {
system.out.println("从数据库查询产品:" + id);
return productrepository.findbyid(id).orelse(null);
}
@transactional(readonly = true)
public list<product> getallproducts() {
return productrepository.findall();
}
}控制器类:
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.web.bind.annotation.*;
import java.util.list;
@restcontroller
@requestmapping("/api/products")
public class productcontroller {
@autowired
private productservice productservice;
@getmapping("/")
public list<product> getallproducts() {
return productservice.getallproducts();
}
@postmapping("/")
public product addproduct(@requestbody product product) {
return productservice.addproduct(product);
}
@putmapping("/{id}")
public product updateproduct(@pathvariable long id, @requestbody product product) {
product.setid(id);
return productservice.updateproduct(product);
}
@deletemapping("/{id}")
public void deleteproduct(@pathvariable long id) {
productservice.deleteproduct(id);
}
@getmapping("/{id}")
public product getproductbyid(@pathvariable long id) {
return productservice.getproductbyid(id);
}
}应用启动类:
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;
import org.springframework.cache.annotation.enablecaching;
@springbootapplication
@enablecaching
public class productapplication {
public static void main(string[] args) {
springapplication.run(productapplication.class, args);
}
@autowired
private productservice productservice;
public void run(string... args) {
// 初始化数据
productservice.addproduct(new product("p001", "手机", 1000.0, 100));
productservice.addproduct(new product("p002", "电脑", 5000.0, 50));
productservice.addproduct(new product("p003", "电视", 3000.0, 80));
productservice.addproduct(new product("p004", "手表", 500.0, 200));
productservice.addproduct(new product("p005", "耳机", 300.0, 150));
}
}测试类:
import org.junit.jupiter.api.test;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.boot.test.context.springboottest;
import org.springframework.boot.test.web.client.testresttemplate;
import org.springframework.boot.web.server.localserverport;
import static org.assertj.core.api.assertions.assertthat;
@springboottest(webenvironment = springboottest.webenvironment.random_port)
class productapplicationtests {
@localserverport
private int port;
@autowired
private testresttemplate resttemplate;
@test
void contextloads() {
}
@test
void testgetproductbyid() {
product product1 = resttemplate.getforobject("http://localhost:" + port + "/api/products/1", product.class);
assertthat(product1).isnotnull();
assertthat(product1.getproductid()).isequalto("p001");
product product2 = resttemplate.getforobject("http://localhost:" + port + "/api/products/1", product.class);
assertthat(product2).isnotnull();
assertthat(product2.getproductid()).isequalto("p001");
}
@test
void testaddproduct() {
product product = new product("p006", "平板", 2000.0, 70);
product savedproduct = resttemplate.postforobject("http://localhost:" + port + "/api/products/", product, product.class);
assertthat(savedproduct).isnotnull();
assertthat(savedproduct.getproductid()).isequalto("p006");
}
@test
void testupdateproduct() {
product product = new product("p001", "手机", 1500.0, 120);
resttemplate.put("http://localhost:" + port + "/api/products/1", product);
product updatedproduct = resttemplate.getforobject("http://localhost:" + port + "/api/products/1", product.class);
assertthat(updatedproduct).isnotnull();
assertthat(updatedproduct.getprice()).isequalto(1500.0);
}
@test
void testdeleteproduct() {
resttemplate.delete("http://localhost:" + port + "/api/products/2");
product product = resttemplate.getforobject("http://localhost:" + port + "/api/products/2", product.class);
assertthat(product).isnull();
}
}✅ 结论:集成ehcache的步骤包括创建spring boot项目、添加所需的依赖、配置ehcache、创建数据访问层、创建业务层、创建控制器类、测试应用。
23.4 spring boot与数据缓存的配置
spring boot与数据缓存的配置是java开发中的重要内容。
23.4.1 配置caffeine缓存
定义:配置caffeine缓存是指使用spring boot与caffeine缓存集成的方法。
步骤:
- 创建spring boot项目。
- 添加所需的依赖。
- 配置caffeine缓存。
- 创建数据访问层。
- 创建业务层。
- 创建控制器类。
- 测试应用。
示例:
pom.xml文件中的依赖:
<dependencies>
<!-- web依赖 -->
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-web</artifactid>
</dependency>
<!-- data jpa依赖 -->
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-data-jpa</artifactid>
</dependency>
<!-- h2数据库依赖 -->
<dependency>
<groupid>com.h2database</groupid>
<artifactid>h2</artifactid>
<scope>runtime</scope>
</dependency>
<!-- caffeine依赖 -->
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-cache</artifactid>
</dependency>
<dependency>
<groupid>com.github.ben-manes.caffeine</groupid>
<artifactid>caffeine</artifactid>
</dependency>
<!-- 测试依赖 -->
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-test</artifactid>
<scope>test</scope>
</dependency>
</dependencies>application.properties文件中的caffeine缓存配置:
# 服务器端口 server.port=8080 # 数据库连接信息 spring.datasource.url=jdbc:h2:mem:testdb spring.datasource.driver-class-name=org.h2.driver spring.datasource.username=sa spring.datasource.password=password # jpa配置 spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true # h2数据库控制台 spring.h2.console.enabled=true spring.h2.console.path=/h2-console # caffeine缓存配置 spring.cache.type=caffeine spring.cache.caffeine.spec=maximumsize=1000,expireafterwrite=60s
控制器类:
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.web.bind.annotation.*;
import java.util.list;
@restcontroller
@requestmapping("/api/products")
public class productcontroller {
@autowired
private productservice productservice;
@getmapping("/")
public list<product> getallproducts() {
return productservice.getallproducts();
}
@postmapping("/")
public product addproduct(@requestbody product product) {
return productservice.addproduct(product);
}
@putmapping("/{id}")
public product updateproduct(@pathvariable long id, @requestbody product product) {
product.setid(id);
return productservice.updateproduct(product);
}
@deletemapping("/{id}")
public void deleteproduct(@pathvariable long id) {
productservice.deleteproduct(id);
}
@getmapping("/{id}")
public product getproductbyid(@pathvariable long id) {
return productservice.getproductbyid(id);
}
}测试类:
import org.junit.jupiter.api.test;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.boot.test.context.springboottest;
import org.springframework.boot.test.web.client.testresttemplate;
import org.springframework.boot.web.server.localserverport;
import static org.assertj.core.api.assertions.assertthat;
@springboottest(webenvironment = springboottest.webenvironment.random_port)
class productapplicationtests {
@localserverport
private int port;
@autowired
private testresttemplate resttemplate;
@test
void contextloads() {
}
@test
void testgetproductbyid() {
product product1 = resttemplate.getforobject("http://localhost:" + port + "/api/products/1", product.class);
assertthat(product1).isnotnull();
assertthat(product1.getproductid()).isequalto("p001");
product product2 = resttemplate.getforobject("http://localhost:" + port + "/api/products/1", product.class);
assertthat(product2).isnotnull();
assertthat(product2.getproductid()).isequalto("p001");
}
@test
void testaddproduct() {
product product = new product("p006", "平板", 2000.0, 70);
product savedproduct = resttemplate.postforobject("http://localhost:" + port + "/api/products/", product, product.class);
assertthat(savedproduct).isnotnull();
assertthat(savedproduct.getproductid()).isequalto("p006");
}
@test
void testupdateproduct() {
product product = new product("p001", "手机", 1500.0, 120);
resttemplate.put("http://localhost:" + port + "/api/products/1", product);
product updatedproduct = resttemplate.getforobject("http://localhost:" + port + "/api/products/1", product.class);
assertthat(updatedproduct).isnotnull();
assertthat(updatedproduct.getprice()).isequalto(1500.0);
}
@test
void testdeleteproduct() {
resttemplate.delete("http://localhost:" + port + "/api/products/2");
product product = resttemplate.getforobject("http://localhost:" + port + "/api/products/2", product.class);
assertthat(product).isnull();
}
}✅ 结论:配置caffeine缓存是指使用spring boot与caffeine缓存集成的方法,步骤包括创建spring boot项目、添加所需的依赖、配置caffeine缓存、创建数据访问层、创建业务层、创建控制器类、测试应用。
23.5 spring boot与数据缓存的基本方法
spring boot与数据缓存的基本方法包括使用@cacheable、@cacheput、@cacheevict注解。
23.5.1 使用@cacheable注解
定义:使用@cacheable注解是指spring boot与数据缓存集成的基本方法之一。
作用:
- 实现数据缓存。
- 提高应用程序的性能。
示例:
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 java.util.list;
@service
public class productservice {
@autowired
private productrepository productrepository;
@transactional(readonly = true)
@cacheable(value = "productcache", key = "#id")
public product getproductbyid(long id) {
system.out.println("从数据库查询产品:" + id);
return productrepository.findbyid(id).orelse(null);
}
@transactional(readonly = true)
public list<product> getallproducts() {
return productrepository.findall();
}
}✅ 结论:使用@cacheable注解是指spring boot与数据缓存集成的基本方法之一,作用是实现数据缓存、提高应用程序的性能。
23.5.2 使用@cacheput注解
定义:使用@cacheput注解是指spring boot与数据缓存集成的基本方法之一。
作用:
- 实现数据缓存。
- 提高应用程序的性能。
示例:
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.cache.annotation.cacheput;
import org.springframework.stereotype.service;
import org.springframework.transaction.annotation.transactional;
import java.util.list;
@service
public class productservice {
@autowired
private productrepository productrepository;
@transactional
@cacheput(value = "productcache", key = "#product.id")
public product addproduct(product product) {
return productrepository.save(product);
}
@transactional
@cacheput(value = "productcache", key = "#product.id")
public product updateproduct(product product) {
return productrepository.save(product);
}
@transactional(readonly = true)
@cacheable(value = "productcache", key = "#id")
public product getproductbyid(long id) {
system.out.println("从数据库查询产品:" + id);
return productrepository.findbyid(id).orelse(null);
}
@transactional(readonly = true)
public list<product> getallproducts() {
return productrepository.findall();
}
}✅ 结论:使用@cacheput注解是指spring boot与数据缓存集成的基本方法之一,作用是实现数据缓存、提高应用程序的性能。
23.5.3 使用@cacheevict注解
定义:使用@cacheevict注解是指spring boot与数据缓存集成的基本方法之一。
作用:
- 实现数据缓存。
- 提高应用程序的性能。
示例:
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.cache.annotation.cacheevict;
import org.springframework.stereotype.service;
import org.springframework.transaction.annotation.transactional;
import java.util.list;
@service
public class productservice {
@autowired
private productrepository productrepository;
@transactional
@cacheput(value = "productcache", key = "#product.id")
public product addproduct(product product) {
return productrepository.save(product);
}
@transactional
@cacheput(value = "productcache", key = "#product.id")
public product updateproduct(product product) {
return productrepository.save(product);
}
@transactional
@cacheevict(value = "productcache", key = "#id")
public void deleteproduct(long id) {
productrepository.deletebyid(id);
}
@transactional(readonly = true)
@cacheable(value = "productcache", key = "#id")
public product getproductbyid(long id) {
system.out.println("从数据库查询产品:" + id);
return productrepository.findbyid(id).orelse(null);
}
@transactional(readonly = true)
public list<product> getallproducts() {
return productrepository.findall();
}
}✅ 结论:使用@cacheevict注解是指spring boot与数据缓存集成的基本方法之一,作用是实现数据缓存、提高应用程序的性能。
23.6 spring boot的实际应用场景
在实际开发中,spring boot数据缓存与性能优化的应用场景非常广泛,如:
- 实现产品信息的缓存。
- 实现用户信息的缓存。
- 实现订单信息的缓存。
- 实现日志信息的缓存。
示例:
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;
import org.springframework.cache.annotation.enablecaching;
@springbootapplication
@enablecaching
public class productapplication {
public static void main(string[] args) {
springapplication.run(productapplication.class, args);
}
@autowired
private productservice productservice;
public void run(string... args) {
// 初始化数据
productservice.addproduct(new product("p001", "手机", 1000.0, 100));
productservice.addproduct(new product("p002", "电脑", 5000.0, 50));
productservice.addproduct(new product("p003", "电视", 3000.0, 80));
productservice.addproduct(new product("p004", "手表", 500.0, 200));
productservice.addproduct(new product("p005", "耳机", 300.0, 150));
}
}
@service
class productservice {
@autowired
private productrepository productrepository;
@transactional
@cacheput(value = "productcache", key = "#product.id")
public product addproduct(product product) {
return productrepository.save(product);
}
@transactional
@cacheput(value = "productcache", key = "#product.id")
public product updateproduct(product product) {
return productrepository.save(product);
}
@transactional
@cacheevict(value = "productcache", key = "#id")
public void deleteproduct(long id) {
productrepository.deletebyid(id);
}
@transactional(readonly = true)
@cacheable(value = "productcache", key = "#id")
public product getproductbyid(long id) {
system.out.println("从数据库查询产品:" + id);
return productrepository.findbyid(id).orelse(null);
}
@transactional(readonly = true)
public list<product> getallproducts() {
return productrepository.findall();
}
}
@restcontroller
@requestmapping("/api/products")
class productcontroller {
@autowired
private productservice productservice;
@getmapping("/")
public list<product> getallproducts() {
return productservice.getallproducts();
}
@postmapping("/")
public product addproduct(@requestbody product product) {
return productservice.addproduct(product);
}
@putmapping("/{id}")
public product updateproduct(@pathvariable long id, @requestbody product product) {
product.setid(id);
return productservice.updateproduct(product);
}
@deletemapping("/{id}")
public void deleteproduct(@pathvariable long id) {
productservice.deleteproduct(id);
}
@getmapping("/{id}")
public product getproductbyid(@pathvariable long id) {
return productservice.getproductbyid(id);
}
}
// 测试类
@springboottest(webenvironment = springboottest.webenvironment.random_port)
class productapplicationtests {
@localserverport
private int port;
@autowired
private testresttemplate resttemplate;
@test
void contextloads() {
}
@test
void testgetproductbyid() {
product product1 = resttemplate.getforobject("http://localhost:" + port + "/api/products/1", product.class);
assertthat(product1).isnotnull();
assertthat(product1.getproductid()).isequalto("p001");
product product2 = resttemplate.getforobject("http://localhost:" + port + "/api/products/1", product.class);
assertthat(product2).isnotnull();
assertthat(product2.getproductid()).isequalto("p001");
}
@test
void testaddproduct() {
product product = new product("p006", "平板", 2000.0, 70);
product savedproduct = resttemplate.postforobject("http://localhost:" + port + "/api/products/", product, product.class);
assertthat(savedproduct).isnotnull();
assertthat(savedproduct.getproductid()).isequalto("p006");
}
@test
void testupdateproduct() {
product product = new product("p001", "手机", 1500.0, 120);
resttemplate.put("http://localhost:" + port + "/api/products/1", product);
product updatedproduct = resttemplate.getforobject("http://localhost:" + port + "/api/products/1", product.class);
assertthat(updatedproduct).isnotnull();
assertthat(updatedproduct.getprice()).isequalto(1500.0);
}
@test
void testdeleteproduct() {
resttemplate.delete("http://localhost:" + port + "/api/products/2");
product product = resttemplate.getforobject("http://localhost:" + port + "/api/products/2", product.class);
assertthat(product).isnull();
}
}输出结果:
- 访问http://localhost:8080/api/products/1:第一次访问从数据库查询,第二次访问从缓存查询。
- 控制台输出:
从数据库查询产品:1
✅ 结论:在实际开发中,spring boot数据缓存与性能优化的应用场景非常广泛,需要根据实际问题选择合适的缓存策略。
总结
本章我们学习了spring boot数据缓存与性能优化,包括数据缓存的定义与特点、spring boot与数据缓存的集成、spring boot与数据缓存的配置、spring boot与数据缓存的基本方法、spring boot的实际应用场景,学会了在实际开发中处理数据缓存与性能优化问题。其中,数据缓存的定义与特点、spring boot与数据缓存的集成、spring boot与数据缓存的配置、spring boot与数据缓存的基本方法、spring boot的实际应用场景是本章的重点内容。从下一章开始,我们将学习spring boot的其他组件、微服务等内容。
到此这篇关于spring boot 数据缓存与性能优化实战指南的文章就介绍到这了,更多相关spring boot 数据缓存与性能优化内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论