redis是什么?
redis是非关系性数据库,redis 是一个开源的高性能键值数据库,主要用于数据存储和缓存。
它支持多种数据结构,如字符串、哈希、列表、集合和有序集合等。
redis的特点
- 丰富的数据类型:redis不仅支持字符串类型的键值对,还支持列表、哈希、集合和有序集合等多种数据结构,这使得redis能够处理更复杂的数据操作23。
- 高性能:由于数据存储在内存中,redis能够提供极高的读写速度,适用于需要高速响应的场景12。
- 持久化功能:redis支持将内存中的数据周期性地写入磁盘,确保数据的安全性,同时也支持将修改操作写入追加的记录文件,实现主从同步14。
- 可扩展性:redis提供了丰富的api和脚本功能,支持通过lua脚本批量执行命令,同时也支持水平扩展,类似于分库分表
依赖
<!-- springboot boot redis --> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-data-redis</artifactid> </dependency> <dependency> <groupid>redis.clients</groupid> <artifactid>jedis</artifactid> </dependency> <!-- spring boot starter test --> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency> <!-- junit 5 --> <dependency> <groupid>org.junit.jupiter</groupid> <artifactid>junit-jupiter-api</artifactid> <version>5.8.2</version> <scope>test</scope> </dependency> <dependency> <groupid>org.junit.jupiter</groupid> <artifactid>junit-jupiter-engine</artifactid> <version>5.8.2</version> <scope>test</scope> </dependency>
配置
spring: redis: host: 你的ip port: 6379
redis的配置类(用于格式转换,处理乱码)
package com.bwie.common.config; import com.fasterxml.jackson.annotation.jsonautodetect; import com.fasterxml.jackson.annotation.propertyaccessor; import com.fasterxml.jackson.databind.objectmapper; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.configuration; import org.springframework.data.redis.connection.redisconnectionfactory; import org.springframework.data.redis.core.redistemplate; import org.springframework.data.redis.serializer.jackson2jsonredisserializer; import org.springframework.data.redis.serializer.stringredisserializer; @configuration public class redisconfig { @bean public redistemplate<string, object> redistemplate(redisconnectionfactory factory) { redistemplate<string, object> template = new redistemplate<>(); template.setconnectionfactory(factory); jackson2jsonredisserializer jackson2jsonredisserializer = new jackson2jsonredisserializer(object.class); objectmapper om = new objectmapper(); om.setvisibility(propertyaccessor.all, jsonautodetect.visibility.any); om.enabledefaulttyping(objectmapper.defaulttyping.non_final); jackson2jsonredisserializer.setobjectmapper(om); stringredisserializer stringredisserializer = new stringredisserializer(); // key采用string的序列化方式 template.setkeyserializer(stringredisserializer); // hash的key也采用string的序列化方式 template.sethashkeyserializer(stringredisserializer); // value序列化方式采用jackson template.setvalueserializer(jackson2jsonredisserializer); // hash的value序列化方式采用jackson template.sethashvalueserializer(jackson2jsonredisserializer); template.afterpropertiesset(); return template; } }
string(字符串)
特点
- 最基本的数据类型,可以包含任何类型的数据,比如整数、浮点数或者字符串。
- 最大存储容量为 512mb。
业务场景
- 缓存: 存储临时数据,如用户会话信息或页面缓存。
- 计数器: 可以用来实现访问计数器、点赞数等自增功能。
- 配置存储: 存储应用程序的配置参数。
代码使用案例
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; import static org.junit.jupiter.api.assertions.assertequals; @springboottest public class stringexampletest { @autowired private redistemplate<string, object> redistemplate; @test public void teststring() { // 设置字符串值 redistemplate.opsforvalue().set("mystringkey", "hello redis!"); // 获取并验证字符串值 string value = (string) redistemplate.opsforvalue().get("mystringkey"); assertequals("hello redis!", value); // 验证值是否正确 // 更新字符串值 redistemplate.opsforvalue().set("mystringkey", "updated value"); assertequals("updated value", redistemplate.opsforvalue().get("mystringkey")); // 验证更新后的值 // 增加计数器值 redistemplate.opsforvalue().increment("mycounter", 1); assertequals(1, redistemplate.opsforvalue().get("mycounter")); // 验证计数器值 // 设置带有过期时间的字符串 redistemplate.opsforvalue().set("tempkey", "i will expire soon", 10); // 验证过期时间是否小于10秒(实际过期时间可能略小于10秒) assertequals(true, redistemplate.getexpire("tempkey") < 10l); } }
注意:
- 如果说注入的时候泛型和配置类不一样的话,可能会导致使用不了配置类的相关配置,就会采用redistemplate原有的配置,然后存储到的redis的数据就是一些乱码。
- 一定要保证注入的泛型和配置类一样,这样存储到redis中的数据格式就是正确的
list(列表)
特点
- 是一个链表结构,可以包含多个字符串值(元素)。
- 可以在列表的两端进行插入(push)和删除(pop)操作,支持对列表进行修剪(trim)以保留指定范围的元素。
业务场景
- 消息队列:通过列表实现生产者/消费者模式。
- 时间线:存储用户的活动日志或社交媒体的时间线。
- 任务调度:存储待处理任务的队列。
代码使用案例
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; import java.util.list; import static org.junit.jupiter.api.assertions.assertequals; @springboottest public class listexampletest { @autowired private redistemplate<string, object> redistemplate; @test public void testlist() { // 向列表中添加元素 redistemplate.opsforlist().leftpush("mylistkey", "item1"); redistemplate.opsforlist().leftpush("mylistkey", "item2"); // 获取并验证列表中的所有元素 list<object> list = redistemplate.opsforlist().range("mylistkey", 0, -1); assertequals(true, list.contains("item2")); // 验证列表包含 item2 assertequals(true, list.contains("item1")); // 验证列表包含 item1 // 从列表中弹出元素 object poppeditem = redistemplate.opsforlist().rightpop("mylistkey"); assertequals("item1", poppeditem); // 验证弹出的元素是 item1 // 获取列表长度 long size = redistemplate.opsforlist().size("mylistkey"); assertequals(1l, size); // 验证列表长度为 1 // 在列表开头插入新元素 redistemplate.opsforlist().insert("mylistkey", 0, "item0"); list<object> updatedlist = redistemplate.opsforlist().range("mylistkey", 0, -1); assertequals(true, updatedlist.contains("item0")); // 验证列表包含 item0 assertequals(true, updatedlist.contains("item2")); // 验证列表仍包含 item2 } }
注意:
- 如果说注入的时候泛型和配置类不一样的话,可能会导致使用不了配置类的相关配置,就会采用redistemplate原有的配置,然后存储到的redis的数据就是一些乱码。
- 一定要保证注入的泛型和配置类一样,这样存储到redis中的数据格式就是正确的
set(集合)
特点
- 是字符串的无序集合,集合中的元素都是独一无二的,即不允许重复。
- 支持集合间的交集、并集、差集等操作,可以用于处理多个用户的共同好友、标签等数据。
业务场景
- 标签系统:存储用户的兴趣标签或文章的标签。
- 好友关系:存储用户的好友列表,快速查找共同好友。
- 去重:用于去重操作,例如统计独立访客数。
代码使用案例
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; import java.util.set; import static org.junit.jupiter.api.assertions.assertequals; import static org.junit.jupiter.api.assertions.assertfalse; @springboottest public class setexampletest { @autowired private redistemplate<string, object> redistemplate; @test public void testset() { // 向集合中添加元素 redistemplate.opsforset().add("mysetkey", "member1", "member2", "member3"); // 获取并验证集合中的所有成员 set<object> members = redistemplate.opsforset().members("mysetkey"); assertequals(true, members.contains("member1")); // 验证集合包含 member1 assertequals(true, members.contains("member2")); // 验证集合包含 member2 assertequals(true, members.contains("member3")); // 验证集合包含 member3 // 从集合中删除某个成员 redistemplate.opsforset().remove("mysetkey", "member2"); assertfalse(redistemplate.opsforset().members("mysetkey").contains("member2")); // 验证 member2 已被删除 // 检查某个成员是否在集合中 boolean ismember = redistemplate.opsforset().ismember("mysetkey", "member1"); assertequals(true, ismember); // 验证 member1 仍在集合中 } }
注意:
- 如果说注入的时候泛型和配置类不一样的话,可能会导致使用不了配置类的相关配置,就会采用redistemplate原有的配置,然后存储到的redis的数据就是一些乱码。
- 一定要保证注入的泛型和配置类一样,这样存储到redis中的数据格式就是正确的
hash(哈希表)
特点
- 是一个键值对集合,适合用于存储对象。
- 每个 hash 可以存储多达 2^32 - 1 键值对(40多亿)。
- 适合存储和读取整个对象,如用户信息、商品信息等。
业务场景
- 用户信息: 存储用户的基本信息,例如用户名、邮箱和其他属性。
- 商品属性: 存储商品的详细信息,如价格、描述、库存等。
- 状态信息: 用于存储状态信息,比如会话或任务的当前状态。
代码使用案例
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; import java.util.hashmap; import java.util.map; import static org.junit.jupiter.api.assertions.assertequals; import static org.junit.jupiter.api.assertions.assertfalse; @springboottest public class hashexampletest { @autowired private redistemplate<string, object> redistemplate; @test public void testhash() { // 创建 hashmap 并存储到 redis 中 map<string, object> myhash = new hashmap<>(); myhash.put("field1", "value1"); myhash.put("field2", "value2"); redistemplate.opsforhash().putall("myhashkey", myhash); // 获取并验证哈希字段值 string fieldvalue = (string) redistemplate.opsforhash().get("myhashkey", "field1"); assertequals("value1", fieldvalue); // 验证字段值 // 更新哈希字段 redistemplate.opsforhash().put("myhashkey", "field1", "newvalue"); assertequals("newvalue", redistemplate.opsforhash().get("myhashkey", "field1")); // 验证更新后的值 // 删除哈希字段 redistemplate.opsforhash().delete("myhashkey", "field2"); assertfalse(redistemplate.opsforhash().haskey("myhashkey", "field2")); // 验证字段是否已删除 // 获取所有字段及其值 map<object, object> entries = redistemplate.opsforhash().entries("myhashkey"); assertequals(true, entries.containskey("field1")); // 验证字段存在 assertequals("newvalue", entries.get("field1")); // 验证字段值 } }
注意:
- 如果说注入的时候泛型和配置类不一样的话,可能会导致使用不了配置类的相关配置,就会采用redistemplate原有的配置,然后存储到的redis的数据就是一些乱码。
- 一定要保证注入的泛型和配置类一样,这样存储到redis中的数据格式就是正确的
sorted set(有序集合)
特点
- 类似于集合,每个成员都是唯一的,但每个成员都会关联一个浮点数值,称为分数(score)。
- 成员按分数排序,可以根据分数范围获取成员,也可以按分数获取排名。
- 适合需要根据分数进行排序和范围查询的场景,如排行榜、时间线等
业务场景
- 排行榜: 存储用户积分、排名等,可以快速查询前 n 名用户。
- 时间戳排序: 根据时间戳的排序存储事件日志。
- 优先级队列: 实现任务调度,按照优先级处理任务。
代码使用案例
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; import java.util.set; import static org.junit.jupiter.api.assertions.assertequals; import static org.junit.jupiter.api.assertions.assertfalse; @springboottest public class zsetexampletest { @autowired private redistemplate<string, object> redistemplate; @test public void testzset() { // 向有序集合中添加元素 redistemplate.opsforzset().add("myzsetkey", "member1", 1); redistemplate.opsforzset().add("myzsetkey", "member2", 2); // 获取并验证有序集合中的所有成员 set<object> zsetmembers = redistemplate.opsforzset().range("myzsetkey", 0, -1); assertequals(true, zsetmembers.contains("member1")); // 验证 member1 存在 assertequals(true, zsetmembers.contains("member2")); // 验证 member2 存在 // 更新元素的分数 redistemplate.opsforzset().incrementscore("myzsetkey", "member1", 3); double score = redistemplate.opsforzset().score("myzsetkey", "member1"); assertequals(4.0, score); // 验证 member1 的分数更新为 4.0 // 获取指定分数范围内的成员 set<object> rangebyscore = redistemplate.opsforzset().rangebyscore("myzsetkey", 1, 2); assertequals(true, rangebyscore.contains("member2")); // 验证 member2 在分数范围内 // 从有序集合中删除某个成员 redistemplate.opsforzset().remove("myzsetkey", "member2"); assertfalse(redistemplate.opsforzset().range("myzsetkey", 0, -1).contains("member2")); // 验证 member2 已被删除 } }
注意:
- 如果说注入的时候泛型和配置类不一样的话,可能会导致使用不了配置类的相关配置,就会采用redistemplate原有的配置,然后存储到的redis的数据就是一些乱码。
- 一定要保证注入的泛型和配置类一样,这样存储到redis中的数据格式就是正确的
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论