Redis+整合SpringDataRedis

2023-11-22 20:36

本文主要是介绍Redis+整合SpringDataRedis,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Nosql和缓存的背景

数据库架构设计的发展史
第一阶段:单库:随着访问量的增加出现了性能问题
第二阶段:缓存:通过缓存,缓解数据库的压力,优化数据结构和索引
第三阶段:读写分离:数据写入的压力增加,主从复制和读写分离的方案进入视野
第四阶段:分库分表:主表的写压力过高,开始使用InnoDB引擎,以及分库分表技术

核心问题:
传统数据库的扩展性差(需要复杂的技术来实现),大数据下IO压力大,表结构更加困难

持久化方式

持久化:将数据(如内存中的对象)保存到可永久保存的存储设备中

方式一:RDB方式
在指定的时间间隔内对数据进行快照存储。先将数据集写入临时文件,写入成功后,再替换之前的文件,用二进制压缩存储,是一次的全量备份
方式二:AOF方式
以日志文本的形式记录服务器所处理的每一个数据更改指令,然后通过重放来恢复数据,是连续的增量备份

Redis整合SpringDataRedis

Spring Data是Spring公司的顶级项目,里面包含了N多个二级子项目,这些子项目都是相当独立的项目。每个子项目是对不同API的封装
所有Spring Boot整合Spring Data xxx的启动器都叫做spring-boot-starter-data-xxx
Spring Data好处很方便操作对象类型。
把Redis不同值的类型放到一个opsForxxx方法中
opsForValue:String值
opsForList:列表List
opsForHash:哈希表Hash
opsForZSet:有序集合Sorted Set
opsForSet:集合
使用步骤:
1.添加依赖

 <!--导入redis依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId><version>2.5.0</version></dependency>

