当前位置: 首页 > news >正文

SpringBoot的Web应用开发——Web缓存利器Redis的应用!

Web缓存利器Redis的应用

Redis是目前使用非常广泛的开源的内存数据库,是一个高性能的keyvalue数据库,它支持多种数据结构,常用做缓存、消息代理和配置中心。本节将简单介绍Redis的使用,想深入了解的读者可以参考其官方文档继续学习。

Redis的应用场景

Redis在项目中的应用场景有以下几个:

1. 热点数据的缓存

由于Redis的访问速度快、支持的数据类型很丰富,所以很适合用来存储热点数据,其内置的expire可以对缓存的数据设置过期时间。在缓存的数据过期后再设置新的缓存数据。

2. 计数器

Redis的incrby命令是原子性地递增,因此可以运用于商城系统的高并发的秒杀活动、分布式序列号的生成等场景。

3. 排行榜

可以使用Redis的SortedSet进行热点数据的排序。

4. 分布式锁

Redis的setnx命令的作用是,如果当前的缓存数据,不存在则设置缓存成功同时返回1,否则设置缓存失败并返回0。可以利用这个特性在Redis集群中检测锁的有效时间,如果超时,那么等待的进程将有机会获得锁,从而防止项目出现死锁。

5. 消息系统

Redis也可以作为消息系统,但在实际场景中用得不多。

Redis的安装和使用

本文以Window系统为例,简单介绍Redis的安装和使用。

(1)下载最新版Redis的Window版,然后解压文件。双击redisserver.exe会打开Redis服务,如图4.14所示,表示Redis已经启动成功。

图4.14 Redis服务端启动

(2)如果要使用Redis命令行工具,双击redis-cli.exe就会打开Redis的命令行界面,如图4.15所示。

图4.15 Redis cli工具

 Redis的命令

Redis支持的数据类型有String(字符串)、Hash(哈希)、List(列表)和Set(不重复集合),常用的命令如表4.4至表4.9所示。

Redis全局命令如表4.4所示。

表4.4 Redis全局命令列表

针对String类型数据的操作命令整理如表4.5所示。

表4.5 String类型数据的操作命令

针对Hash类型数据的操作命令如表4.6所示。

表4.6 Hash类型数据的操作命令

针对List操作类型数据的操作命令如表4.7所示。

表4.7 List类型数据的操作命令

针对Set类型数据的操作命令如表4.8所示。

表4.8 Set类型数据的操作命令

说明:smembers、lrange和hgetall都属于比较“重”(消耗Redis性能)的命令,可以使用sscan来完成。

Redis的事务和数据库的事务含义相似,都是将多个操作合为一个整体,操作的结果要么成功,要么失败。Redis事务的命令如表4.9所示。

表4.9 Redis的事务命令

实战:在Spring Boot项目中集成Redis

前面介绍了Redis的基础知识,下面在项目中集成Redis。

(1)启动本地的Redis服务,在Spring Boot项目的pom.xml中添加Redis依赖,使用Spring-redis工具:

<!-- 添加Redis客户端 -->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-data-redis</artifactId>

</dependency>

(2)添加Redis操作配置文件代码如下:

package com.example.thymeleafdemo.redis;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.data.redis.connection.lettuce.Lettuce

ConnectionFactory;

import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.data.redis.serializer.GenericJackson2Json

RedisSerializer;

import

org.springframework.data.redis.serializer.StringRedisSerializer;@Configuration

public class RedisConfig {

@Bean

public RedisTemplate<String, Object> redisTemplate

(LettuceConnectionFactory connectionFactory) {

RedisTemplate<String, Object> redisTemplate = new

RedisTemplate<>();

redisTemplate.setKeySerializer(new StringRedisSerializer());

redisTemplate.setValueSerializer(new

GenericJackson2JsonRedisSerializer());

redisTemplate.setConnectionFactory(connectionFactory);

return redisTemplate;

}

}

(3)在application.properties中添加Redis的配置文件代码如下:

#Redis 基础配置

# Redis数据库索引(默认为0)

spring.redis.database=0

# Redis服务器地址

spring.redis.host=127.0.0.1

# Redis服务器连接端口

