1. jedis基本使用
(1) maven坐标
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.7.0</version>
</dependency>
(2) 建立连接
@Test
public void testJedis(){
Jedis j = new Jedis("localhost",6379);
j.select(0);
j.set("xjy","xjyyyy");
String xjy = j.get("xjy");
System.out.println(xjy);
j.close();
}
2. jedis连接池
@Test
public void jedisPool(){
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(8);
jedisPoolConfig.setMaxIdle(8);
jedisPoolConfig.setMinIdle(0);
jedisPoolConfig.setMaxWaitMillis(200);
JedisPool jedisPool = new JedisPool(jedisPoolConfig,"localhost",6379);
Jedis j = jedisPool.getResource();
System.out.println(j.get("xjy"));
}
3. springDataRedis
(1) maven坐标
<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>
(2) 配置
spring:
data:
redis:
port: 6379
host: localhost
database: 0
lettuce:
pool:
max-active: 8
max-idle: 8
min-idle: 0
max-wait: 100ms
(3) 测试使用
@SpringBootTest
public class redistest {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void testSpringDataRedis(){
ValueOperations j = redisTemplate.opsForValue();
j.set("xjy","xjyyyy");
Object xjy = j.get("xjy");
System.out.println(xjy);
}
}