【Redis】学习(3)Redis的Java客户端

Jedis客户端

Jedis使用基本步骤

Jedis的官网地址:https://github.com/redis/jedis

1.引入依赖:

2.创建Jedis对象,建立连接:

3.使用Jedis对象,方法名与Redis命令一致:

4.释放资源:

测试通过

Jedis连接池

Jedis本身是线程不安全的,并且频繁的创建和销毁链接会有性能损耗,因此推荐使用Jedis连接池代替Jedis的直连方式

1.创建连接池

2.更改建立连接方式

SpringDataRedis客户端

SpringDataRedis是Spring中数据操作的模块,包含对各种数据库的集成,其中对Redis的集成模块就叫做SpringDataRedis,官网地址:https://spring.io/projects/spring-data-redis

提供了对不同Redis客户端的整合(Lettuce和Jedis)

提供了RedisTemplate统一API来操作Redis

支持Redis的发布订阅模型

支持Redis哨兵和Redis集群

支持基于Lettuce的响应式编程

支持基于JDK,JSON,字符串,Spring对象的数据序列化及反序列化

支持基于Redis的JDKCollection实现

SpringDataRedis中提供了RedisTemplate工具类

其中封装了各种对Redis的操作。并且将不同数据类型的操作API封装到了不同的类型中:

API 返回值类型 说明
redisTemplate.opsForValue() ValueOperations 操作String类型数据
redisTemplate.opsForHash() HashOperations 操作Hash类型数据
redisTemplate.opsForList() ListOperations 操作List类型数据
redisTemplate.opsForSet() SetOperations 操作Set类型数据
redisTemplate.opsForZSet() ZSetOperations 操作SortedSet类型数据
redisTemplate 通用的命令

SpringDataRedis快速入门

SpringBoot已经提供了对SpringDataRedis的支持,使用非常简单:

1.引入spring-boot-starter-data-redis依赖

2.在application.yml配置Redis信息

Lettuce是springboot自带的,写配置文件的时候别选错了

配置文件

3.注入RedisTemplate

测试通过

SpringDataRedis的序列化方式

RedisTemplate可以接收任意的Object作为值写入Redis,只不过写入前会把Object序列化为字节形式,默认是采用JDK序列化,得到的结果如图

缺点:

可读性差,内存占用较大

可以通过自定义RedisTemplate的序列化方式解决这一点:

1.导入Maven

java 复制代码
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

如果项目中没有导入SpringMVC则需要手动导入jacson

java 复制代码
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.13.3</version>
        </dependency>

2.编写配置类

将String和Hash类型的Key设置成String,Value设置成Json

java 复制代码
 
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.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
 
@Configuration
public class RedisConfig {
 
    @Bean
    public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory connectionFactory){
 
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        //设置连接工厂
        redisTemplate.setConnectionFactory(connectionFactory);
 
        //创建Json序列化工具
        GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer=new GenericJackson2JsonRedisSerializer();
 
        //设置key的序列化
        redisTemplate.setKeySerializer(RedisSerializer.string()); //设置String 类型key为 String
        redisTemplate.setHashKeySerializer(RedisSerializer.string()); //设置Hash 类型Key 为String
 
        //设置value的序列化
        redisTemplate.setValueSerializer(genericJackson2JsonRedisSerializer); //设置 String类型的value值为 Josn类型
        redisTemplate.setHashValueSerializer(genericJackson2JsonRedisSerializer); //设置 Hash类型 的key为Json类型
 
        return redisTemplate;
 
 
 
 
    }
 
 
}
相关推荐
阿星仔6661 小时前
claude code switch安装使用指南:一键切换多Claude API
java
weixin199701080161 小时前
淘宝客商品详情页前端性能优化实战
java·前端·python·性能优化
云边散步1 小时前
godot2D游戏教程系列二(14)
笔记·学习·游戏·游戏开发
zb200641201 小时前
Redis的Spring配置
数据库·redis·spring
程途知微1 小时前
Java 内存模型 (JMM) 与 volatile 底层实现
java·后端
何中应1 小时前
IDEA中三个很方便的设置
java·ide·intellij-idea
shuair1 小时前
redis内存淘汰策略
redis
jing-ya1 小时前
day 50 图论part2
java·算法·深度优先·图论