1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
| 3. 使用RedisTemplate 3.1: 测试类 package com.sky.test; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.redis.connection.DataType; import org.springframework.data.redis.core.*; import java.util.List; import java.util.Set; import java.util.concurrent.TimeUnit;
@SpringBootTest public class SpringDataRedisTest { @Autowired private RedisTemplate redisTemplate;
@Test public void setRedisTemplate(){ System.out.println(redisTemplate); ListOperations listOperations = redisTemplate.opsForList(); ValueOperations valueOperations = redisTemplate.opsForValue(); HashOperations hashOperations = redisTemplate.opsForHash(); SetOperations setOperations = redisTemplate.opsForSet(); ZSetOperations zSetOperations = redisTemplate.opsForZSet();
}
@Test public void testString(){ redisTemplate.opsForValue().set("city","背景"); String city = (String) redisTemplate.opsForValue().get("city"); System.out.println(city); redisTemplate.opsForValue().set("code","123456",3, TimeUnit.MINUTES); redisTemplate.opsForValue().setIfAbsent("lock","1"); redisTemplate.opsForValue().setIfAbsent("lock","2"); }
@Test public void testHash(){ HashOperations hashOperations = redisTemplate.opsForHash(); hashOperations.put("100","name","tom"); hashOperations.put("100","age","20");
String name = (String) hashOperations.get("100", "name"); System.out.println("name: "+name);
Set keys = hashOperations.keys("100"); System.out.println("keys: "+keys); List values = hashOperations.values("100"); System.out.println("values: "+values); hashOperations.delete("100","age");
}
@Test public void testList(){ ListOperations listOperations = redisTemplate.opsForList(); listOperations.leftPushAll("mylist","a","b","c"); listOperations.leftPush("mylist","d");
List mylist = listOperations.range("mylist", 0, -1); System.out.println(mylist); listOperations.rightPop("mylist");
Long size = listOperations.size("mylist"); System.out.println(size); }
@Test public void testSet(){ SetOperations setOperations = redisTemplate.opsForSet();
setOperations.add("set1","a","b","c","d"); setOperations.add("set2","a","b","x","y");
Set members = setOperations.members("set1"); System.out.println(members);
Long size = setOperations.size("set1"); System.out.println(size); Set intersect = setOperations.intersect("set1", "set2"); System.out.println(intersect); Set union = setOperations.union("set1", "set2"); System.out.println(union); setOperations.remove("set1","a","b"); }
@Test public void testZset(){ ZSetOperations zSetOperations = redisTemplate.opsForZSet(); zSetOperations.add("zset1","a",10); zSetOperations.add("zset1","b",12); zSetOperations.add("zset1","c",9); Set zset1 = zSetOperations.range("zset1", 0, -1); System.out.println(zset1); zSetOperations.incrementScore("zset1","c",10); zSetOperations.remove("zset1","a","b"); }
@Test public void testCommon(){ Set keys = redisTemplate.keys("*"); System.out.println(keys); Boolean name = redisTemplate.hasKey("name"); Boolean set1 = redisTemplate.hasKey("set1"); for (Object key : keys) { DataType type = redisTemplate.type(key); System.out.println(type.name()); } redisTemplate.delete("mylist"); }
}
|