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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
| @RunWith(SpringRunner.class) @SpringBootTest @Slf4j public class RedistemplateApplicationTests { @Autowired private RedisTemplate<String, Object> redisTemplate;
@Autowired private ValueOperations<String, String> stringOperations;
@Autowired private ValueOperations<String, Object> valueOperations;
@Autowired private HashOperations<String, String, Object> hashOperations;
@Autowired private ListOperations<String, Object> listOperations;
@Autowired private SetOperations<String, Object> setOperations;
@Autowired private ZSetOperations<String, Object> zSetOperations;
@Test public void testString() { String key = "name"; stringOperations.set(key, "李白", 1, TimeUnit.SECONDS); log.info(stringOperations.get(key)); log.info(redisTemplate.getExpire(key, TimeUnit.MILLISECONDS) + ""); redisTemplate.delete(key); log.info(redisTemplate.hasKey(key) + ""); }
@Test public void testPOJO() { String key = "libai"; User user = new User(); user.setId(1L); user.setUsername("李白"); user.setPassword("123456"); user.setAge(28); user.setSex(1); valueOperations.set(key, user, 1, TimeUnit.SECONDS); User result = (User) valueOperations.get(key); log.info(JSONUtil.toJsonPrettyStr(result)); }
@Test public void testHash() { String key = "hashKey";
hashOperations.put(key, "name", "李白"); hashOperations.put(key, "age", 30); hashOperations.put(key, "gender", 1); Map<String, Object> myMap = new HashMap<>(); myMap.put("address", "长安"); myMap.put("salary", 2000.5); hashOperations.putAll(key, myMap); redisTemplate.expire(key, 10, TimeUnit.SECONDS);
log.info(hashOperations.get(key, "name") + ""); hashOperations.delete(key, "name", "salary"); log.info(hashOperations.hasKey(key, "salary") + "");
Map<String, Object> map = hashOperations.entries(key); log.info(JSONUtil.toJsonPrettyStr(map));
Set<String> keySet = hashOperations.keys(key); log.info(JSONUtil.toJsonPrettyStr(keySet));
Long size = hashOperations.size(key); log.info(size + "");
List<Object> values = hashOperations.values(key); log.info(JSONUtil.toJsonPrettyStr(values)); }
@Test public void testlist() { String key = "listKey";
User user1 = new User(); user1.setId(1L); user1.setUsername("aa"); user1.setPassword("123456"); user1.setAge(28); user1.setSex(1);
User user2 = new User(); user2.setId(2L); user2.setUsername("bb"); user2.setPassword("123456"); user2.setAge(55); user2.setSex(2); listOperations.rightPush(key, user1); listOperations.rightPush(key, user2); redisTemplate.expire(key, 10, TimeUnit.SECONDS);
for (int i = 0; i < listOperations.size(key); i++) { User user = (User) listOperations.index(key, i);
log.info(JSONUtil.toJsonPrettyStr(user)); }
List<User> userList = (List) listOperations.range(key, 0, -1); log.info(JSONUtil.toJsonPrettyStr(userList)); }
@Test public void testSet() { String key = "setKey";
Long num = setOperations.add(key, "a", "b", "c", "b"); redisTemplate.expire(key, 10, TimeUnit.SECONDS); log.info("存储数量:{}", num);
log.info(setOperations.isMember(key, "a") + "");
Set<Object> members = setOperations.members(key); log.info(JSONUtil.toJsonPrettyStr(members));
}
@Test public void testZset() { ZSetOperations.TypedTuple<Object> tuple1 = new DefaultTypedTuple<>("libai", 1.1); ZSetOperations.TypedTuple<Object> tuple2 = new DefaultTypedTuple<>("zhaoxb", 14.8); ZSetOperations.TypedTuple<Object> tuple3 = new DefaultTypedTuple<>("cc", -3.2); Set<ZSetOperations.TypedTuple<Object>> set = new HashSet<>(); set.add(tuple1); set.add(tuple2); set.add(tuple3); zSetOperations.add("zset", set);
zSetOperations.add("zset", "jj", 0.7); zSetOperations.add("zset", "kk", 3.5); log.info("zset:{}", zSetOperations.range("zset", 0, -1));
Long rank = zSetOperations.rank("zset", "zhaoxb"); log.info("zhaoxb排名:[{}]", rank);
Set<Object> rangeByScore = zSetOperations.rangeByScore("zset", 0, 10); Long rangeSize = zSetOperations.count("zset", 0, 10); log.info("截取[0,10]范围内元素zset:{},共有:[{}]", rangeByScore, rangeSize);
Double score1 = zSetOperations.score("zset", "zhaoxb"); Double score2 = zSetOperations.score("zset", "zxb"); log.info("存在元素的score:[{}],不存在元素的score:[{}]", score1, score2);
zSetOperations.remove("zset", "jj", "kk"); log.info("删除指定元素后,zset:{}", zSetOperations.range("zset", 0, -1)); zSetOperations.removeRangeByScore("zset", -3.2, 1.1); log.info("删除score在某一范围内的元素,zset:{}", zSetOperations.range("zset", 0, -1)); zSetOperations.removeRange("zset", 0, -1); log.info("删除指定排序范围内的元素,zset:{}", zSetOperations.range("zset", 0, -1)); } }
|