Redis Java版本快速入门

作者: 凯哥Java | 来源:发表于2023-01-30 08:58 被阅读0次

Redis快速入门,分两个客户端:Jedis和SpringDataRedis

ae47afafee6b8c6f753eafea7fb856f7.png

使用Jdedis

1、引入依赖

<pre class="brush:as3;toolbar:false" style="margin: 0.5em 0px; padding: 0.4em 0.6em; border-radius: 8px; background: rgb(248, 248, 248);">
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.7.0</version>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>
</pre>

2、创建测试类:

<pre class="brush:as3;toolbar:false" style="margin: 0.5em 0px; padding: 0.4em 0.6em; border-radius: 8px; background: rgb(248, 248, 248);">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.HashMap;
import java.util.Map;

/**

  • @author 凯哥Java
    */
    public class JedisTest {

    private Jedis jedis;

    @BeforeEach
    public void initJedis(){
    jedis = new Jedis("192.168.50.135",6379);
    }

    @Test
    public void testString(){
    String result = jedis.set("name","kaige");
    System.out.println("set Result"+result);
    String nameValue = jedis.get("name");
    System.out.println("v:"+nameValue);
    }

    @Test
    public void hashTest(){
    Map<String,String> value = new HashMap<>();
    value.put("id","1");
    value.put("name","hset1");
    value.put("age","23");
    Long result = jedis.hset("persion_1",value);
    System.out.println("set Result"+result);
    String age = jedis.hget("persion_1","age");
    System.out.println("age:"+age);
    }
    @AfterEach
    void tearDown() {
    if (jedis != null) {
    jedis.close();
    }
    }

}
</pre>

说明:

如果生产环境就这么使用,会出问题的。jedis是线程不安全的,而且创建、销毁也是很消耗的。所以使用连接池方式:

创建连接池:

<pre class="brush:as3;toolbar:false" style="margin: 0.5em 0px; padding: 0.4em 0.6em; border-radius: 8px; background: rgb(248, 248, 248);">import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**

  • @author 凯哥Java

  • @description jedis连接池
    */
    public class JedisFactory {

    private static final JedisPool jedisPool;

    static {
    JedisPoolConfig poolConfig = new JedisPoolConfig();
    //最大连接数
    poolConfig.setMaxTotal(8);
    //最大空闲连接
    poolConfig.setMaxIdle(8);
    //等待市场
    poolConfig.setMaxWaitMillis(1000);
    //最小空闲连接
    poolConfig.setMinIdle(0);
    jedisPool = new JedisPool(poolConfig,"192.168.50.135",6379);
    }

    public static Jedis getJedis(){
    return jedisPool.getResource();
    }

}
</pre>

使用:

cc9c66871c40a280bcc481fe85953a6a.png

二:springDataRedis

springData介绍:

d4bc5e13b92d57b9064c69cf36a2bb09.png

springDataRedis中提供了RedisTemplate工具类,其中封装了各种对Redis的操作。并且将不同数据类型的操作API封装到了不同类型中:

3767ccf855085245c8f6b69ec61c5a7f.png

spring 默认使用的是lettuce客户端的。如果要使用jedis的话,需要自己引入相关依赖。

使用springdataRedis步骤:

1:引入依赖

<pre class="brush:as3;toolbar:false" style="margin: 0.5em 0px; padding: 0.4em 0.6em; border-radius: 8px; background: rgb(248, 248, 248);">
<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>
</pre>

2:配置Redis

application.yaml文件:

<pre class="brush:as3;toolbar:false" style="margin: 0.5em 0px; padding: 0.4em 0.6em; border-radius: 8px; background: rgb(248, 248, 248);">spring:
redis:
host: 192.168.50.135
port: 6379
lettuce:
pool:
max-active: 8
max-idle: 8
min-idle: 0
max-wait: 100ms
</pre>

3:写测试类:

be8f475a47992a5630fe4da4d39b1307.png

需要注意:在测试类上,一定要写入@RunWith(SpringRunner.class)这个注解。如果不写,会报redisTemplate空指针异常。

执行完成之后,我们到Redis库中查看:

77d8228783454b7cc47dd8b09b86991d.png

为什么存入进去的数据,是乱码的呢?而且,有一个name,是我们自己set的,乱码的这个是我们通过RedisTemplate插入的,我们来看看RedisTemplate源码

RedisTemplate需要设置序列化:

cccaf00acc58fcb9f8c19ac7e3087b53.png

我们跟set源码,会发现,set是使用了value的序列化:

4518ddf00cde8993f01966a2dcd339b5.png

跟着源码,我们知道,默认使用的是jdk自带的序列化工具。

为了解决两个name(一个是正常的name,一个是乱码的name)问题,乱码的缺点:

1:可读性差

2:内存占用较大

我们可以自定义RedisTemplate的序列化方式,代码如下:

<pre class="brush:as3;toolbar:false" style="margin: 0.5em 0px; padding: 0.4em 0.6em; border-radius: 8px; background: rgb(248, 248, 248);">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;

/**

  • @author 凯哥Java
    */
    @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.setHashValueSerializer(jsonRedisSerializer);
    // 返回
    return template;
    }
    }
    </pre>

配置好自定义的之后,再次执行,就可以了。

通过自定义序列化之后,我们在Redis中存入一个user对象:

97565123b146e6327d6c52fd8d9b8ae8.png 233d453949c817b108d2742d3f122a19.png

存入值,我们发现在Redis库中,对象中多了类全路径。

这样有个缺点:增加了额外的内存开销的。那么这个时候怎么办呢?

我们可以规定,在使用String类型的时候,存入对象的是,需要先将对象序列化,然后获取后,在将对象反序列即可。
本文由凯哥Java(kaigejava#com)发布

相关文章

网友评论

    本文标题:Redis Java版本快速入门

    本文链接:https://www.haomeiwen.com/subject/qirmhdtx.html