Springboot中RedisTemplate的操作
Springboot中RedisTemplate的操作
1 @Autowired 2 private RedisTemplate redisTemplate; 3 @Autowired 4 private StringRedisTemplate template;
1.RedisTemplate储存set
1 /** 2 * redis储存set 3 */ 4 @RequestMapping("/set") 5 public void redisSet(){ 6 Set<String> set=new HashSet<String>(); 7 set.add("111"); 8 set.add("222"); 9 set.add("333"); 10 redisTemplate.opsForSet().add("set",set); 11 Set<String> resultSet =redisTemplate.opsForSet().members("set"); 12 System.out.println("resultSet:"+resultSet); 13 }
2.RedisTemplated储存map
1 /** 2 * redis储存map 3 */ 4 @RequestMapping("/map") 5 public void redisMap(){ 6 Map<String,String> map=new HashMap<String,String>(); 7 map.put("111","111"); 8 map.put("222","222"); 9 map.put("333","333"); 10 map.put("444","444"); 11 map.put("555","555"); 12 redisTemplate.opsForHash().putAll("map",map); 13 Map<String,String> resultMap= redisTemplate.opsForHash().entries("map"); 14 List<String> reslutMapList=redisTemplate.opsForHash().values("map"); 15 Set<String>resultMapSet=redisTemplate.opsForHash().keys("map"); 16 String value=(String)redisTemplate.opsForHash().get("map","111"); 17 System.out.println("value:"+value); 18 System.out.println("resultMapSet:"+resultMapSet); 19 System.out.println("resultMap:"+resultMap); 20 System.out.println("resulreslutMapListtMap:"+reslutMapList); 21 }
3.RedisTemplate储存list
1 /** 2 * redis储存list 3 */ 4 @RequestMapping("/list") 5 public void redisList(){ 6 List<String> list1=new ArrayList<String>(); 7 list1.add("111"); 8 list1.add("222"); 9 list1.add("333"); 10 11 List<String> list2=new ArrayList<String>(); 12 list2.add("444"); 13 list2.add("555"); 14 list2.add("666"); 15 redisTemplate.opsForList().leftPush("list1",list1); 16 redisTemplate.opsForList().rightPush("list2",list2); 17 List<String> resultList1=(List<String>)redisTemplate.opsForList().leftPop("list1"); 18 List<String> resultList2=(List<String>)redisTemplate.opsForList().rightPop("list2"); 19 System.out.println("resultList1:"+resultList1); 20 System.out.println("resultList2:"+resultList2); 21 }
4.RedisTemplate储存key-value
1 /** 2 * redis储存key-value 3 */ 4 @RequestMapping("/key/value") 5 public void redisKeyValue(){ 6 System.out.println("缓存正在设置。。。。。。。。。"); 7 redisTemplate.opsForValue().set("111","111"); 8 redisTemplate.opsForValue().set("222","222"); 9 redisTemplate.opsForValue().set("333","333"); 10 redisTemplate.opsForValue().set("444","444"); 11 System.out.println("缓存已经设置完毕。。。。。。。"); 12 String result1=redisTemplate.opsForValue().get("111").toString(); 13 String result2=redisTemplate.opsForValue().get("222").toString(); 14 String result3=redisTemplate.opsForValue().get("333").toString(); 15 System.out.println("缓存结果为:result:"+result1+" "+result2+" "+result3); 16 }
5.RedisTemplate储存对象
1 /** 2 * redis储存对象 3 */ 4 @RequestMapping("/Object") 5 public void redisObject(){ 6 User user = new User(); 7 user.setId(11); 8 user.setName("yi"); 9 user.setPhone("123456"); 10 user.setAge(22); 11 redisTemplate.opsForValue().set("user", user); 12 User users = (User) redisTemplate.opsForValue().get("user"); 13 System.out.println(users); 14 }
public class User implements Serializable{ ... }
6.StringRedisTemplate储存对象
1 /** 2 * 存储json字符串对象和取出 3 */ 4 @RequestMapping("/template") 5 public void StringRedisTemplate(){ 6 User user = new User(); 7 user.setId(11); 8 user.setName("yi"); 9 user.setPhone("123456"); 10 user.setAge(22); 11 template.opsForValue().set("user", JSON.toJSONString(user)); 12 String str = template.opsForValue().get("user"); 13 User userName = JSON.parseObject(str, User.class); 14 System.out.println(userName); 15 }
程序员的眼里,不止有代码和bug,还有诗与远方和妹子!!!