关于java:jedis测试

43次阅读

共计 9509 个字符,预计需要花费 24 分钟才能阅读完成。

键操作

@Test public void testKey() throws InterruptedException
{System.out.println("清空数据:"+jedis.flushDB());
    System.out.println("判断某个键是否存在:"+jedis.exists("username"));
    System.out.println("新增 <'username','zzh'> 的键值对:"+jedis.set("username", "zzh"));
    System.out.println(jedis.exists("name"));
    System.out.println("新增 <'password','password'> 的键值对:"+jedis.set("password", "password"));
    System.out.print("零碎中所有的键如下:");
    Set<String> keys = jedis.keys("*");
    System.out.println(keys);
    System.out.println("删除键 password:"+jedis.del("password"));
    System.out.println("判断键 password 是否存在:"+jedis.exists("password"));
    System.out.println("设置键 username 的过期工夫为 5s:"+jedis.expire("username", 5));
    TimeUnit.SECONDS.sleep(2);
    System.out.println("查看键 username 的残余生存工夫:"+jedis.ttl("username"));
    System.out.println("移除键 username 的生存工夫:"+jedis.persist("username"));
    System.out.println("查看键 username 的残余生存工夫:"+jedis.ttl("username"));
    System.out.println("查看键 username 所存储的值的类型:"+jedis.type("username"));
}

字节串

@Test public void testString() throws InterruptedException
{jedis.flushDB();
    System.out.println("=========== 减少数据 ===========");
    System.out.println(jedis.set("key1","value1"));
    System.out.println(jedis.set("key2","value2"));
    System.out.println(jedis.set("key3", "value3"));
    System.out.println("删除键 key2:"+jedis.del("key2"));
    System.out.println("获取键 key2:"+jedis.get("key2"));
    System.out.println("批改 key1:"+jedis.set("key1", "value1Changed"));
    System.out.println("获取 key1 的值:"+jedis.get("key1"));
    System.out.println("在 key3 前面退出值:"+jedis.append("key3", "End"));
    System.out.println("key3 的值:"+jedis.get("key3"));
    System.out.println("减少多个键值对:"+jedis.mset("key01","value01","key02","value02","key03","value03"));
    System.out.println("获取多个键值对:"+jedis.mget("key01","key02","key03"));
    System.out.println("获取多个键值对:"+jedis.mget("key01","key02","key03","key04"));
    System.out.println("删除多个键值对:"+jedis.del(new String[]{"key01","key02"}));
    System.out.println("获取多个键值对:"+jedis.mget("key01","key02","key03"));
 
    jedis.flushDB();
    System.out.println("=========== 新增键值对避免笼罩原先值 ==============");
    System.out.println(jedis.setnx("key1", "value1"));
    System.out.println(jedis.setnx("key2", "value2"));
    System.out.println(jedis.setnx("key2", "value2-new"));
    System.out.println(jedis.get("key1"));
    System.out.println(jedis.get("key2"));
 
    System.out.println("=========== 新增键值对并设置无效工夫 =============");
    System.out.println(jedis.setex("key3", 2, "value3"));
    System.out.println(jedis.get("key3"));
    TimeUnit.SECONDS.sleep(3);
    System.out.println(jedis.get("key3"));
 
    System.out.println("=========== 获取原值,更新为新值 ==========");//GETSET is an atomic set this value and return the old value command.
    System.out.println(jedis.getSet("key2", "key2GetSet"));
    System.out.println(jedis.get("key2"));
 
    System.out.println("取得 key2 的值的字串:"+jedis.getrange("key2", 2, 4));
}

整数和浮点数

@Test public void testNumber()
{jedis.flushDB();
    jedis.set("key1", "1");
    jedis.set("key2", "2");
    jedis.set("key3", "2.3");
    System.out.println("key1 的值:"+jedis.get("key1"));
    System.out.println("key2 的值:"+jedis.get("key2"));
    System.out.println("key1 的值加 1:"+jedis.incr("key1"));
    System.out.println("获取 key1 的值:"+jedis.get("key1"));
    System.out.println("key2 的值减 1:"+jedis.decr("key2"));
    System.out.println("获取 key2 的值:"+jedis.get("key2"));
    System.out.println("将 key1 的值加上整数 5:"+jedis.incrBy("key1", 5));
    System.out.println("获取 key1 的值:"+jedis.get("key1"));
    System.out.println("将 key2 的值减去整数 5:"+jedis.decrBy("key2", 5));
    System.out.println("获取 key2 的值:"+jedis.get("key2"));
}

list 列表

