1.Jedis连接
1.1 引入依赖
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.7.1</version>
</dependency>
1.2 建立连接
private Jedis jedis;
@BeforeEach
void setUp() {
jedis = new Jedis("localhost", 6379);
jedis.select(0);
}
1.3 本地测试
@Test
public void testString() {
String result = jedis.set("name", "test");
System.out.println("result: " + result);
String name = jedis.get("name");
System.out.println(name);
}
1.4 释放连接
@AfterEach
void tearDown() {
if (jedis != null) {
jedis.close();
}
}
2.Jdeis连接池
package com.example.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, "localhost", 6379, 1000);
}
public static Jedis getJedis() {
return jedisPool.getResource();
}
}
3.SpringBoot连接
3.1 导入依赖
<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>
3.2 配置properties文件
spring.data.redis.host=127.0.0.1
spring.data.redis.port=6379
# 最大连接
spring.data.redis.lettuce.pool.max-active=8
# 最大空闲连接
spring.data.redis.lettuce.pool.max-idle=8
# 最小连接
spring.data.redis.lettuce.pool.min-idle=0
# 连接等待时间
spring.data.redis.lettuce.pool.max-wait=100
4.JSON序列化
package com.example.redisboot.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<String,Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(connectionFactory);
GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer();
redisTemplate.setKeySerializer(RedisSerializer.string());
redisTemplate.setHashKeySerializer(RedisSerializer.string());
redisTemplate.setValueSerializer(serializer);
redisTemplate.setHashValueSerializer(serializer);
return redisTemplate;
}
}
5.StringRedisTemplate
private static final ObjectMapper objectMapper = new ObjectMapper();
@Test
void testSaveUser() throws JsonProcessingException {
User user = new User("李寒衣", 21);
String useString = objectMapper.writeValueAsString(user);
redisTemplate.opsForValue().set("user:100", useString);
String jsonResult = redisTemplate.opsForValue().get("user:100");
User user1 = objectMapper.readValue(jsonResult, User.class);
System.out.println(user1);
}