一次springboot和redis缓存的实践

本文涉及的产品
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
简介: 性能缓慢是开发人员经常面临的一个反复出现且复杂的问题。解决此类问题的最常见方法之一是通过缓存。实际上,这种机制允许在任何类型的应用程序的性能方面实现显着改进。问题是处理缓存并不是一件容易的事。幸运的是,Spring Boot 透明地提供了缓存,这要归功于 Spring Boot 缓存抽象,这是一种允许一致使用各种缓存方法而对代码影响最小的机制。让我们看看开始处理它应该知道的一切。

性能缓慢是开发人员经常面临的一个反复出现且复杂的问题。解决此类问题的最常见方法之一是通过缓存。实际上,这种机制允许在任何类型的应用程序的性能方面实现显着改进。问题是处理缓存并不是一件容易的事。幸运的是,Spring Boot 透明地提供了缓存,这要归功于 Spring Boot 缓存抽象,这是一种允许一致使用各种缓存方法而对代码影响最小的机制。让我们看看开始处理它应该知道的一切。

首先,我们将介绍缓存的概念。然后,我们将研究最常见的 Spring Boot 缓存相关注解,了解最重要的注解是什么,在哪里以及如何使用它们。接下来,是时候看看在撰写本文时 Spring Boot 支持的最流行的缓存引擎有哪些。最后,我们将通过一个示例了解 Spring Boot 缓存的实际应用。

什么是缓存

缓存是一种旨在提高任何类型应用程序性能的机制。它依赖于缓存,缓存可以看作是一种临时的快速访问软件或硬件组件,用于存储数据以减少处理与相同数据相关的未来请求所需的时间。处理缓存是很复杂的,但掌握这个概念对于任何开发人员来说几乎都是不可避免的。如果您有兴趣深入研究缓存、了解它是什么、它是如何工作的以及它最重要的类型是什么,您应该首先点击这个链接。

如何在 Spring Boot 应用程序中实现 Redis 缓存?

为了使用 Spring Boot 实现 Redis 缓存,我们需要创建一个小型应用程序,该应用程序将具有 CRUD 操作。然后我们将在检索、更新和删除操作中应用 Redis 缓存功能。

我们将使用 REST 创建一个 CRUD 应用程序。在这里,假设我们的实体类是 Invoice.java。为了创建一个完整的 REST 应用程序,我们将根据行业最佳实践拥有控制器、服务和存储库层。一旦我们完成了 Invoice REST Application 的开发,我们将进一步在某些方法上应用注解来获得 Redis Cache 的好处。这是在我们的应用程序中实现 Redis 缓存的分步方法。

需要引入的依赖
<dependency> 
 <groupId>org.springframework.boot</groupId> 
 <artifactId>spring-boot-starter-data-redis</artifactId> 
 </dependency> 
复制代码
配置文件
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/rediscachetest
spring.datasource.username=root
spring.datasource.password=****
spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.cache.type=redis
spring.cache.redis.cache-null-values=true
#spring.cache.redis.time-to-live=40000
复制代码

启动类注解  @EnableCaching at starter class

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
 @SpringBootApplication
 @EnableCaching
public class RedisAsaCacheWithSpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(RedisAsaCacheWithSpringBootApplication.class, args);
}
} 
复制代码
创建 Invoice.java实体类
@Data
 @NoArgsConstructor
 @AllArgsConstructor
 @Entity
public class Invoice implements Serializable{
private static final long serialVersionUID = -4439114469417994311L;
 @Id
 @GeneratedValue
private Integer invId;
private String invName;
private Double invAmount;
} 
复制代码

创建一个接口 InvoiceRepository.java

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.dev.springboot.redis.model.Invoice;
 @Repository
public interface InvoiceRepository extends JpaRepository<Invoice, Integer> {
} 
复制代码

自定义异常类

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(HttpStatus.NOT_FOUND)
public class InvoiceNotFoundException extends RuntimeException {
private static final long serialVersionUID = 7428051251365675318L;
public InvoiceNotFoundException(String message) {
super(message);
}
} 
复制代码

一个具体的实现类,中间包含一些增删改查的方法

import com.dev.springboot.redis.model.Invoice;
import java.util.List;
public interface InvoiceService {
public Invoice saveInvoice(Invoice inv);
public Invoice updateInvoice(Invoice inv, Integer invId);
public void deleteInvoice(Integer invId);
public Invoice getOneInvoice(Integer invId);
public List<Invoice> getAllInvoices();
} 
复制代码
import java.util.List;
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 com.dev.springboot.redis.exception.InvoiceNotFoundException;
import com.dev.springboot.redis.model.Invoice;
import com.dev.springboot.redis.repo.InvoiceRepository;
 @Service