@Test public void testList() 
{jedis.flushDB(); 
System.out.println(“=========== 增加一个 list===========”); 
jedis.lpush(“collections”,“ArrayList”,“Vector”,“Stack”,“HashMap”,“WeakHashMap”,“LinkedHashMap”); 
jedis.lpush(“collections”,“HashSet”); 
jedis.lpush(“collections”,“TreeSet”); 
jedis.lpush(“collections”,“TreeMap”); 
System.out.println(“collections 的内容:”+jedis.lrange(“collections”, 0, -1));//- 1 代表倒数第一个元素,- 2 代表倒数第二个元素 
System.out.println(“collections 区间 0 - 3 的元素:”+jedis.lrange(“collections”,0,3)); 
System.out.println(“===============================”); 
// 删除列表指定的值,第二个参数为删除的个数(有反复时),后 add 进去的值先被删,相似于出栈 
System.out.println(“删除指定元素个数:”+jedis.lrem(“collections”, 2,“HashMap”)); 
System.out.println(“collections 的内容:”+jedis.lrange(“collections”, 0, -1)); 
System.out.println(“删除下表 0 - 3 区间之外的元素:”+jedis.ltrim(“collections”, 0, 3)); 
System.out.println(“collections 的内容:”+jedis.lrange(“collections”, 0, -1)); 
System.out.println(“collections 列表出栈(左端):”+jedis.lpop(“collections”)); 
System.out.println(“collections 的内容:”+jedis.lrange(“collections”, 0, -1)); 
System.out.println(“collections 增加元素,从列表右端,与 lpush 绝对应:”+jedis.rpush(“collections”,“EnumMap”)); 
System.out.println(“collections 的内容:”+jedis.lrange(“collections”, 0, -1)); 
System.out.println(“collections 列表出栈(右端):”+jedis.rpop(“collections”)); 
System.out.println(“collections 的内容:”+jedis.lrange(“collections”, 0, -1)); 
System.out.println(“批改 collections 指定下标 1 的内容:”+jedis.lset(“collections”, 1,“LinkedArrayList”)); 
System.out.println(“collections 的内容:”+jedis.lrange(“collections”, 0, -1)); 
System.out.println(“===============================”); 
System.out.println(“collections 的长度:”+jedis.llen(“collections”)); 
System.out.println(“获取 collections 下标为 2 的元素:”+jedis.lindex(“collections”, 2)); 
System.out.println(“===============================”); 
jedis.lpush(“sortedList”,“3”,”6”,”2”,”0”,”7”,”4”); 
System.out.println(“sortedList 排序前:”+jedis.lrange(“sortedList”, 0, -1)); 
System.out.println(jedis.sort(“sortedList”)); 
System.out.println(“sortedList 排序后:”+jedis.lrange(“sortedList”, 0, -1)); 
} 

Set

@Test public void testSet()
{jedis.flushDB();
    System.out.println("============ 向汇合中增加元素 ============");
    System.out.println(jedis.sadd("eleSet", "e1","e2","e4","e3","e0","e8","e7","e5"));
    System.out.println(jedis.sadd("eleSet", "e6"));
    System.out.println(jedis.sadd("eleSet", "e6"));
    System.out.println("eleSet 的所有元素为:"+jedis.smembers("eleSet"));
    System.out.println("删除一个元素 e0:"+jedis.srem("eleSet", "e0"));
    System.out.println("eleSet 的所有元素为:"+jedis.smembers("eleSet"));
    System.out.println("删除两个元素 e7 和 e6:"+jedis.srem("eleSet", "e7","e6"));
    System.out.println("eleSet 的所有元素为:"+jedis.smembers("eleSet"));
    System.out.println("随机的移除汇合中的一个元素:"+jedis.spop("eleSet"));
    System.out.println("随机的移除汇合中的一个元素:"+jedis.spop("eleSet"));
    System.out.println("eleSet 的所有元素为:"+jedis.smembers("eleSet"));
    System.out.println("eleSet 中蕴含元素的个数:"+jedis.scard("eleSet"));
    System.out.println("e3 是否在 eleSet 中:"+jedis.sismember("eleSet", "e3"));
    System.out.println("e1 是否在 eleSet 中:"+jedis.sismember("eleSet", "e1"));
    System.out.println("e1 是否在 eleSet 中:"+jedis.sismember("eleSet", "e5"));
    System.out.println("=================================");
    System.out.println(jedis.sadd("eleSet1", "e1","e2","e4","e3","e0","e8","e7","e5"));
    System.out.println(jedis.sadd("eleSet2", "e1","e2","e4","e3","e0","e8"));
    System.out.println("将 eleSet1 中删除 e1 并存入 eleSet3 中:"+jedis.smove("eleSet1", "eleSet3", "e1"));
    System.out.println("将 eleSet1 中删除 e2 并存入 eleSet3 中:"+jedis.smove("eleSet1", "eleSet3", "e2"));
    System.out.println("eleSet1 中的元素:"+jedis.smembers("eleSet1"));
    System.out.println("eleSet3 中的元素:"+jedis.smembers("eleSet3"));
    System.out.println("============ 汇合运算 =================");
    System.out.println("eleSet1 中的元素:"+jedis.smembers("eleSet1"));
    System.out.println("eleSet2 中的元素:"+jedis.smembers("eleSet2"));
    System.out.println("eleSet1 和 eleSet2 的交加:"+jedis.sinter("eleSet1","eleSet2"));
    System.out.println("eleSet1 和 eleSet2 的并集:"+jedis.sunion("eleSet1","eleSet2"));
    System.out.println("eleSet1 和 eleSet2 的差集:"+jedis.sdiff("eleSet1","eleSet2"));//eleSet1 中有,eleSet2 中没有
}

