Redis 5 种数据类型

  • 字符串
@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基础(思维导图)附Redis工具类


下一篇:RedisTemplate报空指针错误