java 使用 redis
- 摘要:一,下载jedis.jar包,并导入路径二,java连接redis基本用法//连接redis服务Jedisjedis=newJedis("192.168.0.225",6400);//验证密码jedis.auth("test");//kv储存jedis.set("redis","redis");//获取jedis.get("redis")//对value进行拼接jedis.append("redis","newredis");//删除keyjedis.del("redis");//添加
- 标签:
一,下载jedis.jar包,并导入路径
二,java连接redis基本用法
//连接redis服务
Jedis jedis = new Jedis("192.168.0.225",6400);
//验证密码
jedis.auth("test");
//kv储存
jedis.set("redis", "redis");
//获取
jedis.get("redis")
//对value进行拼接
jedis.append("redis", "newredis");
//删除key
jedis.del("redis");
//添加,获取多个kv
jedis.mset("name1","yangw","name2","demon","name3","elena");
jedis.mget("name1","name2","name3")
//map
Map<String,String> user = new HashMap<String,String>();
user.put("name", "cd");
user.put("password", "123456");
//map存入redis
jedis.hmset("user", user);
//mapkey个数
System.out.println(String.format("len:%d", jedis.hlen("user")));
//map中的所有键值
System.out.println(String.format("keys: %s", jedis.hkeys("user") ));
//map中的所有value
System.out.println(String.format("values: %s", jedis.hvals("user") ));
//取出map中的name字段值
List<String> rsmap = jedis.hmget("user", "name","password");
System.out.println(rsmap);
//删除map中的某一个键值 password
jedis.hdel("user", "password");
System.out.println(jedis.hmget("user", "name", "password"));
//list
jedis.del("listDemo");
System.out.println(jedis.lrange("listDemo", 0, -1));
jedis.lpush("listDemo", "A");
jedis.lpush("listDemo", "B");
jedis.lpush("listDemo", "C");
System.out.println(jedis.lrange("listDemo", 0, -1));
System.out.println(jedis.lrange("listDemo", 0, 1));
//set
jedis.sadd("sname", "wobby");
jedis.sadd("sname", "kings");
jedis.sadd("sname", "demon");
System.out.println(String.format("set num: %d", jedis.scard("sname")));
System.out.println(String.format("all members: %s", jedis.smembers("sname")));
System.out.println(String.format("is member: %B", jedis.sismember("sname", "wobby")));
System.out.println(String.format("rand member: %s", jedis.srandmember("sname")));
//删除一个对象
jedis.srem("sname", "demon");
System.out.println(String.format("all members: %s", jedis.smembers("sname")));