package com.buba.R.test;
import java.util.HashSet;
import org.junit.Test;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
public class JedisTest {
// 连接redis的单机版
@Test
public void jedisPool() {
// jedis(ip地址,单机版的端口号)
Jedis jedis = new Jedis("192.168.194.128", 6379);
jedis.set("以为然", "绿光");
System.out.println(jedis.get("以为然"));
jedis.close();
}
@Test
public void del() {
Jedis jedis = new Jedis("192.168.194.128", 6379);
jedis.del("以为然");
jedis.close();
}
// 设置生命周期
@Test
public void expire() {
Jedis jedis = new Jedis("192.168.194.128", 6379);
jedis.set("a", "绿光");
jedis.expire("a", 20);
System.out.println(jedis.get("a"));
jedis.close();
}
@Test
public void get() {
Jedis jedis = new Jedis("192.168.194.128", 6379);
System.out.println(jedis.get("a"));
jedis.close();
}
// 查看生命周期
@Test
public void ttl() {
Jedis jedis = new Jedis("192.168.194.128", 6379);
System.out.println(jedis.ttl("a"));
jedis.close();
}
/**
* 集群版
*/
@Test
public void jedisCluster() {
// 泛型是ip地址加端口号
HashSet<HostAndPort> host = new HashSet<>();
host.add(new HostAndPort("192.168.194.128", 7001));
host.add(new HostAndPort("192.168.194.128", 7002));
host.add(new HostAndPort("192.168.194.128", 7003));
host.add(new HostAndPort("192.168.194.128", 7004));
host.add(new HostAndPort("192.168.194.128", 7005));
host.add(new HostAndPort("192.168.194.128", 7006));
JedisCluster jedisCluster = new JedisCluster(host);
jedisCluster.set("a", "b");
System.out.println(jedisCluster.get("a"));
jedisCluster.close();
}
}
连接redis的单机版、集群版
最新推荐文章于 2023-09-21 18:05:14 发布