如何在Spring中操作redis
在创建springboot项目的时候引入redis的依赖.
在配置文件里指定redis主机的地址和端口,此处我们配置了ssh隧道,所以连接的就是本机的8888端口.
创建一个controller类,注入操作redis的对象.
前面使用jedis,是通过jedis对象里的各种方法来操作redis的,此处的Spring则是通过StringRedisTemplate来操作redis.
最原始提供的操作redis的类是RedisTemplate.而StringRedisTemplate是RedisTemplate的子类,是专门用来处理文本数据的.
StringRedisTemplate提供的方法相对于之前jedis中的各种方法,还是存在较大差异的.和原生的命令不太一样.
此处StringRedisTemplate把这些操作redis的方法,又按照数据类型的不同,分成了几个大类别,分门别类的来组织这些命令.相当于又做了进一步的封装,不像jedis那样能点出所有类型的方法.
在这里你要使用一个类型的命令,就必须点出专门操作此数据结构的对象来,然后在点,出现的方法就是具体的命令了.
RedisTemplate没有封装所有的redis命令,所以为了让我们能够执行redis的原生命令,提供了一个execute方法.
相当于一个回调函数,在回调里写我们的redis命令,这个回调就会被RedisTemplate内部进行执行.
这里的RedisConnection就代表了redis连接,对标jedis对象.
Spring操作Redis命令演示
package com.example.demo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.connection.RedisConnection; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.ZSetOperations; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.Map; import java.util.Set; @RestController public class RedisController { @Autowired private StringRedisTemplate redisTemplate; @RequestMapping("/testString") public String testString(){ redisTemplate.execute((RedisConnection redisConnection)->{ // execute 要求回调方法中必须写 return 语句. 返回个东西. // 这个回调返回的对象, 就会作为 execute 本身的返回值. redisConnection.flushAll(); return null; }); redisTemplate.opsForValue().set("key1","111"); redisTemplate.opsForValue().set("key2","222"); redisTemplate.opsForValue().set("key3","333"); String value = redisTemplate.opsForValue().get("key1"); System.out.println("value:"+value); String value2 = redisTemplate.opsForValue().get("key1"); System.out.println("value2:"+value2); String value3 = redisTemplate.opsForValue().get("key1"); System.out.println("value3:"+value3); return "OK"; } @RequestMapping("/testHash") public String testHash(){ redisTemplate.execute((RedisConnection redisConnection)->{ redisConnection.flushAll(); return null; }); redisTemplate.opsForHash().put("k1","f1","v1"); redisTemplate.opsForHash().put("k1","f2","v2"); redisTemplate.opsForHash().put("k1","f3","v3"); Map<Object, Object> getall = redisTemplate.opsForHash().entries("k1"); System.out.println(getall); return "ok"; } @RequestMapping("/testList") public String testList(){ redisTemplate.execute((RedisConnection redisConnection)->{ redisConnection.flushAll(); return null; }); redisTemplate.opsForList().leftPushAll("k1","111","222","333"); String result = redisTemplate.opsForList().rightPop("k1"); System.out.println(result); return "ok"; } @RequestMapping("/testSet") public String testSet(){ redisTemplate.execute((RedisConnection redisConnection)->{ redisConnection.flushAll(); return null; }); redisTemplate.opsForSet().add("k1","111","222","333"); Set<String> set = redisTemplate.opsForSet().members("k1"); Long length = redisTemplate.opsForSet().size("k1"); System.out.println("length:"+length); System.out.println(set); return "ok"; } @RequestMapping("/testZSet") public String testZSet(){ redisTemplate.execute((RedisConnection connection)->{ connection.flushAll(); return null; }); redisTemplate.opsForZSet().add("key", "zhangsan", 10); redisTemplate.opsForZSet().add("key", "lisi", 20); redisTemplate.opsForZSet().add("key", "wangwu", 30); Set<String> members = redisTemplate.opsForZSet().range("key", 0, -1); System.out.println("members: " + members); Set<ZSetOperations.TypedTuple<String>> membersWithScore = redisTemplate.opsForZSet().rangeWithScores("key", 0, -1); System.out.println("membersWithScore: " + membersWithScore); Double score = redisTemplate.opsForZSet().score("key", "zhangsan"); System.out.println("score: " + score); redisTemplate.opsForZSet().remove("key", "zhangsan"); Long size = redisTemplate.opsForZSet().size("key"); System.out.println("size: " + size); Long rank = redisTemplate.opsForZSet().rank("key", "lisi"); System.out.println("rank: " + rank); return "OK"; } }