2.配置文件

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/userdb?characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=123456mybatis.type-aliases-package=com.zuxia.entity
mybatis.mapper-locations=classpath:mapper/*.xml#配置redis 服务参数
spring.redis.database=1
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=spring.redis.jedis.pool.max-active=80
spring.redis.jedis.pool.max-wait=-1spring.redis.jedis.pool.max-idle=50spring.redis.jedis.pool.min-idle=30spring.redis.timeout=500

3.编写配置类

@Configuration
public class RedisConfig {@Beanpublic RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory){RedisTemplate<String ,Object> redisTemplate=new RedisTemplate<>();redisTemplate.setKeySerializer(new StringRedisSerializer());redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<Object>(Object.class));redisTemplate.setConnectionFactory(factory);return redisTemplate;}
}
  1. Controller类
package com.zuxia.controller;import com.sun.org.apache.xpath.internal.operations.Mod;
import com.zuxia.entity.Product;
import com.zuxia.service.IProduct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;@Controller
public class ProductController {@Autowiredprivate IProduct iProduct;@GetMapping("/show")public String select(Integer id, Model model){Product product = iProduct.findByID(id);model.addAttribute("product",product);return "show";}@RequestMapping("/del")@ResponseBodypublic String del(Integer id, Model model){int i=iProduct.del(id);if(i>0){return "删除成功";}return "删除失败";}@RequestMapping("/update ")@ResponseBodypublic String update(Product product, Model model){int i=iProduct.update(product);if(i>0){return "修改成功";}return "修改失败";}@RequestMapping("/add")@ResponseBodypublic String Add(Product product, Model model){int i=iProduct.insert(product);if(i>0){return "增加成功";}return "曾加失败";}
}
  1. Service类
package com.zuxia.service.impl;import com.zuxia.entity.Product;
import com.zuxia.mapper.ProductMapper;
import com.zuxia.service.IProduct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.stereotype.Service;@Service
public class ProductServiceImpl implements IProduct {@Autowired(required = false)private ProductMapper productMapper;@Autowiredprivate RedisTemplate<String,Object> redisTemplate;@Overridepublic Product findByID(Integer id) {String key="product:"+id;//先从redis中获取数据if(redisTemplate.hasKey(key)){//判断id是否在缓存中存在System.out.println("执行缓存");//设置value的值redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<Product>(Product.class));//从缓存中获取数据Product product=(Product) redisTemplate.opsForValue().get(key);return product;}System.out.println("执行mysql数据查询");Product product=productMapper.findByID(id);//将查到的值存入缓存中redisTemplate.opsForValue().set(key,product);return product;}@Overridepublic int del(Integer id) {int i=productMapper.del(id);String key="prodoct:"+id;//判断缓存中是否存在boolean ret=  redisTemplate.hasKey(key);if(ret){System.out.println("缓存中的数据删除了");redisTemplate.delete(key);}return i;}@Overridepublic int update(Product product) {int i=productMapper.update(product);if(i>0){String key="product"+product.getId();if(redisTemplate.hasKey(key)){System.out.println("缓存的数据更改了");redisTemplate.opsForValue().set(key,product);}}return i;}@Overridepublic int insert(Product product) {return productMapper.insert(product);}
}

这篇关于Redis+整合SpringDataRedis的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/412749

相关文章

springboot整合阿里云百炼DeepSeek实现sse流式打印的操作方法

《springboot整合阿里云百炼DeepSeek实现sse流式打印的操作方法》:本文主要介绍springboot整合阿里云百炼DeepSeek实现sse流式打印,本文给大家介绍的非常详细,对大... 目录1.开通阿里云百炼,获取到key2.新建SpringBoot项目3.工具类4.启动类5.测试类6.测

Redis在windows环境下如何启动

《Redis在windows环境下如何启动》:本文主要介绍Redis在windows环境下如何启动的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Redis在Windows环境下启动1.在redis的安装目录下2.输入·redis-server.exe

Redis实现延迟任务的三种方法详解

《Redis实现延迟任务的三种方法详解》延迟任务(DelayedTask)是指在未来的某个时间点,执行相应的任务,本文为大家整理了三种常见的实现方法,感兴趣的小伙伴可以参考一下... 目录1.前言2.Redis如何实现延迟任务3.代码实现3.1. 过期键通知事件实现3.2. 使用ZSet实现延迟任务3.3

Redis分片集群的实现

《Redis分片集群的实现》Redis分片集群是一种将Redis数据库分散到多个节点上的方式,以提供更高的性能和可伸缩性,本文主要介绍了Redis分片集群的实现,具有一定的参考价值,感兴趣的可以了解一... 目录1. Redis Cluster的核心概念哈希槽(Hash Slots)主从复制与故障转移2.

一文详解如何从零构建Spring Boot Starter并实现整合

《一文详解如何从零构建SpringBootStarter并实现整合》SpringBoot是一个开源的Java基础框架,用于创建独立、生产级的基于Spring框架的应用程序,:本文主要介绍如何从... 目录一、Spring Boot Starter的核心价值二、Starter项目创建全流程2.1 项目初始化(

Redis 中的热点键和数据倾斜示例详解

《Redis中的热点键和数据倾斜示例详解》热点键是指在Redis中被频繁访问的特定键,这些键由于其高访问频率,可能导致Redis服务器的性能问题,尤其是在高并发场景下,本文给大家介绍Redis中的热... 目录Redis 中的热点键和数据倾斜热点键(Hot Key)定义特点应对策略示例数据倾斜(Data S

Spring Boot 整合 MyBatis 连接数据库及常见问题

《SpringBoot整合MyBatis连接数据库及常见问题》MyBatis是一个优秀的持久层框架,支持定制化SQL、存储过程以及高级映射,下面详细介绍如何在SpringBoot项目中整合My... 目录一、基本配置1. 添加依赖2. 配置数据库连接二、项目结构三、核心组件实现(示例)1. 实体类2. Ma

redis+lua实现分布式限流的示例

《redis+lua实现分布式限流的示例》本文主要介绍了redis+lua实现分布式限流的示例,可以实现复杂的限流逻辑,如滑动窗口限流,并且避免了多步操作导致的并发问题,具有一定的参考价值,感兴趣的可... 目录为什么使用Redis+Lua实现分布式限流使用ZSET也可以实现限流,为什么选择lua的方式实现

Redis中管道操作pipeline的实现

《Redis中管道操作pipeline的实现》RedisPipeline是一种优化客户端与服务器通信的技术,通过批量发送和接收命令减少网络往返次数,提高命令执行效率,本文就来介绍一下Redis中管道操... 目录什么是pipeline场景一:我要向Redis新增大批量的数据分批处理事务( MULTI/EXE

Redis中高并发读写性能的深度解析与优化

《Redis中高并发读写性能的深度解析与优化》Redis作为一款高性能的内存数据库,广泛应用于缓存、消息队列、实时统计等场景,本文将深入探讨Redis的读写并发能力,感兴趣的小伙伴可以了解下... 目录引言一、Redis 并发能力概述1.1 Redis 的读写性能1.2 影响 Redis 并发能力的因素二、