import redis.clients.jedis.BinaryClient;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public class test01 {
public static void main(String[] args) {
//0.设置数据库连接池
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
//设置连接池的最大数量
jedisPoolConfig.setMaxTotal(50);
//设置最大空闲数量
jedisPoolConfig.setMaxIdle(10);
//设置最小的空闲的数量
jedisPoolConfig.setMinIdle(5);
//连接超时时间(最大等待的时间)
jedisPoolConfig.setMaxWaitMillis(30000);
//1.获取Redis连接池
JedisPool jedisPool = new JedisPool("node01", 6379);
//2.获取Redis链接对象
Jedis jedis = jedisPool.getResource();
//3.通过对象调用API
//清空
jedis.flushAll();
// 使用List实现以下功能(javaAPI)
//练习:
System.out.println("---------------第一题--------------");
// 1、创建两个list 名字分别为ltest1 ltest2
// 2、Ltest1 从左到右为1 2 3 4 5 6 7 8 9,ltest2 从左到右为 f e d c b a
jedis.lpush("ltest1","1","2","3","4","5","6","7","8","9");
jedis.lpush("ltest2","f","e","d","c","b","a");
// 3、在Ltest1的3 左边插入3333
jedis.linsert("ltest1",BinaryClient.LIST_POSITION.BEFORE,"3","3333");
// 4、在6右边插入6666
jedis.linsert("ltest2",BinaryClient.LIST_POSITION.AFTER,"6","6666");
// 5、通过索引查询Ltest2 索引为3的数据
String data = jedis.lindex("ltest2", 3);
System.out.println("查询Ltest2 索引为3的数据是:"+data);
// 6、将ltest2的e 修改为EEE
jedis.lset("ltest2",1,"EEE");
// 7、只保留ltest2的EEE d c b
jedis.ltrim("ltest2",1,4);
// 8、移除ltest1右边一个数据并插入ltest2的左边
jedis.rpoplpush("ltest1","ltest2");
//查看五个set集合
System.out.println("ltest1:"+jedis.lrange("ltest1", 0, -1));
System.out.println("ltest2:"+jedis.lrange("ltest2", 0, -1));
//使用Set实现以下功能(javaAPI)
System.out.println("---------------第二题--------------");
//练习:添加set 集合 setdemo1(aa,bb,cc,dd,ee,ff) setdemo2(11,22,33,dd,ee,ff)
jedis.sadd("setdemo1","aa","bb","cc","dd","ee","ff");
jedis.sadd("setdemo2","11","22","33","dd","ee","ff");
// 将两个集合的交集写入setdemo3
jedis.sinterstore("setdemo3","setdemo1","setdemo2");
// 将两个集合的并集写入setdemo4
jedis.sunionstore("setdemo4","setdemo1","setdemo2");
// 将setdemo2集合与setdemo1集合的差集写入setdemo5
jedis.sdiffstore("setdemo5","setdemo1","setdemo2");
// 将setdemo2内的11 移动到setdemo1内
jedis.smove("setdemo2","setdemo1","11");
// 删除setdemo1内的bb
jedis.srem("setdemo1","bb");
System.out.println("setdemo1:"+jedis.smembers("setdemo1"));
System.out.println("setdemo2:"+jedis.smembers("setdemo2"));
System.out.println("setdemo3:"+jedis.smembers("setdemo3"));
System.out.println("setdemo4:"+jedis.smembers("setdemo4"));
System.out.println("setdemo5:"+jedis.smembers("setdemo5"));
}
}