Jedis是redis官方出品的redis java客户端,下面我们在spring boot项目中使用Jedis来操作我们的redis
1.加入Jedis依赖
我是用maven构建的项目,首先加入我们的Jedis依赖如下:
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
2.配置文件加入我们的配置
#redis
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.timeout=300
spring.redis.password=123456
spring.redis.pool.max-idle=500
spring.redis.pool.max-wait=500
spring.redis.pool.max-active=1000
这些参数很简单,我只解释下面三个参数。
max-idle
表示最大空闲数
max-wait
表示在连接池用完的时候阻塞多长时间再抛出异常
max-active
表示最大连接数,但是在Jedis高版本用的是max-total
下面我们用一个RedisConfig类来读取我们的配置文件,代码如下:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@PropertySource("classpath:application.properties")
public class RedisConfig {
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private int port;
@Value("${spring.redis.timeout}")
private int timeout;//秒
@Value("${spring.redis.p