public class InvoiceServiceImpl implements InvoiceService {
 @Autowired
private InvoiceRepository invoiceRepo;
 @Override
public Invoice saveInvoice(Invoice inv) {
return invoiceRepo.save(inv);
}
 @Override
@CachePut(value="Invoice", key="#invId")
public Invoice updateInvoice(Invoice inv, Integer invId) {
Invoice invoice = invoiceRepo.findById(invId)
.orElseThrow(() -> new InvoiceNotFoundException("Invoice Not Found"));
invoice.setInvAmount(inv.getInvAmount());
invoice.setInvName(inv.getInvName());
return invoiceRepo.save(invoice);
}
 @Override
@CacheEvict(value="Invoice", key="#invId")
// @CacheEvict(value="Invoice", allEntries=true) //in case there are multiple records to delete
public void deleteInvoice(Integer invId) {
Invoice invoice = invoiceRepo.findById(invId)
.orElseThrow(() -> new InvoiceNotFoundException("Invoice Not Found"));
invoiceRepo.delete(invoice);
}
 @Override
@Cacheable(value="Invoice", key="#invId")
public Invoice getOneInvoice(Integer invId) {
Invoice invoice = invoiceRepo.findById(invId)
.orElseThrow(() -> new InvoiceNotFoundException("Invoice Not Found"));
return invoice;
}
 @Override
@Cacheable(value="Invoice")
public List<Invoice> getAllInvoices() {
return invoiceRepo.findAll();
}
} 
复制代码

添加Controller接口,进行请求测试

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.dev.springboot.redis.model.Invoice;
import com.dev.springboot.redis.service.InvoiceService;
 @RestController
@RequestMapping("/invoice")
public class InvoiceController {
 @Autowired
InvoiceService invoiceService;
@PostMapping("/saveInv")
public Invoice saveInvoice(@RequestBody Invoice inv) {
return invoiceService.saveInvoice(inv);
}
@GetMapping("/allInv")
public ResponseEntity<List<Invoice>> getAllInvoices(){
return ResponseEntity.ok(invoiceService.getAllInvoices());
}
@GetMapping("/getOne/{id}")
public Invoice getOneInvoice(@PathVariable Integer id) {
return invoiceService.getOneInvoice(id);
}
@PutMapping("/modify/{id}")
public Invoice updateInvoice(@RequestBody Invoice inv, @PathVariable Integer id) {
return invoiceService.updateInvoice(inv, id);
}
@DeleteMapping("/delete/{id}")
public String deleteInvoice(@PathVariable Integer id) {
invoiceService.deleteInvoice(id);
return "Employee with id: "+id+ " Deleted !";
}
}


作者:饱饱巴士

链接:https://juejin.cn/post/7164317075693895716

来源:稀土掘金

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
3天前
|
存储 缓存 NoSQL
Redis缓存的运用
缓存预热就是系统上线后,将相关的缓存数据直接加载到缓存系统。这样就可以避免在用户请 求的时候,先查询数据库,然后再将数据缓存的问题!用户直接查询事先被预热的缓存数据!
10 1
|
17天前
|
缓存 监控 NoSQL
redis 缓存穿透 击穿 雪崩 的原因及解决方法
redis 缓存穿透 击穿 雪崩 的原因及解决方法
|
7天前
|
缓存 NoSQL Java
Java一分钟之-Spring Data Redis:使用Redis做缓存
【6月更文挑战第10天】Spring Data Redis是Spring框架的一部分,简化了Java应用与Redis的集成,支持多种数据结构操作。本文介绍了其基本使用,包括添加依赖、配置Redis连接及使用RedisTemplate。还讨论了常见问题,如序列化、缓存穿透和雪崩,并提供解决方案。通过实战示例展示了缓存与数据库读写分离的实现,强调了Spring Data Redis在提升系统性能中的作用。
32 0
|
8天前
|
存储 消息中间件 缓存
Redis:内存数据存储与缓存系统的技术探索
**Redis 概述与最佳实践** Redis,全称Remote Dictionary Server,是流行的内存数据结构存储系统,常用于数据库、缓存和消息中介。它支持字符串、哈希、列表等数据结构,并具备持久化、主从复制、集群部署及发布/订阅功能。Redis适用于缓存系统、计数器、消息队列、分布式锁和实时系统等场景。最佳实践包括选择合适的数据结构、优化缓存策略、监控调优、主从复制与集群部署以及确保安全配置。
14 3
|
11天前
|
存储 缓存 NoSQL
了解Redis,第一弹,什么是RedisRedis主要适用于分布式系统,用来用缓存,存储数据,在内存中存储那么为什么说是分布式呢?什么叫分布式什么是单机架构微服务架构微服务的本质
了解Redis,第一弹,什么是RedisRedis主要适用于分布式系统,用来用缓存,存储数据,在内存中存储那么为什么说是分布式呢?什么叫分布式什么是单机架构微服务架构微服务的本质
|
存储 SQL 消息中间件
springboot整合redis
redis是一个支持key-value的数据库,数据全部在内存中处理,在在一定时间间隔中将数据固化到磁盘。因为是内存操作,所以速度特别快。(这里我们主要介绍redis作为缓存使用)
180 0
springboot整合redis
|
存储 缓存 NoSQL
SpringBoot整合Redis
SpringBoot整合Redis
343 0
SpringBoot整合Redis
|
NoSQL Redis 存储
springboot整合redis
直接上代码吧 1.首先pom中加入 org.springframework.boot spring-boot-starter-web org.
1250 0
|
NoSQL Java Redis
SpringBoot整合Redis
偷懒了几天,好几天没写springboot了。真的不是没什么可写,是因为坚持做一件事真的很难。 今天抽空弄了一个springboot整合redis的小例子。
1418 0