|
@@ -1,20 +1,130 @@
|
|
|
package com.sf;
|
|
|
|
|
|
+import com.google.gson.Gson;
|
|
|
+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 java.util.*;
|
|
|
|
|
|
@SpringBootTest
|
|
|
public class RedisTests {
|
|
|
|
|
|
+ // RedisTemplate是redis整合spring过程中提供的模板工具
|
|
|
@Autowired
|
|
|
private RedisTemplate redisTemplate;
|
|
|
|
|
|
@Test
|
|
|
public void test() {
|
|
|
- redisTemplate.opsForValue().set("k111", "v111");
|
|
|
- Object object = redisTemplate.opsForValue().get("k111");
|
|
|
+ ValueOperations stringOperations = redisTemplate.opsForValue();
|
|
|
+ stringOperations.set("k111", "v111");
|
|
|
+ Object object = stringOperations.get("k111");
|
|
|
System.out.println(object);
|
|
|
}
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testList() {
|
|
|
+ ListOperations forList = redisTemplate.opsForList();
|
|
|
+ String key = "mylist11";
|
|
|
+ forList.rightPushAll(key, "1", "2", "3", "4", "5"); //rightPush
|
|
|
+// forList.leftPushAll(key, "10", "20", "30", "40", "50");
|
|
|
+ // 50 40 30 20 10 1 2 3 4 5
|
|
|
+ forList.leftPushAll(key, "50", "40", "30", "20", "10");
|
|
|
+ // llen
|
|
|
+ System.out.println(forList.size(key));
|
|
|
+ // lrange
|
|
|
+ System.out.println(forList.range(key, 0, -1));
|
|
|
+
|
|
|
+// forList.rightPop(key);
|
|
|
+// forList.trim(key, 0, 4);
|
|
|
+// System.out.println(forList.range(key, 0, -1));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testHash() {
|
|
|
+ // 对hash的数据类型进行操作
|
|
|
+ HashOperations hashOperations = redisTemplate.opsForHash();
|
|
|
+ // hset myhash11 field11 value11
|
|
|
+ String key = "myhash11";
|
|
|
+// hashOperations.put(key, "field11", "value11");
|
|
|
+// // 批量设置key值
|
|
|
+// Map<String, Object> map = new HashMap<>();
|
|
|
+// map.put("field1", "value1");
|
|
|
+// map.put("field2", "value2");
|
|
|
+// hashOperations.putAll(key, map);
|
|
|
+
|
|
|
+ String object = (String) hashOperations.get(key, "field1");
|
|
|
+ System.out.println(object);
|
|
|
+ List<Object> mVals = hashOperations.multiGet(key, Arrays.asList("field1", "field2"));
|
|
|
+ System.out.println("根据指定的多个key获取value: " + mVals);
|
|
|
+
|
|
|
+ Boolean hasKey = hashOperations.hasKey(key, "field1");
|
|
|
+ System.out.println("判断key是否存在: " + hasKey);
|
|
|
+ Set<Object> keys = hashOperations.keys(key);
|
|
|
+ System.out.println("获取全部的key: " + keys);
|
|
|
+ List<Object> values = hashOperations.values(key);
|
|
|
+ System.out.println("获取全部的value: " + values);
|
|
|
+// Long deletedFields = hashOperations.delete(key, "field1");
|
|
|
+// Long incrementedValue = hashOperations.increment(key, "field1", 5);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private HomeBookMapper homeBookMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private BookInfoMapper bookInfoMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private Gson gson;
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testBook() {
|
|
|
+ List<HomeBook> homeBookList = homeBookMapper.selectList(null);
|
|
|
+ ListOperations listOperations = redisTemplate.opsForList();
|
|
|
+ HashOperations hashOperations = redisTemplate.opsForHash();
|
|
|
+
|
|
|
+ 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();
|
|
|
+ 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 = "homeBookList0";
|
|
|
+ String key1 = "homeBookList1";
|
|
|
+ String key2 = "homeBookList2";
|
|
|
+ String key3 = "homeBookList3";
|
|
|
+ String key4 = "homeBookList4";
|
|
|
+ listOperations.rightPushAll(key0, bookIdList0);
|
|
|
+ listOperations.rightPushAll(key1, bookIdList1);
|
|
|
+ listOperations.rightPushAll(key2, bookIdList2);
|
|
|
+ listOperations.rightPushAll(key3, bookIdList3);
|
|
|
+ listOperations.rightPushAll(key4, bookIdList4);
|
|
|
+
|
|
|
+ String hashKey = "bookHash";
|
|
|
+ hashOperations.putAll(hashKey, homeBookMap);
|
|
|
+
|
|
|
+ }
|
|
|
}
|