package com.itcast.redistest;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
import redis.clients.jedis.Jedis;
public class RedisTest {
@Test
public void redisTest() {
Jedis jedis = new Jedis("127.0.0.1", 6379);
jedis.select(1);//存到第几个数据库中
jedis.set("dab1", "newdb");
/**
* String 类型
*/
/*jedis.set("name", "fujia");
jedis.incr("num");
jedis.incrBy("num", 5);
System.out.println(jedis.get("name"));
jedis.set("value", "2");
jedis.append("value", "11");
jedis.append("num", "ll");
jedis.del("num");
jedis.del("num1");
jedis.del("name");
jedis.del("value");
System.out.println(jedis.get("num"));*/
/**
* hash类型
*/
/*jedis.hset("myhash", "name", "fujia");
Map<String, String> map = new HashMap<String, String>();
map.put("phone", "1238178313");
map.put("age", "18");
map.put("sex", "男");
jedis.hmset("myhashnow", map);
System.out.println(jedis.hgetAll("myhashnow"));
jedis.hdel("myhashnow", "phone1", "phone2");
System.out.println(jedis.hmget("myhashnow", "phone"));
jedis.hincrBy("myhashnow", "age", 10);
System.out.println(jedis.hgetAll("myhashnow"));
System.out.println(jedis.hexists("myhashnow", "kahd"));
System.out.println(jedis.hlen("myhashnow"));
System.out.println(jedis.hkeys("myhashnow"));
System.out.println(jedis.hvals("myhashnow"));
System.out.println(jedis.hgetAll("myhashnow"));
jedis.hdel("myhashnow", "age");
System.out.println(jedis.hlen("myhashnow"));*/
/**
* list类型
*/
/*jedis.lpush("username", "fujia", "zhangsan", "lisi");
jedis.rpush("username", "3", "2", "1");
jedis.del("username");
// -1 代表最后一位
jedis.lpop("username");
jedis.lrem("username", 2, "zhangsan");
jedis.lset("username", 2, "ppp");
jedis.linsert("username", LIST_POSITION.AFTER, "ppp", "hello wolrd");
jedis.lpush("mylist", "q", "w", "e");
jedis.rpoplpush("username", "mylist");
System.out.println(jedis.lrange("username", 0, -1));
System.out.println(jedis.lrange("mylist", 0, -1));*/
/**
* set集合
*/
/*jedis.del("myset");
jedis.sadd("myset", "a", "b", "c");
jedis.srem("myset", "a", "c");
System.out.println(jedis.sismember("myset", "d"));
System.out.println(jedis.smembers("myset"));
// 差集计算
jedis.del("set1");
jedis.del("set2");
jedis.sadd("set1", "a", "b", "c");
jedis.sadd("set2", "e", "b", "c");
System.out.println(jedis.smembers("set1"));
System.out.println(jedis.smembers("set2"));
System.out.println(jedis.sdiff("set2", "set1"));
System.out.println(jedis.sinter("set1", "set2"));
System.out.println(jedis.sunion("set1", "set2"));
System.out.println(jedis.scard("set1"));
System.out.println(jedis.srandmember("set1"));
jedis.sunionstore("setunion", "set1", "set2");
jedis.sdiffstore("setdiff", "set1", "set2");
System.out.println(jedis.smembers("setunion"));
System.out.println(jedis.smembers("setdiff"));*/
/**
* zset集合
*/
/*Map<String, Double> scoreMembers = new HashMap<String, Double>();
scoreMembers.put("iphone", 1000d);
scoreMembers.put("huawei", 980d);
scoreMembers.put("zhongxing", 1980d);
scoreMembers.put("xiaomi", 870d);
jedis.zadd("myzset", scoreMembers);
System.out.println(jedis.zrange("myzset", 0, -1));*/
}
}
另外附上redis可视化工具一份~
链接:https://pan.baidu.com/s/16sKXnbMgkFrqeOFz0MPhgg
提取码:3pte