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

Redis 基础(一)

Redis 基础(一)

文章目录

  • 一、了解Redis
    • 1.1 安装Redis
    • 1.2 Redis 概念(键值对结构)
    • 1.3 运用
    • 1.4 使用
      • 1.4.1 登录
        • 1 命令行
        • 2 图形化
      • 1.4.2 Redis的数据结构
      • 1.4.3 常见命令
      • 1.4.4 数据类型
        • 【1】String 类型
  • 二、Redis的 Java客户端
    • 2.1 `Jedis`客户端
      • 2.1.1 连接指定Redis
      • 2.1.2 `Junit`测试代码
      • 2.1.3 `Jedis`连接池
    • 2.2 `SpringDataRedis` 客户端
      • 2.2.1 相关介绍
        • 底层客户端
        • 核心功能
      • 2.2.2 配置
      • 2.2.3 测试代码
      • 2.2.4 序列化
      • 2.2.5 序列化工具
        • 方式一:
        • 方式二:
      • 2.2.6不带类信息的序列化

一、了解Redis

1.1 安装Redis

可以参考我的文章:

Docker Desktop 挂载本地Win系统配置指南:Redis/MySQL/RabbitMQ持久化与自启设置-CSDN博客

步骤一: 在Docker Desktop中拉去并配置好

image-20250715005454299

步骤二:最后看看,挂载在本机的哪里

image-20250715005518896

1.2 Redis 概念(键值对结构)

Redis 是一个开源的、内存中的数据结构存储系统,通常用于缓存、消息队列、会话管理等场景。它支持多种数据结构,包括字符串(String)、哈希(Hash)、列表(List)、集合(Set)、有序集合(Sorted Set)等,因此非常适用于需要快速读写的应用。

特性SQLNoSQL
数据模型关系型,表格、行、列非关系型,文档、键值、图、Graph等
扩展性垂直扩展,复杂的水平扩展水平扩展,分布式架构
查询语言SQL非SQL,自定义的
一致性强一致性,支持 ACID支持 BASE
事务支持完整的 ACID 支持基本支持,通常不提供复杂事务
性能高并发时可能遇到瓶颈高并发和大规模数据时表现优异
应用场景传统应用(金融、电商等)
安全性高的、一致性强
大数据、实时分析、社交媒体等
性能要求高的

1.3 运用

我们也可以在真正的Linux中用

image-20250715015844784

