端口怎么设置,看我前一篇文章
前面使用jedis,通过Jedis对象中各种方法来操作redis的。
此处Spring中则是通过StringRedisTemplate来操作redis。
最原始提供的类是RedisTemplate
StringRedisTemplate是RedisTemplate的子类,专门处理文本数据的
这个类提供的方法,相比较于Jedis还是有区别的。
做了进一步封装,得到了专门操作字符串的对象,得到了专门操作xx的对象。初心是为了接口更简单。
字符串常用操作
set-get 设置,获取
有些操作封装来,有些没有封装,比如删库,比如ping
RedisTemplate留了一手,让我们随意执行redis的原操作
execute方法,RedisCallback函数式接口,相当于一个回调函数,就在回调里,写我们要执行的redis命令,这个回调就会被RedisTemplate内部进行执行了。
链表常用操作
leftpop和rightpop,头删,尾删
Set常用操作
Hash常用操作
Zset常用操作
package com.example.demo.Controller;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;import java.util.Set;//后续redis测试的各种功能,都通过这个Controller提供的http接口
@Controller
public class MYController {@Autowiredprivate StringRedisTemplate redisTemplate;@GetMapping("/testString")@ResponseBodypublic String testString() {redisTemplate.opsForValue().set("key", "111");redisTemplate.opsForValue().set("key", "222");redisTemplate.opsForValue().set("key", "333");String value = redisTemplate.opsForValue().get("key");System.out.println("value:" + value);return "ok";}@GetMapping("/testList")@ResponseBodypublic String testList() {//先删库redisTemplate.execute((RedisConnection connection) -> {connection.flushAll();//execute要求回调方法中必须写return,返回个东西//这个回调返回的对象,就会作为execute本身的返回值return null;});redisTemplate.opsForList().leftPush("key", "111");redisTemplate.opsForList().rightPush("key", "222");String value = redisTemplate.opsForList().rightPop("key");System.out.println("value:" + value);value = redisTemplate.opsForList().leftPop("key");System.out.println("value:" + value);return "OK";}@GetMapping("/testSet")@ResponseBodypublic String testSet() {redisTemplate.execute((RedisConnection connecion) -> {connecion.flushAll();return null;});redisTemplate.opsForSet().add("key", "111", "222", "333");Set<String> result = redisTemplate.opsForSet().members("key");System.out.println("result:" + result);boolean exists = redisTemplate.opsForSet().isMember("key", "111");System.out.println("exist:" + exists);Long count = redisTemplate.opsForSet().size("key");System.out.println("count:" + count);redisTemplate.opsForSet().remove("key", "111", "222");result = redisTemplate.opsForSet().members("key");System.out.println("result:" + result);return "Ok";}@GetMapping("/testHash")@ResponseBodypublic String testHash(){redisTemplate.execute((RedisConnection connection)->{connection.flushAll();return null;});redisTemplate.opsForHash().put("key","f1","111");redisTemplate.opsForHash().put("key","f2","222");redisTemplate.opsForHash().put("key","f3","333");String value= (String) redisTemplate.opsForHash().get("key","f1");Boolean exists=redisTemplate.opsForHash().hasKey("key","f1");System.out.println("exists:"+exists);redisTemplate.opsForHash().delete("key","f1","f2");Long size=redisTemplate.opsForHash().size("key");System.out.println("size"+size);return "OK";}@GetMapping("/testZSet")@ResponseBodypublic 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";}
}