码迷,mamicode.com
首页 > 编程语言 > 详细

java操作redis

时间:2019-01-18 17:18:55      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:取数据   sys   数据   print   名称   lse   hle   stat   多个   


static Jedis jedis=new Jedis("localhost");
1 java 操作list
@Test
public void testList(){
jedis.del("javalist");
jedis.lpush("javalist","a");
jedis.lpush("javalist","b","c");
List<String> listOne=jedis.sort("javalist",new SortingParams().alpha().desc()); ////字符串降序排序
listOne.forEach(x->System.out.println(x));

/* List<String> list=jedis.lrange("javalist",0,-1); ////lange 获取list中的值 end为-1 代表查询全部
list.forEach(x->System.out.println(x));*/
//System.out.println("javalist 出栈(左端)"+jedis.lpop("javalist"));
//System.out.println("javalist 出栈(右端)"+jedis.rpop("javalist"));
// System.out.println(jedis.ltrim("javalist",0,1)); //只保留指定区间的数据
System.out.println("修改下标为0 的为k:"+jedis.lset("javalist",0,"k"));
System.out.println("javalist 的长度:"+jedis.llen("javalist"));
System.out.println("javalist 下标0 对应的元素为:"+jedis.lindex("javalist",0));
}
2 java操作set
@Test
public void testSet(){
jedis.sadd("javaset","a","c","d","b");
System.out.println("javaset 所有元素为:"+jedis.smembers("javaset"));
System.out.println("javaset 中包含的元素个数:"+jedis.scard("javaset"));
System.out.println("javaset 中是否包含a:"+jedis.sismember("javaset","a"));
// System.out.println("javaset 中随机移除;"+jedis.spop("javaset"));
//System.out.println("javaset 中移除a,b:"+jedis.srem("javaset","a","b"));
System.out.println(jedis.smembers("javaset"));

jedis.sadd("javaset2","c","d","e","f");
System.out.println("javaset javaset2 的交集:"+jedis.sinter("javaset","javaset2"));
System.out.println("javaset javaset2 的并集:"+jedis.sunion("javaset","javaset2"));
System.out.println("javaset javaset2 的差集:"+jedis.sdiff("javaset","javaset2"));

System.out.println("将javaset 中a删除 并移入javaset2中:"+jedis.smove("javaset","javaset2","a"));
jedis.smembers("javaset").forEach(x->System.out.println(x));
jedis.smembers("javaset2").forEach(x->System.out.println(x));
}
3 java操作HASH
@Test
public void testHash(){
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); //添加名称为hash 的元素
jedis.hset("hash","key5","value5");
System.out.println(jedis.hgetAll("hash"));//获取hash的所有值
System.out.println("keys:"+jedis.hkeys("hash"));//获取hash 对应的所有key
System.out.println("values:"+jedis.hvals("hash"));//获取hash 对应的所有value
System.out.println("将key6 保存的值加上一个整数,如果key6 不存在 则添加key6:"+jedis.hincrBy("hash","key6",6));
System.out.println("将key6 保存的值加上一个整数,如果key6 不存在 则添加key6:"+jedis.hincrBy("hash","key6",6));
System.out.println(jedis.hgetAll("hash"));

System.out.println("删除一个或多个键值对"+jedis.hdel("hash","key2","key3"));
System.out.println("hash 中的个数"+jedis.hlen("hash"));
System.out.println("判断hash中是否存在 key3:"+jedis.hexists("hash","key3"));
System.out.println("获取hash中的值:"+jedis.hmget("hash","key3","key4"));

}
4 java操作String
@Test
public void testString(){
jedis.set("javastring","value1");//增加数据
jedis.set("hello","world");
jedis.set("hello","worlds");//修改数据
System.out.println(jedis.get("hello"));//获取数据
System.out.println(jedis.del("hello"));//删除数据
System.out.println(jedis.append("hello"," ni hao")); //追加
System.out.println("增加多个值:"+jedis.mset("k1","v1","k2","v2"));
System.out.println("获取多个值:"+jedis.mget("k1","k2"));
System.out.println("删除多个值:"+jedis.del(new String[]{"k1","k2"}));
jedis.setnx("key1","kkk");//将 key 的值设为 value ,当且仅当 key 不存在
jedis.setex("key1",2,"sdsds"); //将值 value 关联到 key ,并将 key 的生存时间设为 seconds (以秒为单位)。
System.out.println(jedis.getSet("key1","newKey1")); //于设置指定 key 的值,并返回 key 的旧值

System.out.println(jedis.getrange("key1",1,2)); //截取字符串
}

java操作redis

标签:取数据   sys   数据   print   名称   lse   hle   stat   多个   

原文地址:https://www.cnblogs.com/shujiying/p/10288430.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!