RedisTemplate中opsForValue和opsForList方法使用

导读:本篇文章讲解 RedisTemplate中opsForValue和opsForList方法使用,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

一、opsForList用法

key:字符串,value:可以是任意对象Object(例如String、具体对象如自定义类Student等),一个key可以分别先后添加多个value。

redis对list操作分为左和右两种

redisTemplate.opsForList().leftPush() 实际调用的是lPush return
connection.lPush(rawKey, new byte[][]{rawValue});
redisTemplate.opsForList().rightPush() 调用的是rPush
lPush将数据添加到key对应的现有数据的左边,也就是头部,rPush是将现有数据添加到现有数据的右边,也就是尾部,可以根据业务的不同进行对应的添加

//赋值方法
redisTemplate.opsForList().leftPush("testKey","L1");
redisTemplate.opsForList().leftPush("testKey","L2");
redisTemplate.opsForList().leftPush("testKey","L3");
redisTemplate.opsForList().rightPush("testKey","R4");
redisTemplate.opsForList().rightPush("testKey","R5");
redisTemplate.opsForList().rightPush("testKey","R6");
redisTemplate.opsForList().rightPush("testKey","R7");
   L3 L2 L1 R4 R5 R6 R7

二、opsForValue用法
key:字符串,value:可以是任意对象Object(例如String、具体对象如自定义类Student等),一个key只能对应一个value。

//赋值方法
redisTemplate.opsForValue().set("username1","李四");
redisTemplate.opsForValue().set("username2","王五");
redisTemplate.opsForValue().set("username3","赵六");
//取值,返回是String类型
String str1 = redisTemplate.opsForValue().get("username1");
String str2 = redisTemplate.opsForValue().get("username2");
String str3 = redisTemplate.opsForValue().get("username3");
//赋值方法
redisTemplate.opsForValue().set("stu1",new Student(1,"张三",20,"张无忌"));
redisTemplate.opsForValue().set("stu2",new Student(2,"李四",20,"张真人"));
//取值,返回是Student类型
Student stu1 = redisTemplate.opsForValue().get("stu1");
Student stu2 = redisTemplate.opsForValue().get("stu2");

总结:set时value传的是什么类型,get后返回的就是什么类型。

1、leftPush(K key, V value)
  在变量左边添加元素值。
redisTemplate.opsForList().leftPush("list","a");  
redisTemplate.opsForList().leftPush("list","b");  
redisTemplate.opsForList().leftPush("list","c");  
 
 
2、index(K key, long index)
  获取集合指定位置的值。
String listValue = redisTemplate.opsForList().index("list",1) + "";  
System.out.println(listValue);  
 
 
3、range(K key, long start, long end)
  获取指定区间的值。
List<Object> list =  redisTemplate.opsForList().range("list",0,-1);  
System.out.println(list);  
 
 
4、leftPush(K key, V pivot, V value)
  把最后一个参数值放到指定集合的第一个出现中间参数的前面,如果中间参数值存在的话。
redisTemplate.opsForList().leftPush("list","a","n");  
list =  redisTemplate.opsForList().range("list",0,-1);  
System.out.println(list);  
 
 
5、leftPushAll(K key, V... values)
  向左边批量添加参数元素。
redisTemplate.opsForList().leftPushAll("list","w","x","y");  
list =  redisTemplate.opsForList().range("list",0,-1);  
System.out.println(list);  
 
 
6、leftPushAll(K key, Collection<V> values)
   以集合的方式向左边批量添加元素。
List newList = new ArrayList();  
newList.add("o");  
newList.add("p");  
newList.add("q");  
redisTemplate.opsForList().leftPushAll("list",newList);  
list =  redisTemplate.opsForList().range("list",0,-1);  
System.out.println(list);  
 
 
7、leftPushIfPresent(K key, V value)
  如果存在集合则添加元素。
redisTemplate.opsForList().leftPushIfPresent("presentList","o");  
list =  redisTemplate.opsForList().range("presentList",0,-1);  
System.out.println(list);  
 
 
8、rightPush(K key, V value)
   向集合最右边添加元素。
redisTemplate.opsForList().rightPush("list","w");  
list =  redisTemplate.opsForList().range("list",0,-1);  
System.out.println(list);  
 
 
9、rightPush(K key, V pivot, V value)
   向集合中第一次出现第二个参数变量元素的右边添加第三个参数变量的元素值。
redisTemplate.opsForList().rightPush("list","w","r");  
list =  redisTemplate.opsForList().range("list",0,-1);  
System.out.println(list);  
 
 
10、rightPushAll(K key, V... values)
   向右边批量添加元素。