修改为后台启动redis.conf的daemonize yes,后在redis目录中,`redis-server redis.con

image-20250715020351451

开机自启, systemctl daemon-reloadsystemctl start redis

image-20250715020558655

image-20250715020738776

systemctl enable redis

image-20250715020853177

1.4 使用

1.4.1 登录

1 命令行

image-20250715021114659

2 图形化

image-20250715022149911

1.4.2 Redis的数据结构

基本类型:String、List、Set、Hash、Sorted Set。

特殊类型:Bitmaps、HyperLogLog、Geospatial、Stream。

1.4.3 常见命令

MSETkeysdelEXISTSExpiRe(key的有效期);TTL(查看key的有效期)

keys *不建议生产环境如此使用

image-20250715025208251

image-20250715030102751

TTL = -2 即有时效性,-1为永久有效

image-20250715030250671

1.4.4 数据类型

【1】String 类型

image-20250722100338189

二、Redis的 Java客户端

Redis客户端下载

image-20250803090353429

2.1 Jedis客户端

2.1.1 连接指定Redis

Step1:引入依赖

<dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>4.3.1</version> <!-- 使用最新版本 -->
</dependency>

Step2: 建立连接

import redis.clients.jedis.Jedis;public class JedisExample {public static void main(String[] args) {// 创建连接,默认连接本地6379端口Jedis jedis = new Jedis("localhost", 6379);// 如果有密码// jedis.auth("password");// 测试连接System.out.println("连接成功: " + jedis.ping());// 关闭连接jedis.close();}
}

image-20250803115110070

Step3:测试代码

package org.example;import redis.clients.jedis.Jedis;import java.util.List;public class JedisExample {public static void main(String[] args) {// 创建连接,默认连接本地6379端口Jedis jedis = new Jedis("192.168.150.143", 6379);// 如果有密码jedis.auth("123456");// 测试连接System.out.println("连接成功: " + jedis.ping());jedis.select(1);jedis.set("key", "value");
// 字符串操作jedis.set("key", "value");String value = jedis.get("key");System.out.println(value); // 输出: value// 哈希操作jedis.hset("user:1", "name", "John");jedis.hset("user:1", "age", "30");String name = jedis.hget("user:1", "name");System.out.println(name); // 输出: John// 关闭连接jedis.close();}
}

image-20250803115820092

Step4:一定要释放资源

jedis.close();

2.1.2 Junit测试代码

pom.xml中引入依赖Junit任意一个即可

<dependencies><!--junit--><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13.2</version><scope>test</scope></dependency><dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter</artifactId><version>RELEASE</version><scope>test</scope></dependency>
</dependencies>

后在Test包下,完整


import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import redis.clients.jedis.Jedis;public class JedisTest {private Jedis jedis;//    初始化jedis连接@BeforeEachvoid setUp() {jedis = new Jedis("192.168.150.143", 6379);jedis.auth("123456");jedis.select(2);}
//    测试代码String@Testvoid testRedisString() {jedis.set("name", "zhangsan");System.out.println(jedis.get("name"));jedis.set("age", String.valueOf(12));System.out.println(jedis.get("age"));}
//    测试代码Hash@Testvoid testRedisHashCode() {jedis.hset("user", "name", "lisi");jedis.hset("user", "age", "12");jedis.hset("user", "sex", "男");System.out.println(jedis.hgetAll("user"));}//    释放资源@AfterEachvoid tearDown() {if (jedis != null){jedis.close();}}
}

image-20250803122859152

image-20250803122823726

2.1.3 Jedis连接池

由于Jedis本身非线程安全,连接池还能为每个线程分配独立连接,避免并发问题,是高性能Redis访问的必备组件。

Step1:创建Jedis连接池

package org.example;import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;public class JedisConnectionFacotry {private Jedis jedis;private static final JedisPool jedisPool;static {//配置连接池JedisPoolConfig poolConfig = new JedisPoolConfig();poolConfig.setMaxTotal(8);poolConfig.setMaxIdle(8);poolConfig.setMinIdle(0);poolConfig.setMaxWaitMillis(1000);//创建连接池对象jedisPool = new JedisPool(poolConfig,"192.168.150.143",6379,1000,"123456");}public static Jedis getJedis(){return jedisPool.getResource();}@BeforeEachvoid setUp(){//建立连接jedis = JedisConnectionFacotry.getJedis();//选择库jedis.select(12);}@Testvoid testRedisHashCode() {jedis.hset("user", "name", "li1111si");jedis.hset("user", "age", "12");jedis.hset("user", "sex", "男");System.out.println(jedis.hgetAll("user"));}@AfterEachvoid tearDown() {if (jedis != null) {jedis.close();}}
}

image-20250804131214831

2.2 SpringDataRedis 客户端

Spring Data Redis 是 Spring 生态中用于简化 Redis 操作的客户端工具,它基于 Redis 的 Java 客户端(如 Jedis、Lettuce)进行了高层封装,提供了更简洁的 API 和与 Spring 框架的无缝集成。

2.2.1 相关介绍

底层客户端

Spring Data Redis 本身是封装层,实际依赖以下客户端之一:

  • Lettuce:基于 Netty 的高性能、异步客户端,支持集群和哨兵模式。
  • Jedis:老牌同步客户端,轻量但多线程环境下需连接池管理。
核心功能
  • 模板化操作:提供 RedisTemplateStringRedisTemplate 类,封装了常见的 Redis 命令(如 set/gethset/hget、事务等)。
  • 序列化支持:内置多种序列化方案(如 JSON、JDK、String),避免手动转换数据格式。
  • Repository 支持:类似 JPA,可通过接口定义自动生成 Redis 数据访问代码(需注解 @RedisHash)。
  • 发布/订阅:简化消息队列和事件驱动模型的实现。
  • 事务管理:与 Spring 事务注解(@Transactional)集成。

2.2.2 配置

<dependencies><!--redis依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><!--common-pool--><dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId></dependency><!--Jackson依赖--><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency>
</dependencies>

image-20250804142903319

spring:data:redis:host: 192.168.150.143port: 6379password: 123456lettuce:pool:max-active: 8max-idle: 8min-idle: 0max-wait: 100ms

2.2.3 测试代码

package com.example.redisdemo;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
class RedisDemoApplicationTests {@Autowiredprivate RedisTemplate redisTemplate;@Testvoid testString() {// 写入一条String数据redisTemplate.opsForValue().set("name", "lucky");// 获取string数据Object name = redisTemplate.opsForValue().get("name");System.out.println("name = " + name);}
}

2.2.4 序列化

利用JDK的序列化工具

  • 可读性差、内存占用较大
  • 默认JDK序列化

image-20250804145633523

image-20250804145737509

image-20250804145920628

2.2.5 序列化工具

方式一:
package com.example.redisdemo;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 {@Beanpublic RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {RedisTemplate<String, Object> template = new RedisTemplate<>();template.setConnectionFactory(connectionFactory);// 使用StringRedisSerializer来序列化和反序列化redis的key值template.setKeySerializer(new StringRedisSerializer());// 使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值ObjectMapper mapper = new ObjectMapper();mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);mapper.activateDefaultTyping(mapper.getPolymorphicTypeValidator(),ObjectMapper.DefaultTyping.NON_FINAL);// 修复:直接在构造函数中传入配置好的ObjectMapperJackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<>(mapper, Object.class);template.setValueSerializer(serializer);template.setHashKeySerializer(new StringRedisSerializer());template.setHashValueSerializer(serializer);template.afterPropertiesSet();return template;}
}

image-20250804165126888

方式二:
package com.example.redisdemo;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.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;@Configuration
public class RedisConfig {@Beanpublic RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory){// 创建RedisTemplate对象RedisTemplate<String, Object> template = new RedisTemplate<>();// 设置连接工厂template.setConnectionFactory(connectionFactory);// 创建JSON序列化工具GenericJackson2JsonRedisSerializer jsonRedisSerializer = new GenericJackson2JsonRedisSerializer();// 设置Key的序列化template.setKeySerializer(RedisSerializer.string());template.setHashKeySerializer(RedisSerializer.string());// 设置Value的序列化template.setValueSerializer(jsonRedisSerializer);template.setHashValueSerializer(jsonRedisSerializer);// 返回return template;}
}

image-20250804165212001

package com.example.redisdemo;import com.example.redisdemo.User;
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
class RedisDemoApplicationTests {@Autowiredprivate RedisTemplate<String, Object> redisTemplate;@Testvoid testSaveUser() {// 写入数据redisTemplate.opsForValue().set("user:100", new User("lucky", 21));// 获取数据User o = (User) redisTemplate.opsForValue().get("user:100");System.out.println("o = " + o);}
}

2.2.6不带类信息的序列化

RedisTemplate<String,Object> → JDK序列化 → 包含类信息
StringRedisTemplate + ObjectMapper → JSON序列化 → 只包含属性数据

  • RedisTemplate<String,Object>
    该模板默认使用JDK序列化器(JdkSerializationRedisSerializer)
    JDK序列化会将对象的完整类信息包含在序列化数据中
    因此存储时包含了"@class"字段来标识对象类型

  • StringRedisTemplate
    该模板专门用于处理字符串数据
    使用手动JSON序列化方式(ObjectMapper)
    JSON序列化只序列化对象的属性,不包含类的元数据信息

    @Autowiredprivate StringRedisTemplate stringRedisTemplate;// JSON序列化工具private static final ObjectMapper mapper = new ObjectMapper();
@Test
void testSaveUser1() throws JsonProcessingException {// 创建对象User user = new User("uu", 21);// 手动序列化String json = mapper.writeValueAsString(user);// 写入数据stringRedisTemplate.opsForValue().set("user:200", json);// 获取数据String jsonUser = stringRedisTemplate.opsForValue().get("user:200");// 手动反序列化User user1 = mapper.readValue(jsonUser, User.class);System.out.println("user1 = " + user1);
}

image-20250805140221252

http://www.dtcms.com/a/315674.html

相关文章:

  • 数字图像处理(冈萨雷斯)第三版:第四章——频率域滤波(学前了解知识)——主要内容和重点
  • 【运维基础】Linux 系统启动原理
  • 增量:增量处理
  • 游戏行业DDoS攻防实战指南
  • ApplicationContext的实现类有哪些?
  • 「PromptPilot 大模型智能提示词平台」—— PromptPilot × 豆包大模型 1.6:客户投诉邮件高效回复智能提示词解决方案
  • 芯祥科技:工业/车规级BMS芯片厂商 规格选型对比
  • Python import 详解
  • linux_https,udp,tcp协议(更新中)
  • C++ ---》string类的模拟实现
  • CRT调试堆检测:从原理到实战的资源泄漏排查指南
  • HBM Basic(VCU128)
  • nflsoi 7.29 题解
  • Python-深度学习--2信息熵,条件熵(ID3决策树),KL散度
  • 飞算JavaAI—AI编程助手 | 引领开发新时代,智能化编程的完美助手
  • python学智能算法(三十三)|SVM-构建软边界拉格朗日方程
  • 分布式微服务--Nacos持久化
  • Modstart 请求出现 Access to XMLHttpRequest at ‘xx‘
  • 用 Python 构建高质量的中文 Wikipedia 语料库:从原始 XML 到干净段落
  • rabbitMq内容整理
  • PromptPilot搭配Doubao-seed-1.6:定制你需要的AI提示prompt
  • 云计算一阶段Ⅱ——11. Linux 防火墙管理
  • LeetCood算法题~水果成篮
  • [element-plus] ClickOutside点击其他地方
  • 【IDEA】IntelliJ IDEA 中文官方文档全面介绍与总结
  • Docker 部署工程基本命令记录
  • uniapp renderjs 逻辑层,视图层互相传递数据封装
  • 星图云开发者平台赋能商储油安全管控数字化转型
  • 漏洞分析:90分钟安全革命
  • NLP自然语言处理 03 Transformer架构