Mã nằm ở cuối bài đăng, nếu bạn quen với nguyên tắc phím nhiều hset sắp xếp lại, bỏ qua nội dung sau và đọc trực tiếp mã.
Redis Sort là nhằm sắp xếp các trường trong danh sách/bộ/Zset, nhưng phương pháp này có thể được sử dụng để sắp xếp nhiều khóa dựa trên số liệu được chỉ định mà chúng tôi muốn. Chúng ta có thể sử dụng "sắp xếp" để sắp xếp các khóa đa hset theo trường được chỉ định, nhưng có giới hạn về mẫu của các khóa hset.
Ví dụ, nếu mẫu của phím hset là "băm {i}" (i là một số nguyên), trong điều kiện này chúng ta có thể sắp xếp nó.
127.0.0.1:6379> keys hash*
1) "hash3"
2) "hash2"
3) "hash1"
Hãy nhìn vào nội dung của hash1:
127.0.0.1:6379> hgetall hash1
1) "id"
2) "24"
3) "name"
4) "kobe"
Mỗi chìa khóa băm chứa hai lĩnh vực: "id", "tên". Nếu chúng ta muốn sắp xếp các khóa này bằng id của nó. Chúng ta nên làm gì ?
Trước tiên, hãy thêm khóa đặt tên là "myset". "myset" là khóa thiết lập chứa các thành viên {"1", "2", "3"}.
127.0.0.1:6379> smembers myset
1) "1"
2) "2"
3) "3"
Sau đó chạy lệnh sau:
127.0.0.1:6379> SORT myset BY hash*->id GET hash*->id GET hash*->name
1) "3"
2) "wade"
3) "24"
4) "kobe"
5) "30"
6) "curry"
Eureka, sắp xếp băm {1-3} bằng id của nó.
Đây là mã của sử dụng Spring Redis để thực hiện công việc:
public static String getRandomStr() {
return String.valueOf(new Random().nextInt(100));
}
public static void redisTemplateSort(RedisTemplate redisTemplate) {
String sortKey = "sortKey";
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
redisTemplate.setKeySerializer(stringRedisSerializer);
redisTemplate.setValueSerializer(stringRedisSerializer);
redisTemplate.setHashKeySerializer(stringRedisSerializer);
redisTemplate.setHashValueSerializer(stringRedisSerializer);
redisTemplate.delete(sortKey);
if (!redisTemplate.hasKey(sortKey)) {
for (int i = 0; i < 10; i++) {
redisTemplate.boundSetOps(sortKey).add(String.valueOf(i));
String hashKey = "hash" + i,
strId = String.valueOf(i),
strName = getRandomStr(),
strSite = getRandomStr();
redisTemplate.boundHashOps(hashKey).put("_id", strId);
redisTemplate.boundHashOps(hashKey).put("Name", strName);
redisTemplate.boundHashOps(hashKey).put("Site", strSite);
System.out.printf("%s : {\"_id\": %s, \"Name\": %s, \"Site\", %s}\n",
hashKey, strId, strName, strSite);
}
}
SortQuery<String> sortQuery = SortQueryBuilder.sort(sortKey).by("hash*->Name")
.get("hash*->_id").get("hash*->Name").get("hash*->Site").build();
List<String> sortRslt = redisTemplate.sort(sortQuery);
for (int i = 0; i < sortRslt.size();) {
System.out.printf("{\"_id\": %s, \"Name\": %s, \"Site\", %s}\n", sortRslt.get(i+2), sortRslt.get(i+1), sortRslt.get(i));
i += 3;
}
}
Kết quả chạy redisTemplateSort(redisTemplate)
(như sắp xếp theo tên trong code):
hash0 : {"_id": 0, "Name": 59, "Site", 60}
hash1 : {"_id": 1, "Name": 37, "Site", 57}
hash2 : {"_id": 2, "Name": 6, "Site", 40}
hash3 : {"_id": 3, "Name": 91, "Site", 58}
hash4 : {"_id": 4, "Name": 39, "Site", 32}
hash5 : {"_id": 5, "Name": 27, "Site", 82}
hash6 : {"_id": 6, "Name": 43, "Site", 10}
hash7 : {"_id": 7, "Name": 17, "Site", 55}
hash8 : {"_id": 8, "Name": 14, "Site", 91}
hash9 : {"_id": 9, "Name": 39, "Site", 91}
{"_id": 40, "Name": 6, "Site", 2}
{"_id": 91, "Name": 14, "Site", 8}
{"_id": 55, "Name": 17, "Site", 7}
{"_id": 82, "Name": 27, "Site", 5}
{"_id": 57, "Name": 37, "Site", 1}
{"_id": 32, "Name": 39, "Site", 4}
{"_id": 91, "Name": 39, "Site", 9}
{"_id": 10, "Name": 43, "Site", 6}
{"_id": 60, "Name": 59, "Site", 0}
{"_id": 58, "Name": 91, "Site", 3}
Chỉ muốn đề cập đến, phím là các hoạt động băm. localhost> HKeys Khách hàng "1+ {\" _ id ":" 1 ", \" Tên \ ": \" C5796 \ ", \" Trang web \ ": \" DRG1 \ "}" "2+ { \ "_ id": "2", \ "Tên \": \ "CX1XE \", \ "Trang web \": \ "DG1 \"} " " 3+ {\ "_ id": "3", \ " Tên \ ": \" C553 \ ", \" Trang web \ ": \" DG1 \ "}" – ashK