很實用
鏈接在此 https://www.cnblogs.com/edisonfeng/p/3571870.html
System.out.println("======================key=========================="); // 清空數據
System.out.println("清空庫中所有數據:"+jedis.flushDB()); // 判斷key否存在
System.out.println("判斷key999鍵是否存在:"+shardedJedis.exists("key999")); System.out.println("新增key001,value001鍵值對:"+shardedJedis.set("key001", "value001")); System.out.println("判斷key001是否存在:"+shardedJedis.exists("key001")); // 輸出系統中所有的key
System.out.println("新增key002,value002鍵值對:"+shardedJedis.set("key002", "value002")); System.out.println("系統中所有鍵如下:"); Set<String> keys = jedis.keys("*"); Iterator<String> it=keys.iterator() ; while(it.hasNext()){ String key = it.next(); System.out.println(key); } // 刪除某個key,若key不存在,則忽略該命令。
System.out.println("系統中刪除key002: "+jedis.del("key002")); System.out.println("判斷key002是否存在:"+shardedJedis.exists("key002")); // 設置 key001的過期時間
System.out.println("設置 key001的過期時間為5秒:"+jedis.expire("key001", 5)); try{ Thread.sleep(2000); } catch (InterruptedException e){ } // 查看某個key的剩余生存時間,單位【秒】.永久生存或者不存在的都返回-1
System.out.println("查看key001的剩余生存時間:"+jedis.ttl("key001")); // 移除某個key的生存時間
System.out.println("移除key001的生存時間:"+jedis.persist("key001")); System.out.println("查看key001的剩余生存時間:"+jedis.ttl("key001")); // 查看key所儲存的值的類型
System.out.println("查看key所儲存的值的類型:"+jedis.type("key001")); /* * 一些其他方法:1、修改鍵名:jedis.rename("key6", "key0"); * 2、將當前db的key移動到給定的db當中:jedis.move("foo", 1) */ }