| 命令 | 說明 | 可用版本 | 時間復(fù)雜度 |
|---|---|---|---|
| SETBIT | 對 key 所儲存的字符串值,設(shè)置或清除指定偏移量上的位(bit)。 | >= 2.2.0 | O(1) |
| GETBIT | 對 key 所儲存的字符串值,獲取指定偏移量上的位(bit)。 | >= 2.2.0 | O(1) |
| BITCOUNT | 計算給定字符串中,被設(shè)置為 1 的比特位的數(shù)量。 | >= 2.6.0 | O(N) |
| BITPOS | 返回位圖中第一個值為 bit 的二進制位的位置。 | >= 2.8.7 | O(N) |
| BITOP | 對一個或多個保存二進制位的字符串 key 進行位元操作。 | >= 2.6.0 | O(N) |
| BITFIELD | BITFIELD 命令可以在一次調(diào)用中同時對多個位范圍進行操作。 | >= 3.2.0 | O(1) |
考慮到每月要重置連續(xù)簽到次數(shù),最簡單的方式是按用戶每月存一條簽到數(shù)據(jù)。Key的格式為 u:sign:{uid}:{yyyMM},而Value則采用長度為4個字節(jié)的(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 # 統(tǒng)計1月份的簽到次數(shù) BITCOUNT u:sign:1225:202101 # 獲取1月份前31天的簽到數(shù)據(jù) BITFIELD u:sign:1225:202101 get u31 0 # 獲取1月份首次簽到的日期 BITPOS u:sign:1225:202101 1 # 返回的首次簽到的偏移量,加上1即為當(dāng)月的某一天
示例代碼
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
/**
* 基于Redis Bitmap的用戶簽到功能實現(xiàn)類
*
* 實現(xiàn)功能:
* 1. 用戶簽到
* 2. 檢查用戶是否簽到
* 3. 獲取當(dāng)月簽到次數(shù)
* 4. 獲取當(dāng)月連續(xù)簽到次數(shù)
* 5. 獲取當(dāng)月首次簽到日期
* 6. 獲取當(dāng)月簽到情況
*/
public class UserSignDemo
{
private IDatabase _db;
public UserSignDemo(IDatabase db)
{
_db = db;
}
/**
* 用戶簽到
*
* @param uid 用戶ID
* @param date 日期
* @return 之前的簽到狀態(tài)
*/
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 當(dāng)前的簽到狀態(tài)
*/
public bool CheckSign(int uid, DateTime date)
{
int offset = date.Day - 1;
return _db.StringGetBit(BuildSignKey(uid, date), offset);
}
/**
* 獲取用戶簽到次數(shù)
*
* @param uid 用戶ID
* @param date 日期
* @return 當(dāng)前的簽到次數(shù)
*/
public long GetSignCount(int uid, DateTime date)
{
return _db.StringBitCount(BuildSignKey(uid, date));
}
/**
* 獲取當(dāng)月連續(xù)簽到次數(shù)
*
* @param uid 用戶ID
* @param date 日期
* @return 當(dāng)月連續(xù)簽到次數(shù)
*/
public long GetContinuousSignCount(int uid, DateTime date)
{
int signCount = 0;
string type = $"u{date.Day}"; // 取1號到當(dāng)天的簽到狀態(tài)
RedisResult result = _db.Execute("BITFIELD", (RedisKey)BuildSignKey(uid, date), "GET", type, 0);
if (!result.IsNull)
{
var list = (long[])result;
if (list.Length > 0)
{
// 取低位連續(xù)不為0的個數(shù)即為連續(xù)簽到次數(shù),需考慮當(dāng)天尚未簽到的情況
long v = list[0];
for (int i = 0; i date.Day; i++)
{
if (v >> 1 1 == v)
{
// 低位為0且非當(dāng)天說明連續(xù)簽到中斷了
if (i > 0) break;
}
else
{
signCount += 1;
}
v >>= 1;
}
}
}
return signCount;
}
/**
* 獲取當(dāng)月首次簽到日期
*
* @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));
}
/**
* 獲取當(dāng)月簽到情況
*
* @param uid 用戶ID
* @param date 日期
* @return Key為簽到日期,Value為簽到狀態(tài)的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);
}
/**
* 構(gòu)建簽到Key
*
* @param uid 用戶ID
* @param date 日期
* @return 簽到Key
*/
private static string BuildSignKey(int uid, DateTime date)
{
return $"u:sign:{uid}:{FormatDate(date)}";
}
/**
* 獲取月份天數(shù)
*
* @param date 日期
* @return 天數(shù)
*/
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("本月簽到次數(shù):" + count);
}
{ // getContinuousSignCount
long count = demo.GetContinuousSignCount(uid, today);
Console.WriteLine("連續(xù)簽到次數(shù):" + 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("當(dāng)月簽到情況:");
Dictionarystring, bool> signInfo = new Dictionarystring, bool>(demo.GetSignInfo(uid, today));
foreach (var entry in signInfo)
{
Console.WriteLine(entry.Key + ": " + (entry.Value ? "√" : "-"));
}
}
}
}
運行結(jié)果

基于Redis位圖實現(xiàn)用戶簽到功能
Redis 深度歷險:核心原理與應(yīng)用實踐
Redis:Bitmap的setbit,getbit,bitcount,bitop等使用與應(yīng)用場景
BITFIELD SET command is not working
到此這篇關(guān)于Redis基于Bitmap實現(xiàn)用戶簽到功能的文章就介紹到這了,更多相關(guān)Redis Bitmap用戶簽到內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
標(biāo)簽:大慶 江蘇 臺州 朝陽 果洛 吉安 楊凌 北京
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Redis基于Bitmap實現(xiàn)用戶簽到功能》,本文關(guān)鍵詞 Redis,基于,Bitmap,實現(xiàn),用戶,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。