1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
| Jedis jedis = new Jedis("127.0.0.1", 6379); jedis.auth("password");
System.out.println("清空数据:"+jedis.flushDB());
System.out.println("判断某个键是否存在:"+jedis.exists("username")); System.out.println("新增<'username','kuangshen'>的键值对:"+jedis.set("username", "kuangshen")); 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所存储的值的类型:"+jedis.type("username")); System.out.println("随机返回key空间的一个:"+jedis.randomKey()); System.out.println("重命名key:"+jedis.rename("username","name")); System.out.println("取出改后的name:"+jedis.get("name")); System.out.println("按索引查询:"+jedis.select(0)); System.out.println("删除当前选择数据库中的所有key:"+jedis.flushDB()); System.out.println("返回当前数据库中key的数目:"+jedis.dbSize()); System.out.println("删除所有数据库中的所有key:"+jedis.flushAll()); 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("key01","key02")); System.out.println("获取多个键值对:"+jedis.mget("key01","key02","key03"));
Map<String,String> map = new HashMap<String,String>(); 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")); System.out.println("散列hash的所有键为:"+jedis.hkeys("hash")); System.out.println("散列hash的所有值为:"+jedis.hvals("hash")); 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"));
JSONObject jsonObject = new JSONObject(); jsonObject.put("hello", "world"); jsonObject.put("name", "java");
Transaction multi = jedis.multi(); String result = jsonObject.toJSONString(); try{ multi.set("json", result); multi.set("json2", result); int i = 100/0; multi.exec(); }catch(Exception e){ e.printStackTrace(); multi.discard(); }finally{ System.out.println(jedis.get("json")); System.out.println(jedis.get("json2")); jedis.close(); }
|