Redis 的 Java 客户端

本文涉及的产品
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
云原生内存数据库 Tair,内存型 2GB
云数据库 Redis 版,倚天版 1GB 1个月
简介: Redis 的 Java 客户端

Redis 的 Java 客户端

客户端对比

Jedis

  • 引入依赖
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>4.2.0</version>
</dependency>
  • 编码测试
package com.ruochen.test;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import redis.clients.jedis.Jedis;

import java.util.Map;

public class JedisTest {

    private Jedis jedis;

    @BeforeEach
    void setUp() {
        // 1. 建立连接
        jedis = new Jedis("ip", 6379);
        // 2. 设置密码
        jedis.auth("pwd");
        // 3. 选择库
        jedis.select(0);
    }

    @Test
    void testString() {
        // 存入数据
        String result = jedis.set("name", "ruochen");
        System.out.println("result => " + result);
        // 获取数据
        String name = jedis.get("name");
        System.out.println("name => " + name);
    }

    @Test
    void testHash() {
        // 插入 hash 数据
        jedis.hset("user:1", "name", "ruochen");
        jedis.hset("user:1", "age", "22");

        // 获取数据
        Map<String, String> map = jedis.hgetAll("user:1");
        System.out.println(map);
    }

    @AfterEach
    void tearDown() {
        // 关闭连接
        if (jedis != null) {
            jedis.close();
        }
    }
}

Jedis 连接池

  • Jedis 连接池工具类
package com.ruochen.jedis.util;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class JedisConnectionFactory {

    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,
                "ip", 6379, 1000, "pwd");
    }

    public static Jedis getJedis() {
        return jedisPool.getResource();
    }
}
  • 获取时直接从连接池获取即可
jedis = JedisConnectionFactory.getJedis();

SpringDataRedis

  • 引入依赖 pom.xml
<!--redis 依赖-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

<!--连接池依赖-->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
</dependency>
  • 配置 application.yml
spring:
  redis:
    host: ip
    port: 6379
    password: pwd
    jedis:
      pool:
        # 最大连接
        max-active: 8
        # 最大空闲连接
        max-idle: 8
        # 最小空闲连接
        min-idle: 0
        # 等待时长
        max-wait: 1000ms
  • 测试(String)
package com.ruochen;

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 SpringdataRedisDemoApplicationTests {

    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    void testString() {
        // 写入一条 String 数据
        redisTemplate.opsForValue().set("name", "若尘");
        // 获取 String 数据
        Object name = redisTemplate.opsForValue().get("name");
        System.out.println("name => " + name);
    }

}
  • RedisTemplate 写入前会把 Object 序列化为字节形式(默认采用 JDK 序列化),会得到如下结果


  • 那么,如果我们不想得到这样的结果,我们就要改变 RedisTemplate 的序列化方式。若 key value 都为 String 类型,一般使用 StringRedisSerializer,若为 Java 对象,一般使用 GenericJackson2JsonRedisSerializer

  • 编写 RedisTemplate 配置类
package com.ruochen.redis.config;

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 {

    @Bean
    public 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.setHashKeySerializer(jsonRedisSerializer);
        // 返回
        return template;
    }
}
  • 由于项目目前没有引入 srping-mvc,我们需要手动引入一下 Jackson 依赖
<!--Jackson 依赖-->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
</dependency>
  • 测试
package com.ruochen;

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 SpringdataRedisDemoApplicationTests {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Test
    void testString() {
        // 写入一条 String 数据
        redisTemplate.opsForValue().set("name", "若尘");
        // 获取 String 数据
        Object name = redisTemplate.opsForValue().get("name");
        System.out.println("name => " + name);
    }
}
  • 可以看到,写入成功

  • 接下来,我们测试一下 RedisTemplate 能否将 Java 对象进行序列化。先写一个实体类 User
package com.ruochen.redis.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
    private String name;
    private Integer age;
}
  • 接下来进行测试
    @Test
    void testSaveUser() {
        // 写入数据
        redisTemplate.opsForValue().set("user:222", new User("ruochen", 22));
        // 获取数据
        User user = (User) redisTemplate.opsForValue().get("user:222");
        System.out.println("user => " + user);
    }
  • 然后我们通过图形界面可以看到已经存入成功

  • 我们可以注意到,在写入 json 数据的同时写入了一条 Class 属性,对应类的字节码名称,正式因为有这条属性,在反序列化时才能读取到类的字节码名称,从而将 json 反序列化为对用的 User
  • 虽然上述的 JSON 序列化方式已经可以解决我们的问题,但由此引发了另外一个问题,为了在反序列化时知道对象的类型,将类的class写入json中势必会带来额外的内存开销。因此,为了节省空间,我们统一使用 String 序列化器,当存储 Java 对象时,就得手动进行序列化和反序列化

  • Spring 已经默认提供了一个 StringRedisTemplate 类,它的 key 和 value 的序列化方式默认就是 String 方式
