對(duì)于用戶簽到數(shù)據(jù),如果每條數(shù)據(jù)都用K/V的方式存儲(chǔ),當(dāng)用戶量大的時(shí)候內(nèi)存開銷是非常大的。而位圖(BitMap)是由一組bit位組成的,每個(gè)bit位對(duì)應(yīng)0和1兩個(gè)狀態(tài),雖然內(nèi)部還是采用String類型存儲(chǔ),但Redis提供了一些指令用于直接操作位圖,可以把它看作是一個(gè)bit數(shù)組,數(shù)組的下標(biāo)就是偏移量。它的優(yōu)點(diǎn)是內(nèi)存開銷小、效率高且操作簡(jiǎn)單,很適合用于簽到這類場(chǎng)景。
考慮到每月初需要重置連續(xù)簽到次數(shù),最簡(jiǎn)單的方式是按用戶每月存一條簽到數(shù)據(jù)(也可以每年存一條數(shù)據(jù))。Key的格式為u:sign:uid:yyyyMM
,Value則采用長(zhǎng)度為4個(gè)字節(jié)(32位)的位圖(最大月份只有31天)。位圖的每一位代表一天的簽到,1表示已簽,0表示未簽。
import redis.clients.jedis.Jedis;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
/**
* 基于Redis位圖的用戶簽到功能實(shí)現(xiàn)類
* p>
* 實(shí)現(xiàn)功能:
* 1. 用戶簽到
* 2. 檢查用戶是否簽到
* 3. 獲取當(dāng)月簽到次數(shù)
* 4. 獲取當(dāng)月連續(xù)簽到次數(shù)
* 5. 獲取當(dāng)月首次簽到日期
* 6. 獲取當(dāng)月簽到情況
*/
public class UserSignDemo {
private Jedis jedis = new Jedis();
/**
* 用戶簽到
*
* @param uid 用戶ID
* @param date 日期
* @return 之前的簽到狀態(tài)
*/
public boolean doSign(int uid, LocalDate date) {
int offset = date.getDayOfMonth() - 1;
return jedis.setbit(buildSignKey(uid, date), offset, true);
}
/**
* 檢查用戶是否簽到
*
* @param uid 用戶ID
* @param date 日期
* @return 當(dāng)前的簽到狀態(tài)
*/
public boolean checkSign(int uid, LocalDate date) {
int offset = date.getDayOfMonth() - 1;
return jedis.getbit(buildSignKey(uid, date), offset);
}
/**
* 獲取用戶簽到次數(shù)
*
* @param uid 用戶ID
* @param date 日期
* @return 當(dāng)前的簽到次數(shù)
*/
public long getSignCount(int uid, LocalDate date) {
return jedis.bitcount(buildSignKey(uid, date));
}
/**
* 獲取當(dāng)月連續(xù)簽到次數(shù)
*
* @param uid 用戶ID
* @param date 日期
* @return 當(dāng)月連續(xù)簽到次數(shù)
*/
public long getContinuousSignCount(int uid, LocalDate date) {
int signCount = 0;
String type = String.format("u%d", date.getDayOfMonth());
ListLong> list = jedis.bitfield(buildSignKey(uid, date), "GET", type, "0");
if (list != null list.size() > 0) {
// 取低位連續(xù)不為0的個(gè)數(shù)即為連續(xù)簽到次數(shù),需考慮當(dāng)天尚未簽到的情況
long v = list.get(0) == null ? 0 : list.get(0);
for (int i = 0; i date.getDayOfMonth(); i++) {
if (v >> 1 1 == v) {
// 低位為0且非當(dāng)天說(shuō)明連續(xù)簽到中斷了
if (i > 0) break;
} else {
signCount += 1;
}
v >>= 1;
}
}
return signCount;
}
/**
* 獲取當(dāng)月首次簽到日期
*
* @param uid 用戶ID
* @param date 日期
* @return 首次簽到日期
*/
public LocalDate getFirstSignDate(int uid, LocalDate date) {
long pos = jedis.bitpos(buildSignKey(uid, date), true);
return pos 0 ? null : date.withDayOfMonth((int) (pos + 1));
}
/**
* 獲取當(dāng)月簽到情況
*
* @param uid 用戶ID
* @param date 日期
* @return Key為簽到日期,Value為簽到狀態(tài)的Map
*/
public MapString, Boolean> getSignInfo(int uid, LocalDate date) {
MapString, Boolean> signMap = new HashMap>(date.getDayOfMonth());
String type = String.format("u%d", date.lengthOfMonth());
ListLong> list = jedis.bitfield(buildSignKey(uid, date), "GET", type, "0");
if (list != null list.size() > 0) {
// 由低位到高位,為0表示未簽,為1表示已簽
long v = list.get(0) == null ? 0 : list.get(0);
for (int i = date.lengthOfMonth(); i > 0; i--) {
LocalDate d = date.withDayOfMonth(i);
signMap.put(formatDate(d, "yyyy-MM-dd"), v >> 1 1 != v);
v >>= 1;
}
}
return signMap;
}
private static String formatDate(LocalDate date) {
return formatDate(date, "yyyyMM");
}
private static String formatDate(LocalDate date, String pattern) {
return date.format(DateTimeFormatter.ofPattern(pattern));
}
private static String buildSignKey(int uid, LocalDate date) {
return String.format("u:sign:%d:%s", uid, formatDate(date));
}
public static void main(String[] args) {
UserSignDemo demo = new UserSignDemo();
LocalDate today = LocalDate.now();
{ // doSign
boolean signed = demo.doSign(1000, today);
if (signed) {
System.out.println("您已簽到:" + formatDate(today, "yyyy-MM-dd"));
} else {
System.out.println("簽到完成:" + formatDate(today, "yyyy-MM-dd"));
}
}
{ // checkSign
boolean signed = demo.checkSign(1000, today);
if (signed) {
System.out.println("您已簽到:" + formatDate(today, "yyyy-MM-dd"));
} else {
System.out.println("尚未簽到:" + formatDate(today, "yyyy-MM-dd"));
}
}
{ // getSignCount
long count = demo.getSignCount(1000, today);
System.out.println("本月簽到次數(shù):" + count);
}
{ // getContinuousSignCount
long count = demo.getContinuousSignCount(1000, today);
System.out.println("連續(xù)簽到次數(shù):" + count);
}
{ // getFirstSignDate
LocalDate date = demo.getFirstSignDate(1000, today);
System.out.println("本月首次簽到:" + formatDate(date, "yyyy-MM-dd"));
}
{ // getSignInfo
System.out.println("當(dāng)月簽到情況:");
MapString, Boolean> signInfo = new TreeMap>(demo.getSignInfo(1000, today));
for (Map.EntryString, Boolean> entry : signInfo.entrySet()) {
System.out.println(entry.getKey() + ": " + (entry.getValue() ? "√" : "-"));
}
}
}
}
到此這篇關(guān)于基于Redis位圖實(shí)現(xiàn)用戶簽到功能的文章就介紹到這了,更多相關(guān)Redis用戶簽到內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!