如何利用 Redis 快速實現簽到統計功能
需求
最新接到一個需求:為了增加用戶的粘合度,新增簽到功能,具體的說明如下:(玩游戲的同學是不是很熟悉這個套路 WAW )
- 簽到1天得1積分,連續簽到2天得2積分,3天得3積分,3天以上均得3積分等。
- 如果連續簽到中斷,則重置計數,每月重置計數。
- 當月簽到滿3天領取獎勵1,滿5天領取獎勵2,滿7天領取獎勵3
- 顯示用戶某月的簽到次數和首次簽到時間。
- 在日歷控件上展示用戶每月簽到,可以切換年月顯示。
功能分析
對于用戶簽到數據,如果直接采用數據庫存儲,當出現高并發訪問時,對數據庫壓力會很大。這時候應該采用緩存,以減輕數據庫的壓力,Redis是高性能的內存數據庫,適用于這樣的場景。如果采用String類型保存,當用戶數量大時,內存開銷就非常大。如果采用集合類型保存,例如Set、Hash,查詢用戶某個范圍的數據時,查詢效率又不高。
Redis其實還提供了一種特殊的數據類型BitMap(位圖),每個bit位對應0和1兩個狀態。雖然內部還是采用String類型存儲,但Redis提供了一些指令用于直接操作BitMap,可以把它看作一個bit數組,數組的下標就是偏移量。它的優點是內存開銷小,效率高且操作簡單,很適合用于簽到這類場景。缺點在于位計算和位表示數值的局限。如果要用位來做業務數據記錄,就不要在意value的值。
準備
Redis提供了以下幾個指令用于操作BitMap:
| 命令 | 說明 | 可用版本 | 時間復雜度 |
|---|---|---|---|
| SETBIT | 對 key 所儲存的字符串值,設置或清除指定偏移量上的位(bit)。 |
>= 2.2.0 | O(1) |
| GETBIT | 對 key 所儲存的字符串值,獲取指定偏移量上的位(bit)。 |
>= 2.2.0 | O(1) |
| BITCOUNT | 計算給定字符串中,被設置為 1 的比特位的數量。 | >= 2.6.0 | O(N) |
| BITPOS | 返回位圖中第一個值為 bit 的二進制位的位置。 | >= 2.8.7 | O(N) |
| BITOP | 對一個或多個保存二進制位的字符串 key 進行位元操作。 |
>= 2.6.0 | O(N) |
| BITFIELD | BITFIELD 命令可以在一次調用中同時對多個位范圍進行操作。 |
>= 3.2.0 | O(1) |
大家知道 Redis 的字符串數據都是以二進制的形式存放的,所以說 Redis 的 Bit 操作非常適合處理這個場景,因為 Bit 的值為 0 或 1,用戶是否打卡也可以用 0 或 1 來表示,我們把簽到的天數對應到每個字節上,打卡了就是 1,沒打卡就是 0,那么一個用戶一年下來的記錄就是 365 位的長度,100 萬用戶一年只需要耗費大約 43 M 左右的存儲空間就可以了,而且速度賊快。
那么究竟如何去打卡呢,我們可以利用 setbit 命令來實現,setbit 的作用說的直白點就是:在你想要的位置操作字節值,比如說用戶 1 在 6 月 7 號 簽到了,那么 setbit (20220607, 1 ,1) 就可以實現簽到功能了,這里的 offset 就是 1,同理,不同的用戶不同的日期,改變對應的值就好了。
例如u:sign:1:202206表示ID=1的用戶在2022年6月的簽到記錄。
# 用戶6月10號簽到 SETBIT u:sign:1:202206 9 1 # 偏移量是從0開始,所以要把10減1 # 檢查6月10號是否簽到 GETBIT u:sign:1:202206 9 # 偏移量是從0開始,所以要把10減1 # 統計6月份的簽到次數 BITCOUNT u:sign:1:202206 # 獲取6月份上旬的簽到數據 BITFIELD u:sign:1:202206 get u10 0 # 獲取6月份首次簽到的日期 BITPOS u:sign:1:202206 1 # 返回的首次簽到的偏移量,加上1即為當月的某一天
示例代碼
using StackExchange.Redis; using System; using System.Collections.Generic; using System.Linq; /** * 基于Redis Bitmap的用戶簽到功能實現類 * * 實現功能: * 1. 用戶簽到 * 2. 檢查用戶是否簽到 * 3. 獲取當月簽到次數 * 4. 獲取當月連續簽到次數 * 5. 獲取當月首次簽到日期 * 6. 獲取當月簽到情況 */ public class UserSignDemo { private IDatabase _db; public UserSignDemo(IDatabase db) { _db = db; } /** * 用戶簽到 * * @param uid 用戶ID * @param date 日期 * @return 之前的簽到狀態 */ public bool DoSign(int uid, DateTime date) { int offset = date.Day - 1; return _db.StringSetBit(BuildSignKey(uid, date), offset, true); } /** * 檢查用戶是否簽到 * * @param uid 用戶ID * @param date 日期 * @return 當前的簽到狀態 */ public bool CheckSign(int uid, DateTime date) { int offset = date.Day - 1; return _db.StringGetBit(BuildSignKey(uid, date), offset); } /** * 獲取用戶簽到次數 * * @param uid 用戶ID * @param date 日期 * @return 當前的簽到次數 */ public long GetSignCount(int uid, DateTime date) { return _db.StringBitCount(BuildSignKey(uid, date)); } /** * 獲取當月連續簽到次數 * * @param uid 用戶ID * @param date 日期 * @return 當月連續簽到次數 */ public long GetContinuousSignCount(int uid, DateTime date) { int signCount = 0; string type = $"u{date.Day}"; // 取1號到當天的簽到狀態 RedisResult result = _db.Execute("BITFIELD", (RedisKey)BuildSignKey(uid, date), "GET", type, 0); if (!result.IsNull) { var list = (long[])result; if (list.Length > 0) { // 取低位連續不為0的個數即為連續簽到次數,需考慮當天尚未簽到的情況 long v = list[0]; for (int i = 0; i < date.Day; i++) { if (v >> 1 << 1 == v) { // 低位為0且非當天說明連續簽到中斷了 if (i > 0) break; } else { signCount += 1; } v >>= 1; } } } return signCount; } /** * 獲取當月首次簽到日期 * * @param uid 用戶ID * @param date 日期 * @return 首次簽到日期 */ public DateTime? GetFirstSignDate(int uid, DateTime date) { long pos = _db.StringBitPosition(BuildSignKey(uid, date), true); return pos < 0 ? null : date.AddDays(date.Day - (int)(pos + 1)); } /** * 獲取當月簽到情況 * * @param uid 用戶ID * @param date 日期 * @return Key為簽到日期,Value為簽到狀態的Map */ public Dictionary<string, bool> GetSignInfo(int uid, DateTime date) { Dictionary<string, bool> signMap = new Dictionary<string, bool>(date.Day); string type = $"u{GetDayOfMonth(date)}"; RedisResult result = _db.Execute("BITFIELD", (RedisKey)BuildSignKey(uid, date), "GET", type, 0); if (!result.IsNull) { var list = (long[])result; if (list.Length > 0) { // 由低位到高位,為0表示未簽,為1表示已簽 long v = list[0]; for (int i = GetDayOfMonth(date); i > 0; i--) { DateTime d = date.AddDays(i - date.Day); signMap.Add(FormatDate(d, "yyyy-MM-dd"), v >> 1 << 1 != v); v >>= 1; } } } return signMap; } private static string FormatDate(DateTime date) { return FormatDate(date, "yyyyMM"); } private static string FormatDate(DateTime date, string pattern) { return date.ToString(pattern); } /** * 構建簽到Key * * @param uid 用戶ID * @param date 日期 * @return 簽到Key */ private static string BuildSignKey(int uid, DateTime date) { return $"u:sign:{uid}:{FormatDate(date)}"; } /** * 獲取月份天數 * * @param date 日期 * @return 天數 */ private static int GetDayOfMonth(DateTime date) { if (date.Month == 2) { return 28; } if (new int[] { 1, 3, 5, 7, 8, 10, 12 }.Contains(date.Month)) { return 31; } return 30; } static void Main(string[] args) { ConnectionMultiplexer connection = ConnectionMultiplexer.Connect("192.168.0.104:7001,password=123456"); UserSignDemo demo = new UserSignDemo(connection.GetDatabase()); DateTime today = DateTime.Now; int uid = 1225; { // doSign bool signed = demo.DoSign(uid, today); if (signed) { Console.WriteLine("您已簽到:" + FormatDate(today, "yyyy-MM-dd")); } else { Console.WriteLine("簽到完成:" + FormatDate(today, "yyyy-MM-dd")); } } { // checkSign bool signed = demo.CheckSign(uid, today); if (signed) { Console.WriteLine("您已簽到:" + FormatDate(today, "yyyy-MM-dd")); } else { Console.WriteLine("尚未簽到:" + FormatDate(today, "yyyy-MM-dd")); } } { // getSignCount long count = demo.GetSignCount(uid, today); Console.WriteLine("本月簽到次數:" + count); } { // getContinuousSignCount long count = demo.GetContinuousSignCount(uid, today); Console.WriteLine("連續簽到次數:" + count); } { // getFirstSignDate DateTime? date = demo.GetFirstSignDate(uid, today); if (date.HasValue) { Console.WriteLine("本月首次簽到:" + FormatDate(date.Value, "yyyy-MM-dd")); } else { Console.WriteLine("本月首次簽到:無"); } } { // getSignInfo Console.WriteLine("當月簽到情況:"); Dictionary<string, bool> signInfo = new Dictionary<string, bool>(demo.GetSignInfo(uid, today)); foreach (var entry in signInfo) { Console.WriteLine(entry.Key + ": " + (entry.Value ? "√" : "-")); } } } }


浙公網安備 33010602011771號