Redis 的有序集合(Sorted Set)是一個基于分數(shù)(score)排序的數(shù)據(jù)結(jié)構(gòu),它在 Redis 中非常重要,常用于實現(xiàn)排行榜、近似計數(shù)器等功能。
Redis 的有序集合(Sorted Set)是基于跳躍表(Skip List)實現(xiàn)的。跳躍表是一種高效的數(shù)據(jù)結(jié)構(gòu),其插入、刪除和查找操作的平均時間復(fù)雜度都是 O(log n),相對于平衡樹(如紅黑樹)的實現(xiàn)要簡單很多。跳躍表的結(jié)構(gòu)類似于鏈表,每個節(jié)點除了保存元素值外,還包含一個指針數(shù)組,分別指向?qū)?yīng)層次的下一個節(jié)點。這種多級指針的設(shè)計,使得跳表可以跨越多個節(jié)點進行快速搜索,同時保證跳表結(jié)構(gòu)的高效性和簡潔性。
有序集合的底層數(shù)據(jù)結(jié)構(gòu)由哈希(Hash)和跳躍表組成。在哈希中,存儲了元素及其關(guān)聯(lián)的評分(分數(shù))。每個元素都有一個唯一的評分,用于確定其在跳躍表中的位置。當(dāng)需要對有序集合進行操作時,Redis 首先通過哈希表找到元素及其評分,然后通過跳躍表進行相應(yīng)的操作。
以下是 Redis 有序集合(Sorted Set)的一些核心操作及其對應(yīng)的核心代碼分析:
添加元素(ZADD):
有序集合中的元素添加操作是通過哈希表和跳躍表協(xié)同完成的。首先,Redis 將元素值和評分存儲在哈希表中。然后,根據(jù)評分在跳躍表中找到對應(yīng)的位置,并將新元素插入到該位置。
獲取元素(ZRANGE、ZREVRANGE):
有序集合中的獲取元素操作主要依賴于跳躍表。ZRANGE 操作從跳躍表的頭部開始,按照給定的評分范圍返回符合條件的元素。ZREVRANGE 操作則從跳躍表的尾部開始,按照給定的評分范圍返回符合條件的元素。
刪除元素(ZREM):
刪除元素操作首先通過哈希表找到對應(yīng)元素,然后在跳躍表中刪除該元素。Redis 只需要刪除哈希表中的指向該元素的指針,跳躍表中的元素會自動上移。
更新元素評分(ZINCRBY):
更新元素評分操作僅需修改哈希表中對應(yīng)元素的評分,然后重新計算跳躍表中元素的位置。
獲取有序集合長度(ZCARD):
有序集合長度的操作直接查詢哈希表中的鍵值對數(shù)量。
隨機獲取元素(ZRANDMEMBER):
隨機獲取元素操作首先從哈希表中隨機選擇一個元素,然后在該元素所在的跳躍表區(qū)間內(nèi)隨機選擇一個元素。
通過以上操作,Redis 實現(xiàn)了高效有序集合(Sorted Set)的數(shù)據(jù)結(jié)構(gòu),提供了高性能的排序和范圍查找功能。
2、實戰(zhàn)
要使用 Spring Boot 和 Redis 實現(xiàn)排行榜功能,你可以遵循以下步驟:
引入依賴
在你的 Spring Boot 項目的 pom.xml 文件中,添加以下依賴:
org.springframework.boot spring-boot-starter-data-redis
配置 Redis
在 application.properties 或 application.yml 文件中配置 Redis 連接信息:
# application.properties spring.redis.host=localhost spring.redis.port=6379
# application.yml
spring:
redis:
host: localhost
port: 6379
創(chuàng)建 Redis 模板
創(chuàng)建一個 RedisTemplate Bean:
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; @Configuration public class RedisConfig { @Bean public RedisTemplateredisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(redisConnectionFactory); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); return redisTemplate; } }
創(chuàng)建排行榜實體類
創(chuàng)建一個排行榜實體類,包含用戶 ID、分數(shù)等信息:
import java.io.Serializable; public class RankingEntity implements Serializable { private String userId; private double score; // 構(gòu)造方法、getter 和 setter
實現(xiàn) Redis 排行榜操作
創(chuàng)建一個服務(wù)類,實現(xiàn)排行榜的相關(guān)操作,如添加分數(shù)、查詢排名等:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;
@Service
public class RankingService {
@Autowired
private RedisTemplate redisTemplate;
private static final String RANKING_KEY = "ranking_list";
/**
* 添加分數(shù)
* @param userId 用戶 ID
* @param score 分數(shù)
*/
public void addScore(String userId, double score) {
ValueOperations valueOperations = redisTemplate.opsForValue();
valueOperations.set(RANKING_KEY + ":" + userId, score, 60, TimeUnit.SECONDS);
}
/**
* 查詢排名
* @return 排名列表
*/
public List
審核編輯:劉清
-
計數(shù)器
+關(guān)注
關(guān)注
32文章
2303瀏覽量
97331 -
Redis
+關(guān)注
關(guān)注
0文章
390瀏覽量
11945
原文標(biāo)題:Redis實戰(zhàn) | 使用Redis 的有序集合(Sorted Set)實現(xiàn)排行榜功能,和Spring Boot集成
文章出處:【微信號:magedu-Linux,微信公眾號:馬哥Linux運維】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
發(fā)布評論請先 登錄
HarmonyOS開發(fā)案例:【排行榜頁面】

使用Redis的有序集合實現(xiàn)排行榜功能和Spring Boot集成
評論