天天看點

java redis實作bitmap簽到功能場景需求設計思路示例代碼運作結果

使用者一天有沒有簽到隻有兩種狀态:是和否(1,0)。一個月最多31天,4個byte32位剛好能容納一個月的資料量,每一位0和1分别表示是否簽到。是以使用4個byte就能表示一個人一個月的簽到狀态
java redis實作bitmap簽到功能場景需求設計思路示例代碼運作結果

場景需求

适用場景如簽到送積分、簽到領取獎勵等,大緻需求如下:

  • 簽到1天送1積分,連續簽到2天送2積分,3天送3積分,3天以上均送3積分等。
  • 如果連續簽到中斷,則重置計數,每月初重置計數。
  • 當月簽到滿3天領取獎勵1,滿5天領取獎勵2,滿7天領取獎勵3……等等。
  • 顯示使用者某個月的簽到次數和首次簽到時間。
  • 在月曆控件上展示使用者每月簽到情況,可以切換年月顯示……等等。

設計思路

對于使用者簽到資料,如果每條資料都用K/V的方式存儲,當使用者量大的時候記憶體開銷是非常大的。而位圖(BitMap)是由一組bit位組成的,每個bit位對應0和1兩個狀态,雖然内部還是采用String類型存儲,但Redis提供了一些指令用于直接操作位圖,可以把它看作是一個bit數組,數組的下标就是偏移量。它的優點是記憶體開銷小、效率高且操作簡單,很适合用于簽到這類場景。

Redis提供了以下幾個指令用于操作位圖:

  • SETBIT
  • GETBIT
  • BITCOUNT
  • BITPOS
  • BITOP
  • BITFIELD

考慮到每月初需要重置連續簽到次數,最簡單的方式是按使用者每月存一條簽到資料(也可以每年存一條資料)。Key的格式為

u:sign:uid:yyyyMM

,Value則采用長度為4個位元組(32位)的位圖(最大月份隻有31天)。位圖的每一位代表一天的簽到,1表示已簽,0表示未簽。

例如

u:sign:1000:201902

表示ID=1000的使用者在2019年2月的簽到記錄。

# 使用者2月17号簽到
SETBIT u:sign:1000:201902 16 1 # 偏移量是從0開始,是以要把17減1

# 檢查2月17号是否簽到
GETBIT u:sign:1000:201902 16 # 偏移量是從0開始,是以要把17減1

# 統計2月份的簽到次數
BITCOUNT u:sign:1000:201902

# 擷取2月份前28天的簽到資料
BITFIELD u:sign:1000:201902 get u28 0

# 擷取2月份首次簽到的日期
BITPOS u:sign:1000:201902 1 # 傳回的首次簽到的偏移量,加上1即為當月的某一天
           

示例代碼

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位圖的使用者簽到功能實作類
 * <p>
 * 實作功能:
 * 1. 使用者簽到
 * 2. 檢查使用者是否簽到
 * 3. 擷取當月簽到次數
 * 4. 擷取當月連續簽到次數
 * 5. 擷取當月首次簽到日期
 * 6. 擷取當月簽到情況
 */
public class UserSignDemo {
    private Jedis jedis = new Jedis();

    /**
     * 使用者簽到
     *
     * @param uid  使用者ID
     * @param date 日期
     * @return 之前的簽到狀态
     */
    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 目前的簽到狀态
     */
    public boolean checkSign(int uid, LocalDate date) {
        int offset = date.getDayOfMonth() - 1;
        return jedis.getbit(buildSignKey(uid, date), offset);
    }

    /**
     * 擷取使用者簽到次數
     *
     * @param uid  使用者ID
     * @param date 日期
     * @return 目前的簽到次數
     */
    public long getSignCount(int uid, LocalDate date) {
        return jedis.bitcount(buildSignKey(uid, date));
    }

    /**
     * 擷取當月連續簽到次數
     *
     * @param uid  使用者ID
     * @param date 日期
     * @return 當月連續簽到次數
     */
    public long getContinuousSignCount(int uid, LocalDate date) {
        int signCount = 0;
        String type = String.format("u%d", date.getDayOfMonth());
        List<Long> list = jedis.bitfield(buildSignKey(uid, date), "GET", type, "0");
        if (list != null && list.size() > 0) {
            // 取低位連續不為0的個數即為連續簽到次數,需考慮當天尚未簽到的情況
            long v = list.get(0) == null ? 0 : list.get(0);
            for (int i = 0; i < date.getDayOfMonth(); 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 LocalDate getFirstSignDate(int uid, LocalDate date) {
        long pos = jedis.bitpos(buildSignKey(uid, date), true);
        return pos < 0 ? null : date.withDayOfMonth((int) (pos + 1));
    }

    /**
     * 擷取當月簽到情況
     *
     * @param uid  使用者ID
     * @param date 日期
     * @return Key為簽到日期,Value為簽到狀态的Map
     */
    public Map<String, Boolean> getSignInfo(int uid, LocalDate date) {
        Map<String, Boolean> signMap = new HashMap<>(date.getDayOfMonth());
        String type = String.format("u%d", date.lengthOfMonth());
        List<Long> 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("本月簽到次數:" + count);
        }

        { // getContinuousSignCount
            long count = demo.getContinuousSignCount(1000, today);
            System.out.println("連續簽到次數:" + count);
        }

        { // getFirstSignDate
            LocalDate date = demo.getFirstSignDate(1000, today);
            System.out.println("本月首次簽到:" + formatDate(date, "yyyy-MM-dd"));
        }

        { // getSignInfo
            System.out.println("當月簽到情況:");
            Map<String, Boolean> signInfo = new TreeMap<>(demo.getSignInfo(1000, today));
            for (Map.Entry<String, Boolean> entry : signInfo.entrySet()) {
                System.out.println(entry.getKey() + ": " + (entry.getValue() ? "√" : "-"));
            }
        }
    }

}
           

運作結果

您已簽到:2019-02-18
您已簽到:2019-02-18
本月簽到次數:11
連續簽到次數:8
本月首次簽到:2019-02-02
當月簽到情況:
2019-02-01: -
2019-02-02: √
2019-02-03: √
2019-02-04: -
2019-02-05: -
2019-02-06: √
2019-02-07: -
2019-02-08: -
2019-02-09: -
2019-02-10: -
2019-02-11: √
2019-02-12: √
2019-02-13: √
2019-02-14: √
2019-02-15: √
2019-02-16: √
2019-02-17: √
2019-02-18: √
2019-02-19: -
2019-02-20: -
2019-02-21: -
2019-02-22: -
2019-02-23: -
2019-02-24: -
2019-02-25: -
2019-02-26: -
2019-02-27: -
2019-02-28: -