使用Jedis

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>RedisDemo</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.8.0</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>

</project>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package org.example;

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

public class Main {
private Jedis jedis;

@BeforeEach
void setUp(){
jedis = new Jedis("**.**.**.**",6379);
jedis.auth("123");
jedis.select(0);
}

@Test
void test(){
var a = jedis.get("name");
System.out.println(a);
}


public static void main(String[] args) {
System.out.println("Hello world!");
}
}

至此,为基本使用。

Jedis的连接池

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package org.example;

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

import java.time.Duration;

public class JedisConnectionFactory {
private static final JedisPool jedisPool;
static {
// 配置
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
// 最大连接数
jedisPoolConfig.setMaxTotal(8);
// 最大空闲数
jedisPoolConfig.setMaxIdle(8);;
jedisPoolConfig.setMinIdle(0);
// 最长等待时间
jedisPoolConfig.setMaxWait(Duration.ofMillis(200));
jedisPool = new JedisPool(jedisPoolConfig,"**.**.**.**",6379,1000,"123",0);
}

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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;

public class Main {
private Jedis jedis;

@BeforeEach
void setUp(){
jedis = JedisConnectionFactory.getJedis();
}

@AfterEach
void tearDown(){
if(jedis!=null){
jedis.close();
}
}


@Test
void testPool(){
var a = jedis.get("hunan:name");
System.out.println(a);
}


public static void main(String[] args) {
System.out.println("Hello world!");
}
}

使用SpringDataRedis

在里面提供了RedisTemplate工具类,封装Redis的操作,如APIredisTemplate.opsForValue()返回ValueOperations来操作String类型。

在创建的时候勾选SpringDataRedis

pom.xml文件如下:

1
2
3
4
5
6
7
8
<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>

配置如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
spring:
data:
redis:
host: **.**.**.**
port: 6379
password: 123
database: 0
lettuce:
pool:
max-idle: 8
max-active: 8
min-idle: 0
max-wait: 100
logging:
level:
org.springframework.data.redis: DEBUG

解决序列化因素-自定义RedisTemplate

避免在序列化时候错误序列化即你存的和实际存的不一样,可以创建一个返回redisTemplate的Bean,其中Key使用String序列化对象,Value使用Json序列化对象。

首先添加jackson以来到pom.xml:

1
2
3
4
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfiguration {

@Bean
public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
redisTemplate.setKeySerializer(RedisSerializer.string());
redisTemplate.setHashKeySerializer(RedisSerializer.string());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
return redisTemplate;
}
}

使用自带的StringRedisTemplate

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.example.redisdemo.pojo;

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

@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private String name;
private Integer age;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Autowired
private StringRedisTemplate stringRedisTemplate;
private static final ObjectMapper mapper = new ObjectMapper();
@Test
void testObject2() throws JsonProcessingException {
User china = new User("china", 5000);
String string = mapper.writeValueAsString(china);
stringRedisTemplate.opsForValue().set("user",string);

string = stringRedisTemplate.opsForValue().get("user");
china = mapper.readValue(string,User.class);
System.out.println(china);
}

用于测试用的文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.example.redisdemo;

import com.example.redisdemo.pojo.User;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.annotation.Resource;
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 org.springframework.data.redis.core.StringRedisTemplate;

@SpringBootTest
class RedisDemoApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
@Test
void testString() throws InterruptedException {
System.out.println(redisTemplate);
var name = redisTemplate.opsForValue();
System.out.println(name);
name.set("asd","asd");
}

@Test
void testObject1(){
redisTemplate.opsForValue().set("user",new User("lnpbqc",18));
}

@Autowired
private StringRedisTemplate stringRedisTemplate;
private static final ObjectMapper mapper = new ObjectMapper();
@Test
void testObject2() throws JsonProcessingException {
User china = new User("china", 5000);
String string = mapper.writeValueAsString(china);
stringRedisTemplate.opsForValue().set("user",string);

string = stringRedisTemplate.opsForValue().get("user");
china = mapper.readValue(string,User.class);
System.out.println(china);
}

}