package com.ruochen;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruochen.redis.pojo.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.StringRedisTemplate;

@SpringBootTest
class RedisStringTests {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Test
    void testString() {
        // 写入一条 String 数据
        stringRedisTemplate.opsForValue().set("name", "若尘");
        // 获取 String 数据
        Object name = stringRedisTemplate.opsForValue().get("name");
        System.out.println("name => " + name);
    }

    private static final ObjectMapper mapper = new ObjectMapper();

    @Test
    void testSaveUser() throws JsonProcessingException {
        // 创建对象
        User user = new User("若尘", 22);
        // 手动序列化
        String json = mapper.writeValueAsString(user);
        // 写入数据
        stringRedisTemplate.opsForValue().set("user:333", json);
        // 获取数据
        String jsonUser = stringRedisTemplate.opsForValue().get("user:333");
        // 手动反序列化
        User u = mapper.readValue(jsonUser, User.class);
        System.out.println("user => " + u);
    }
}

  • 最后,我们再来测试一下 RedisTemplate 操作 Hash 类型
package com.ruochen;

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.StringRedisTemplate;

import java.util.Map;

@SpringBootTest
class RedisHashTests {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Test
    void testHash() {
        stringRedisTemplate.opsForHash().put("user:444", "name", "ruochen");
        stringRedisTemplate.opsForHash().put("user:444", "age", "22");

        Map<Object, Object> entries = stringRedisTemplate.opsForHash().entries("user:444");
        System.out.println("entries => " + entries);
    }
}

相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
目录
相关文章
|
6天前
|
负载均衡 NoSQL 算法
一天五道Java面试题----第十天(简述Redis事务实现--------->负载均衡算法、类型)
这篇文章是关于Java面试中Redis相关问题的笔记,包括Redis事务实现、集群方案、主从复制原理、CAP和BASE理论以及负载均衡算法和类型。
一天五道Java面试题----第十天(简述Redis事务实现--------->负载均衡算法、类型)
|
17天前
|
NoSQL Java Redis
Spring Boot集成Redis全攻略:高效数据存取,打造性能飞跃的Java微服务应用!
【8月更文挑战第3天】Spring Boot是备受欢迎的微服务框架,以其快速开发与轻量特性著称。结合高性能键值数据库Redis,可显著增强应用性能。集成步骤包括:添加`spring-boot-starter-data-redis`依赖,配置Redis服务器参数,注入`RedisTemplate`或`StringRedisTemplate`进行数据操作。这种集成方案适用于缓存、高并发等场景,有效提升数据处理效率。
71 2
|
23天前
|
消息中间件 Java Kafka
Java 客户端访问kafka
Java 客户端访问kafka
29 9
|
26天前
|
存储 NoSQL Java
Java中使用redis的bitMap实现签到功能
这个实现示例提供了一种灵活、高效的方式,展示了如何使用Redis来解决现实中的问题。
27 2
|
5天前
|
Java
Java模拟文件发送给服务器,服务器将文件转发给其他用户,并保存到服务器本地,其他用户可以接收,并保存到本地磁盘,支持各种文件格式,并解决通信中服务器怎么区分客户端发来的文件类型
Java模拟文件发送给服务器,服务器将文件转发给其他用户,并保存到服务器本地,其他用户可以接收,并保存到本地磁盘,支持各种文件格式,并解决通信中服务器怎么区分客户端发来的文件类型
|
30天前
|
负载均衡 NoSQL Java
|
7天前
|
NoSQL 安全 Java
Java Spring Boot中使用Shiro、JWT和Redis实现用户登录鉴权
Java Spring Boot中使用Shiro、JWT和Redis实现用户登录鉴权
|
3月前
|
JSON NoSQL Java
【Redis】2、Redis 的 Java 客户端(Jedis 和 SpringDataRedis)
【Redis】2、Redis 的 Java 客户端(Jedis 和 SpringDataRedis)
91 0
|
1月前
|
Java Redis 数据安全/隐私保护
Redis14----Redis的java客户端-jedis的连接池,jedis本身是线程不安全的,并且频繁的创建和销毁连接会有性能损耗,最好用jedis连接池代替jedis,配置端口,密码
Redis14----Redis的java客户端-jedis的连接池,jedis本身是线程不安全的,并且频繁的创建和销毁连接会有性能损耗,最好用jedis连接池代替jedis,配置端口,密码
|
1月前
|
Java Redis 数据安全/隐私保护
Redis13的Java客户端-Jedis快速入门,建立连接的写法,ip地址,设置密码密码,选择库的写法
Redis13的Java客户端-Jedis快速入门,建立连接的写法,ip地址,设置密码密码,选择库的写法