码迷,mamicode.com
首页 > 其他好文 > 详细

Redis 5 种数据类型

时间:2021-02-01 12:45:42      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:ping   color   hash   font   div   字符   有序   add   for   

  • 字符串
@GetMapping("/string")
public String stringTest(){
 redisTemplate.opsForValue().set("str","Hello World");
 String str = (String) redisTemplate.opsForValue().get("str");
 return str;
}

 

  • 列表
@GetMapping("/list")
public List<String> listTest(){
 ListOperations<String,String> listOperations = redisTemplate.opsForList();
 listOperations.leftPush("list","Hello");
 listOperations.leftPush("list","World");
 listOperations.leftPush("list","Java");
 List<String> list = listOperations.range("list",0,2);
 return list;
}

 

  • 集合
@GetMapping("/set")
public Set<String> setTest(){
 SetOperations<String,String> setOperations = redisTemplate.opsForSet();
 setOperations.add("set","Hello");
 setOperations.add("set","Hello");
 setOperations.add("set","World");
 setOperations.add("set","World");
 setOperations.add("set","Java");
 setOperations.add("set","Java");
 Set<String> set = setOperations.members("set");
 return set;
}

 

  • 有序集合
@GetMapping("/zset")
public Set<String> zsetTest(){
 ZSetOperations<String,String> zSetOperations = redisTemplate.opsForZSet();
 zSetOperations.add("zset","Hello",1);
 zSetOperations.add("zset","World",2);
 zSetOperations.add("zset","Java",3);
 Set<String> set = zSetOperations.range("zset",0,2);
 return set;
}

 

  • 哈希

HashMap key value

HashOperations key hashkey value

key 是每?组数据的 ID,hashkey 和 value 是?组完整的 HashMap 数据,通过 key 来区分不同的 HashMap。

HashMap hashMap1 = new HashMap();
hashMap1.put(key1,value1);
HashMap hashMap2 = new HashMap();
hashMap2.put(key2,value2);
HashMap hashMap3 = new HashMap();
hashMap3.put(key3,value3);
HashOperations<String,String,String> hashOperations =
redisTemplate.opsForHash();
hashOperations.put(hashMap1,key1,value1);
hashOperations.put(hashMap2,key2,value2);
hashOperations.put(hashMap3,key3,value3);

 

Redis 5 种数据类型

标签:ping   color   hash   font   div   字符   有序   add   for   

原文地址:https://www.cnblogs.com/hanfeizi/p/14352430.html

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