redisTemplate.opsForList().rightPushAll("list","j","k");  
list =  redisTemplate.opsForList().range("list",0,-1);  
System.out.println(list);  
 
 
11、rightPushAll(K key, Collection<V> values)
   以集合方式向右边添加元素。
newList.clear();  
newList.add("g");  
newList.add("h");  
redisTemplate.opsForList().rightPushAll("list",newList);  
list =  redisTemplate.opsForList().range("list",0,-1);  
System.out.println(list);  
 
 
12、rightPushIfPresent(K key, V value)
  向已存在的集合中添加元素。
redisTemplate.opsForList().rightPushIfPresent("presentList","d");  
list =  redisTemplate.opsForList().range("presentList",0,-1);  
System.out.println(list);  
 
 
13、size(K key)
  获取集合长度。
long listLength = redisTemplate.opsForList().size("list");  
System.out.println(listLength);  
 
 
14、leftPop(K key)
  移除集合中的左边第一个元素。
Object popValue = redisTemplate.opsForList().leftPop("list");  
System.out.print("移除的元素是:" + popValue);  
list =  redisTemplate.opsForList().range("list",0,-1);  
System.out.println(",剩余的元素是:" + list);  
 
 
15、leftPop(K key, long timeout, TimeUnit unit)
  移除集合中左边的元素在等待的时间里,如果超过等待的时间仍没有元素则退出。
popValue = redisTemplate.opsForList().leftPop("presentList",1, TimeUnit.SECONDS);  
System.out.print("移除的元素是:" + popValue);  
list =  redisTemplate.opsForList().range("presentList",0,-1);  
System.out.println(",剩余的元素是:" + list);  
 
 
16、rightPop(K key)
  移除集合中右边的元素。
popValue = redisTemplate.opsForList().rightPop("list");  
System.out.print("移除的元素是:" + popValue);  
list =  redisTemplate.opsForList().range("list",0,-1);  
System.out.println(",剩余的元素是:" + list);  
 
 
17、rightPop(K key, long timeout, TimeUnit unit)
  移除集合中右边的元素在等待的时间里,如果超过等待的时间仍没有元素则退出。
popValue = redisTemplate.opsForList().rightPop("presentList",1, TimeUnit.SECONDS);  
System.out.print("移除的元素是:" + popValue);  
list =  redisTemplate.opsForList().range("presentList",0,-1);  
System.out.println(",剩余的元素是:" + list); 
 
 
18、rightPopAndLeftPush(K sourceKey, K destinationKey)
  移除集合中右边的元素,同时在左边加入一个元素。
popValue = redisTemplate.opsForList().rightPopAndLeftPush("list","12");  
System.out.print("移除的元素是:" + popValue);  
list =  redisTemplate.opsForList().range("list",0,-1);  
System.out.println(",剩余的元素是:" + list);  
 
 
19、rightPopAndLeftPush(K sourceKey, K destinationKey, long timeout, TimeUnit unit)
  移除集合中右边的元素在等待的时间里,同时在左边添加元素,如果超过等待的时间仍没有元素则退出。
popValue = redisTemplate.opsForList().rightPopAndLeftPush("presentList","13",1,TimeUnit.SECONDS);  
System.out.println("移除的元素是:" + popValue);  
list =  redisTemplate.opsForList().range("presentList",0,-1);  
System.out.print(",剩余的元素是:" + list); 
 
 
20、set(K key, long index, V value)
  在集合的指定位置插入元素,如果指定位置已有元素,则覆盖,没有则新增,超过集合下标+n则会报错。
redisTemplate.opsForList().set("presentList",3,"15");  
list =  redisTemplate.opsForList().range("presentList",0,-1);  
System.out.print("通过set(K key, long index, V value)方法在指定位置添加元素后:" + list);  
 
 
21、remove(K key, long count, Object value)
  从存储在键中的列表中删除等于值的元素的第一个计数事件。count> 0:删除等于从左到右移动的值的第一个元素;count< 0:删除等于从右到左移动的值的第一个元素;count = 0:删除等于value的所有元素。
long removeCount = redisTemplate.opsForList().remove("list",0,"w");  
list =  redisTemplate.opsForList().range("list",0,-1);  
System.out.println("移除元素数量:" + removeCount);  
System.out.println(",剩余的元素:" + list);  
 
 
22、trim(K key, long start, long end)
  截取集合元素长度,保留长度内的数据。
redisTemplate.opsForList().trim("list",0,5);  
list =  redisTemplate.opsForList().range("list",0,-1);  
System.out.println("截取后剩余元素:" + list); 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/115989.html

(0)
Java光头强的头像Java光头强

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!