散列 Map

@Test public void testHash()
{jedis.flushDB();
    Map<String,String> map = new HashMap<>();
    map.put("key1","value1");
    map.put("key2","value2");
    map.put("key3","value3");
    map.put("key4","value4");
    jedis.hmset("hash",map);
    jedis.hset("hash", "key5", "value5");
    System.out.println("散列 hash 的所有键值对为:"+jedis.hgetAll("hash"));//return Map<String,String>
    System.out.println("散列 hash 的所有键为:"+jedis.hkeys("hash"));//return Set<String>
    System.out.println("散列 hash 的所有值为:"+jedis.hvals("hash"));//return List<String>
    System.out.println("将 key6 保留的值加上一个整数,如果 key6 不存在则增加 key6:"+jedis.hincrBy("hash", "key6", 6));
    System.out.println("散列 hash 的所有键值对为:"+jedis.hgetAll("hash"));
    System.out.println("将 key6 保留的值加上一个整数,如果 key6 不存在则增加 key6:"+jedis.hincrBy("hash", "key6", 3));
    System.out.println("散列 hash 的所有键值对为:"+jedis.hgetAll("hash"));
    System.out.println("删除一个或者多个键值对:"+jedis.hdel("hash", "key2"));
    System.out.println("散列 hash 的所有键值对为:"+jedis.hgetAll("hash"));
    System.out.println("散列 hash 中键值对的个数:"+jedis.hlen("hash"));
    System.out.println("判断 hash 中是否存在 key2:"+jedis.hexists("hash","key2"));
    System.out.println("判断 hash 中是否存在 key3:"+jedis.hexists("hash","key3"));
    System.out.println("获取 hash 中的值:"+jedis.hmget("hash","key3"));
    System.out.println("获取 hash 中的值:"+jedis.hmget("hash","key3","key4"));
}

有序汇合 Zset

@Test public void testSortedSet()
{jedis.flushDB();
    Map<Double,String> map = new HashMap<>();
    map.put(1.2,"key2");
    map.put(4.0, "key3");
    map.put(5.0,"key4");
    map.put(0.2,"key5");
    System.out.println(jedis.zadd("zset", 3,"key1"));
    System.out.println(jedis.zadd("zset",map));
    System.out.println("zset 中的所有元素:"+jedis.zrange("zset", 0, -1));
    System.out.println("zset 中的所有元素:"+jedis.zrangeWithScores("zset", 0, -1));
    System.out.println("zset 中的所有元素:"+jedis.zrangeByScore("zset", 0,100));
    System.out.println("zset 中的所有元素:"+jedis.zrangeByScoreWithScores("zset", 0,100));
    System.out.println("zset 中 key2 的分值:"+jedis.zscore("zset", "key2"));
    System.out.println("zset 中 key2 的排名:"+jedis.zrank("zset", "key2"));
    System.out.println("删除 zset 中的元素 key3:"+jedis.zrem("zset", "key3"));
    System.out.println("zset 中的所有元素:"+jedis.zrange("zset", 0, -1));
    System.out.println("zset 中元素的个数:"+jedis.zcard("zset"));
    System.out.println("zset 中分值在 1 - 4 之间的元素的个数:"+jedis.zcount("zset", 1, 4));
    System.out.println("key2 的分值加上 5:"+jedis.zincrby("zset", 5, "key2"));
    System.out.println("key3 的分值加上 4:"+jedis.zincrby("zset", 4, "key3"));
    System.out.println("zset 中的所有元素:"+jedis.zrange("zset", 0, -1));
}

正文完
 0