| 命令 | 說明 | 可用版本 | 時間復雜度 |
|---|---|---|---|
| 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) |
考慮到每月要重置連續簽到次數,最簡單的方式是按用戶每月存一條簽到數據。Key的格式為 u:sign:{uid}:{yyyMM},而Value則采用長度為4個字節的(32位)的BitMap(最大月份只有31天)。BitMap的每一位代表一天的簽到,1表示已簽,0表示未簽。
例如 u:sign:1225:202101 表示ID=1225的用戶在2021年1月的簽到記錄
# 用戶1月6號簽到 SETBIT u:sign:1225:202101 5 1 # 偏移量是從0開始,所以要把6減1 # 檢查1月6號是否簽到 GETBIT u:sign:1225:202101 5 # 偏移量是從0開始,所以要把6減1 # 統計1月份的簽到次數 BITCOUNT u:sign:1225:202101 # 獲取1月份前31天的簽到數據 BITFIELD u:sign:1225:202101 get u31 0 # 獲取1月份首次簽到的日期 BITPOS u:sign:1225:202101 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 Dictionarystring, bool> GetSignInfo(int uid, DateTime date)
{
Dictionarystring, bool> signMap = new Dictionarystring, 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("當月簽到情況:");
Dictionarystring, bool> signInfo = new Dictionarystring, bool>(demo.GetSignInfo(uid, today));
foreach (var entry in signInfo)
{
Console.WriteLine(entry.Key + ": " + (entry.Value ? "√" : "-"));
}
}
}
}
運行結果

基于Redis位圖實現用戶簽到功能
Redis 深度歷險:核心原理與應用實踐
Redis:Bitmap的setbit,getbit,bitcount,bitop等使用與應用場景
BITFIELD SET command is not working
到此這篇關于Redis基于Bitmap實現用戶簽到功能的文章就介紹到這了,更多相關Redis Bitmap用戶簽到內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!