|
@@ -1,18 +1,9 @@
|
|
|
package com.sf;
|
|
|
|
|
|
-import com.google.gson.Gson;
|
|
|
-import com.sf.dto.resp.HomeBookRespDto;
|
|
|
-import com.sf.entity.BookInfo;
|
|
|
-import com.sf.entity.HomeBook;
|
|
|
-import com.sf.mapper.BookInfoMapper;
|
|
|
-import com.sf.mapper.HomeBookMapper;
|
|
|
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.core.HashOperations;
|
|
|
-import org.springframework.data.redis.core.ListOperations;
|
|
|
-import org.springframework.data.redis.core.RedisTemplate;
|
|
|
-import org.springframework.data.redis.core.ValueOperations;
|
|
|
+import org.springframework.data.redis.core.*;
|
|
|
|
|
|
import java.util.*;
|
|
|
|
|
@@ -78,99 +69,54 @@ public class RedisTests {
|
|
|
}
|
|
|
|
|
|
|
|
|
- @Autowired
|
|
|
- private HomeBookMapper homeBookMapper;
|
|
|
+ @Test
|
|
|
+ public void testSet() {
|
|
|
+ // 对集合的操作
|
|
|
+ SetOperations setOperations = redisTemplate.opsForSet();
|
|
|
+ String key = "testSet:myset1";
|
|
|
+ Long add = setOperations.add(key, "12", "13", "14", "15");
|
|
|
+ System.out.println(add);
|
|
|
|
|
|
- @Autowired
|
|
|
- private BookInfoMapper bookInfoMapper;
|
|
|
+ Long size = setOperations.size(key);
|
|
|
+ System.out.println(size);
|
|
|
|
|
|
- @Autowired
|
|
|
- private Gson gson;
|
|
|
+ Boolean isMember = setOperations.isMember(key, "12");
|
|
|
+ System.out.println(isMember);
|
|
|
|
|
|
- // homebook 数据初始化
|
|
|
- // 使用了 list和hash的数据结构
|
|
|
- @Test
|
|
|
- public void testBook() {
|
|
|
- List<HomeBook> homeBookList = homeBookMapper.selectList(null);
|
|
|
- ListOperations listOperations = redisTemplate.opsForList();
|
|
|
- HashOperations hashOperations = redisTemplate.opsForHash();
|
|
|
+ Object object = setOperations.randomMember(key);
|
|
|
+ System.out.println(object);
|
|
|
|
|
|
- List<String> bookIdList0 = new ArrayList<>();
|
|
|
- List<String> bookIdList1 = new ArrayList<>();
|
|
|
- List<String> bookIdList2 = new ArrayList<>();
|
|
|
- List<String> bookIdList3 = new ArrayList<>();
|
|
|
- List<String> bookIdList4 = new ArrayList<>();
|
|
|
- Map<String, String> homeBookMap = new HashMap<>();
|
|
|
- for (HomeBook homeBook : homeBookList) {
|
|
|
- Long bookId = homeBook.getBookId();
|
|
|
- // 根据type的类型不同 放入不同的list中
|
|
|
- switch (homeBook.getType().intValue()) {
|
|
|
- case 0 -> bookIdList0.add(String.valueOf(bookId));
|
|
|
- case 1 -> bookIdList1.add(String.valueOf(bookId));
|
|
|
- case 2 -> bookIdList2.add(String.valueOf(bookId));
|
|
|
- case 3 -> bookIdList3.add(String.valueOf(bookId));
|
|
|
- case 4 -> bookIdList4.add(String.valueOf(bookId));
|
|
|
- }
|
|
|
-
|
|
|
- BookInfo bookInfo = bookInfoMapper.selectById(bookId);
|
|
|
- String json = gson.toJson(bookInfo);
|
|
|
- homeBookMap.put(bookId.toString(), json);
|
|
|
- }
|
|
|
- String key0 = "book:homeBookList0";
|
|
|
- String key1 = "book:homeBookList1";
|
|
|
- String key2 = "book:homeBookList2";
|
|
|
- String key3 = "book:homeBookList3";
|
|
|
- String key4 = "book:homeBookList4";
|
|
|
- listOperations.rightPushAll(key0, bookIdList0);
|
|
|
- listOperations.rightPushAll(key1, bookIdList1);
|
|
|
- listOperations.rightPushAll(key2, bookIdList2);
|
|
|
- listOperations.rightPushAll(key3, bookIdList3);
|
|
|
- listOperations.rightPushAll(key4, bookIdList4);
|
|
|
-
|
|
|
- String hashKey = "book:bookHash";
|
|
|
- hashOperations.putAll(hashKey, homeBookMap);
|
|
|
+ Set set = setOperations.distinctRandomMembers(key, 8);
|
|
|
+ System.out.println(set);
|
|
|
|
|
|
- }
|
|
|
+ Object pop = setOperations.pop(key);
|
|
|
+ System.out.println(pop);
|
|
|
|
|
|
+ Set members = setOperations.members(key);
|
|
|
+ System.out.println(members);
|
|
|
+ }
|
|
|
|
|
|
@Test
|
|
|
- public void testBook1() {
|
|
|
- List<HomeBook> homeBookList = homeBookMapper.selectList(null);
|
|
|
- ListOperations listOperations = redisTemplate.opsForList();
|
|
|
- HashOperations hashOperations = redisTemplate.opsForHash();
|
|
|
- List<String> homeBookRedisList = new ArrayList<>();
|
|
|
- for (HomeBook homeBook : homeBookList) {
|
|
|
- Integer type = (int) homeBook.getType();
|
|
|
- Long bookId = homeBook.getBookId();
|
|
|
- String value = type + "," + bookId;
|
|
|
- homeBookRedisList.add(value);
|
|
|
- }
|
|
|
- String key = "book:homeBookList";
|
|
|
- listOperations.rightPushAll(key, homeBookRedisList);
|
|
|
-
|
|
|
-
|
|
|
- List<HomeBookRespDto> homeBookRespDtoList = new ArrayList<>();
|
|
|
- String hashKey = "book:bookHash";
|
|
|
- List<String> range = listOperations.range(key, 0, -1);
|
|
|
- for (String value : range) {
|
|
|
- String[] split = value.split(",");
|
|
|
- Integer type = Integer.parseInt(split[0]);
|
|
|
- Long bookId = Long.parseLong(split[1]);
|
|
|
- String json = (String) hashOperations.get(hashKey, bookId.toString());
|
|
|
- // 通过gson转化成对象
|
|
|
- BookInfo bookInfo = gson.fromJson(json, BookInfo.class);
|
|
|
- if (bookInfo != null) {
|
|
|
- HomeBookRespDto homeBookRespDto = HomeBookRespDto.builder()
|
|
|
- .type(type) // byte -> int
|
|
|
- .bookId(bookId)
|
|
|
- .picUrl(bookInfo.getPicUrl())
|
|
|
- .bookName(bookInfo.getBookName())
|
|
|
- .authorName(bookInfo.getAuthorName())
|
|
|
- .bookDesc(bookInfo.getBookDesc())
|
|
|
- .build();
|
|
|
- homeBookRespDtoList.add(homeBookRespDto);
|
|
|
- }
|
|
|
- }
|
|
|
- System.out.println(homeBookRespDtoList);
|
|
|
+ public void testZSet() {
|
|
|
+ ZSetOperations zSetOperations = redisTemplate.opsForZSet();
|
|
|
+ String key = "testSet:myzset";
|
|
|
+ zSetOperations.add(key,"zhangsan",90);
|
|
|
+ zSetOperations.add(key,"lisi",80);
|
|
|
+ zSetOperations.add(key,"wangwu",70);
|
|
|
+ zSetOperations.add(key,"zhaoliu",60);
|
|
|
+
|
|
|
+ Set range = zSetOperations.range(key, 0, -1);
|
|
|
+ System.out.println(range);
|
|
|
+
|
|
|
+ Set set = zSetOperations.rangeWithScores(key, 0, -1);
|
|
|
+ System.out.println(set);
|
|
|
+
|
|
|
+ Long rank = zSetOperations.rank(key, "lisi");
|
|
|
+ System.out.println(rank);
|
|
|
+ Long reverseRank = zSetOperations.reverseRank(key, "lisi");
|
|
|
+ System.out.println(reverseRank);
|
|
|
+
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
}
|