<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();
}
}
}
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();
}
}
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
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);
}
}
StringRedisSerializer
,若为 Java 对象,一般使用 GenericJackson2JsonRedisSerializer
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;
}
}
<!--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);
}
}
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);
}
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);
}
}
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);
}
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。