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

B站影视 韩国电影 2025-06-10 13:03 1

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

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

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

1. 热点数据的缓存

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

2. 计数器

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

3. 排行榜

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

4. 分布式锁

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

5. 消息系统

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支持的数据类型有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的事务命令

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

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

org.springframework.boot

spring-boot-starter-data-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 RedisTemplateredisTemplateRedisTemplateredisTemplate = 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

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 RedisTemplatestrRedisTemplate;

@Autowired

redisTemplate;

@Test

public void testString {

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

Assertions.assertEquals("cc",

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

}

@Test

public void testSerializable {

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有更多的认识。

来源:程序员高级码农II一点号

相关推荐