Jedis的使用
Jedis是为redis设计的一个java客户端,目的是提高性能与易用性。
目前Jedis
与lettuce
是redis官方推荐使用的java redis客户端
官方文档介绍:https://redis.io/resources/clients/#java
具体使用
Jedis的使用与redis命令类似,在创建redis客户端连接后,执行语句与redis命令一致。
- 导入Jedis的maven依赖
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.3.0</version>
</dependency>
- 连接redis客户端
正常情况会使用Jedis连接池进行连接
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
@SpringBootTest
class JedisTest {
/**
* 基本的连接
*/
@Test
void testJedis1() {
Jedis jedis1 = new Jedis("127.0.0.1",6379);
jedis1.auth("123456");
System.out.println(jedis1.ping());
//关闭连接
jedis1.close();
}
/**
* 使用数据库连接池进行连接
*/
@Test
void testJedis2() {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
//最大连接数, 默认8个
jedisPoolConfig.setMaxTotal(8);
//最大空闲连接数, 默认8个
jedisPoolConfig.setMaxIdle(8);
//最小空闲连接数, 默认0
jedisPoolConfig.setMinIdle(0);
//连接池配置、ip、端口号、连接超时时间、密码、数据库序号
JedisPool jedisPool = new JedisPool(jedisPoolConfig,"127.0.0.1",6379,2000,"123456",0);
Jedis jedis2 = jedisPool.getResource();
System.out.println(jedis2.ping());
jedis2.close();
jedisPool.close();
}
}
- 执行命令
当创建好Jedis对象后,就可以进行对redis进行命令操作了,Jedis提供的方法名与redis命令名一致
Jedis jedis = new Jedis("127.0.0.1",6379);
jedis.xxx();
eg:
set key value [expiration EX seconds|PX milliseconds] [NX|XX]
相当于
jedis.set(xx);
具体Jedis的方法可以参见java API 文档:https://www.javadoc.io/doc/redis.clients/jedis/3.3.0/redis/clients/jedis/Jedis.html
springboot使用Jedis
spring-boot-starter-data-redis有两种实现,分别是lettuce与jedis,默认使用lettuce
1.引入依赖
注意spring-boot-starter-data-redis与Jedis的版本号,发布时间相差比较久远会出现缺依赖的情况
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.6.3</version>
<exclusions>
<exclusion>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.7.0</version>
</dependency>
2.添加常规配置(application.yml)
配置Jedis的连接池,由于Jedis多线程使用同一个连接时,线程不安全,故需要连接池,为每一个Jedis实例分配一个连接
spring:
redis:
host: 192.168.83.130
port: 6379
password: 123456
database: 0
jedis:
pool:
#最大连接数,负值表示没有限制
max-active: 300
#最大阻塞等待时间,负值表示没有限制,默认-1
max-wait: -1
#最大空闲连接数,默认为8
max-idle: 10
#最小空闲连接数,默认为0
min-idle: 1
3.创建配置类用于自定义redisTemplate
需要redis序列化,确定对象以什么方式存入redis数据库
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import javax.annotation.Resource;
@Configuration
@Slf4j
public class RedisConfig {
@Resource
private ObjectMapper mapper;
@Bean
@SuppressWarnings("all")
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
//为了开发方便一般使用<String, Object>
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(factory);
//json序列化配置
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
jackson2JsonRedisSerializer.setObjectMapper(mapper);
//String序列化
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
//key采用String序列化
redisTemplate.setKeySerializer(stringRedisSerializer);
//hash的key采用String序列化
redisTemplate.setHashKeySerializer(stringRedisSerializer);
//value采用json序列化
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
//hash的value采用json序列化
redisTemplate.setHashKeySerializer(jackson2JsonRedisSerializer);
redisTemplate.afterPropertiesSet();
//打印连接的redis信息
JedisConnectionFactory jedisConnectionFactory = (JedisConnectionFactory) factory;
log.info("connection to redis to :{}/{} successfully", jedisConnectionFactory.getHostName(), jedisConnectionFactory.getPort(), jedisConnectionFactory.getDatabase());
return redisTemplate;
}
}
4.测试
配置好redisTemplate就可以正常使用Jedis访问redis了,一般会封装一个redis工具类用于调用不同的redis命令
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.shen.entity.User;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
@RunWith(SpringRunner.class)
@SpringBootTest
class RedisSpringbootApplicationTests {
@Resource
RedisTemplate<String,Object> redisTemplate;
@Test
void contextLoads() throws JsonProcessingException {
User user = new User(001, "小明");
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(user);
redisTemplate.opsForValue().set("test1", "hello");
redisTemplate.opsForValue().set("test2", json);
}
}