spring.redis.port=6379

# Redis服务器连接密码(默认为空)

#spring.redis.password=

# 链接超时时间 单位为ms(毫秒)

spring.redis.timeout=3000

#Redis线程池设置

# 连接池最大连接数(使用负值表示没有限制) 默认为8

spring.redis.jedis.pool.max-active=8

# 连接池最大阻塞等待时间(使用负值表示没有限制) 默认为-1

spring.redis.jedis.pool.max-wait=-1

# 连接池中的最大空闲连接 默认为8


spring.redis.jedis.pool.max-idle=8# 连接池中的最小空闲连接 默认为0

spring.redis.jedis.pool.min-idle=0

(4)添加Redis操作的测试方法:

package com.example.thymeleafdemo;

import com.example.thymeleafdemo.event.Result;

import org.junit.jupiter.api.Assertions;

import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.test.context.SpringBootTest;

import org.springframework.data.redis.core.RedisTemplate;

// 指定启动类

@SpringBootTest(classes = {ThymeleafDemoApplication.class})

public class RedisTest {

@Autowired

private RedisTemplate<String, String> strRedisTemplate;

@Autowired

private RedisTemplate<String, Object> redisTemplate;

@Test

public void testString() {

strRedisTemplate.opsForValue().set("name", "cc");

Assertions.assertEquals("cc",

strRedisTemplate.opsForValue().get("name"));

}

@Test

public void testSerializable() {

Result<Object> result = new Result<>();

result.setData("cc");

result.setMessage("success");

result.setCode(200);

redisTemplate.opsForValue().set("result", result);

Result result2 = (Result)redisTemplate.opsForValue().get("result");

Assertions.assertEquals(result2, result);

}

}

(5)运行测试用例,testString()方法用于测试String类型的缓存数据的获取值,testSerializable()方法肜于测试缓存对象的保存和再次获取,两个测试用例都通过,结果如图4.16所示。至此,Spring Boot集成Redis的工作已经完成。

图4.16 Redis的测试用例

Redis还有很多的使用场景,若把Redis展开讲解,写一本书都不为过。

Redis在项目中常用的功能还有布隆过滤器,布隆过滤器可以进行在线人数的统计。在开发过程中多总结、多看源码、多讨论,就能对Redis有更多的认识。

http://www.xdnf.cn/news/1023319.html

相关文章:

  • 【UEFI系列】PEI阶段讲解
  • 生产环境LVM存储降级方案
  • Python训练营---DAY53
  • Git 前后端 Java Vue 项目的 .gitignore 配置分享
  • Linux环境下安装和使用RAPIDS平台的cudf和cuml - pip 安装方法
  • java集合(八) ---- Vector 类
  • 电磁铁性能检测所需工具
  • DataGrip 安装和连接Mysql
  • Eslint、Prettier、.vscode 配置
  • 阳台光伏新风口!安科瑞ADL200N-CT/D16-WF防逆流电表精准护航分布式发电
  • NLP学习路线图(四十三):零样本学习
  • 分布式爬虫系统设计与实现:跨节点MySQL存储方案
  • 导出支付宝账单步骤
  • 3款工具打造递进图:快速入门与个性化定制的实用指南
  • 帆软报表超级链接将查询控件的参数传递到子页面查询控件上
  • 谷歌具身智能VLA大模型 —— Gemini Robotics : 将人工智能带入到物理世界
  • 停产料PC28F128J3F75A存储芯片Micron镁光NOR Flash存储器工业级 电子元器件解析
  • AI LLM大模型逆向环境搭建radare2 + r2mcp + r2ghidra
  • AD左边工程面板消失重新打开
  • ansible常用内置模块
  • 13.18 Ollama+LLaMA3企业级部署实战:6步打造私有化大模型高效引擎
  • 【JVM】- 类加载与字节码结构1
  • AXI4-Stream Clock Converter IP
  • 封装python的docker镜像
  • 前端JavaScript面试题(2)
  • 面经总结池
  • Trae国内版使用技巧
  • 通关JUC:Java并发工具包从入门到精通 | 深度源码解析​
  • 720云vr全景怎么制作?720全景制作费用?
  • 什么是PostCSS