package redistest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import redis.clients.jedis.Jedis;
public class test {
private final static Jedis jedis = new Jedis("127.0.0.1",6379);
public static void main(String[] args) {
//add1();
//del();
//append();
//incr();
//add2();
//del2();
//add3();
//add4();
add5();
}
//添加字符串 修改字符串
public static void add1(){
//name-value (键值对)
jedis.set("name", "wangchunyu1");
System.out.println(jedis.get("name"));
}
//删除字符串
public static void del(){
jedis.del("name");
System.out.println(jedis.get("name"));
}
//追加延长字符串
public static void append(){
jedis.append("name", "append");
System.out.println(jedis.get("name"));
}
//加1 incr 减一 decr
public static void incr(){
jedis.set("num", "4");
System.out.println(jedis.incr("num"));
}
//添加Hash 修改hash
public static void add2(){
//Hash 一般第二个为属性 和类最为接近
jedis.hset("users","name", "wangchunyu");
jedis.hset("users","age", "24");
jedis.hset("users", "sex", "男");
Map<String, String> map = jedis.hgetAll("users");
Set<String> sets = map.keySet();
for (String set : sets) {
System.out.println(set+":"+map.get(set));
}
}
//删除hash值
public static void del2(){
jedis.hdel("users", "sex");
Map<String, String> map = jedis.hgetAll("users");
Set<String> sets = map.keySet();
for (String set : sets) {
System.out.println(set+":"+map.get(set));
}
}
//添加list(列表) 可以重复
public static void add3(){
//lpush:左添加 和 rpush:右添加
jedis.lpush("list1", "刘备");
jedis.lpush("list1", "关羽");
jedis.lpush("list1", "张飞");
//lrange:右提取 第二个参数和第三个参数 为index -1时候为取出所有参数
List<String> list = jedis.lrange("list1",0, -1);
for (String each : list) {
System.out.println(each);
}
}
//添加set(集合)重复唯一
public static void add4(){
jedis.sadd("set1", "中国");
jedis.sadd("set1", "美国");
jedis.sadd("set1", "日本");
jedis.sadd("set1", "印度");
Set<String> sets = jedis.smembers("set1");
for (String set : sets) {
System.out.println(set);
}
}
//添加zset(集合)有顺序 值唯一
public static void add5(){
Map<Double, String> map = new HashMap<Double, String>();
map.put(1d, "第一个");
map.put(4d, "第四个");
map.put(3d, "第三个");
map.put(2d, "第二个");
jedis.zadd("zset1", map);
//删除
//jedis.zrem("zset1", "第二个");
Set<String> zsets = jedis.zrange("zset1",0, -1);
for (String set : zsets) {
System.out.println(set);
}
}
}
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import redis.clients.jedis.Jedis;
public class test {
private final static Jedis jedis = new Jedis("127.0.0.1",6379);
public static void main(String[] args) {
//add1();
//del();
//append();
//incr();
//add2();
//del2();
//add3();
//add4();
add5();
}
//添加字符串 修改字符串
public static void add1(){
//name-value (键值对)
jedis.set("name", "wangchunyu1");
System.out.println(jedis.get("name"));
}
//删除字符串
public static void del(){
jedis.del("name");
System.out.println(jedis.get("name"));
}
//追加延长字符串
public static void append(){
jedis.append("name", "append");
System.out.println(jedis.get("name"));
}
//加1 incr 减一 decr
public static void incr(){
jedis.set("num", "4");
System.out.println(jedis.incr("num"));
}
//添加Hash 修改hash
public static void add2(){
//Hash 一般第二个为属性 和类最为接近
jedis.hset("users","name", "wangchunyu");
jedis.hset("users","age", "24");
jedis.hset("users", "sex", "男");
Map<String, String> map = jedis.hgetAll("users");
Set<String> sets = map.keySet();
for (String set : sets) {
System.out.println(set+":"+map.get(set));
}
}
//删除hash值
public static void del2(){
jedis.hdel("users", "sex");
Map<String, String> map = jedis.hgetAll("users");
Set<String> sets = map.keySet();
for (String set : sets) {
System.out.println(set+":"+map.get(set));
}
}
//添加list(列表) 可以重复
public static void add3(){
//lpush:左添加 和 rpush:右添加
jedis.lpush("list1", "刘备");
jedis.lpush("list1", "关羽");
jedis.lpush("list1", "张飞");
//lrange:右提取 第二个参数和第三个参数 为index -1时候为取出所有参数
List<String> list = jedis.lrange("list1",0, -1);
for (String each : list) {
System.out.println(each);
}
}
//添加set(集合)重复唯一
public static void add4(){
jedis.sadd("set1", "中国");
jedis.sadd("set1", "美国");
jedis.sadd("set1", "日本");
jedis.sadd("set1", "印度");
Set<String> sets = jedis.smembers("set1");
for (String set : sets) {
System.out.println(set);
}
}
//添加zset(集合)有顺序 值唯一
public static void add5(){
Map<Double, String> map = new HashMap<Double, String>();
map.put(1d, "第一个");
map.put(4d, "第四个");
map.put(3d, "第三个");
map.put(2d, "第二个");
jedis.zadd("zset1", map);
//删除
//jedis.zrem("zset1", "第二个");
Set<String> zsets = jedis.zrange("zset1",0, -1);
for (String set : zsets) {
System.out.println(set);
}
}
}