Databases 5 min read

Master Redis Bitmap Operations in PHP for Precise User Sign‑In Counting

This tutorial explains Redis bitmap fundamentals, showcases common BITSET, BITGET, and BITCOUNT commands, and provides a complete PHP example that records daily user sign‑ins and accurately counts them using Redis bit operations.

php Courses
php Courses
php Courses
Master Redis Bitmap Operations in PHP for Precise User Sign‑In Counting

Bitmaps are compact data structures composed of binary bits that enable efficient bit‑level storage and operations. Redis, a popular NoSQL database, includes native bitmap commands, allowing fast creation, manipulation, and statistical analysis of bitmaps.

1. Introduction to Redis Bitmap Operations

Redis bitmap commands let you read, write, and query individual bits within a continuous binary string. The primary commands are BITSET (set a bit), BITGET (retrieve a bit), and BITCOUNT (count bits set to 1).

2. Common Redis Bitmap Commands

BITSET : Sets the bit at a specified offset to 0 or 1, e.g., BITSET key 0 1.

BITGET : Retrieves the value of the bit at a given offset, e.g., BITGET key 0.

BITCOUNT : Counts the number of bits set to 1 within a range, e.g., BITCOUNT key 0 9 counts bits 0 through 9.

3. PHP Example: User Sign‑In Statistics with Redis Bitmaps

The following PHP script demonstrates how to record daily user sign‑ins using SETBIT and then obtain the total number of sign‑ins with BITCOUNT:

<?php
/**
 * Redis Bitmap Example: User Sign‑In Statistics
 */
require 'Predis/Autoloader.php';
PredisAutoloader::register();
$redis = new PredisClient();

function userSign($userId, $date) {
    global $redis;
    $key = 'user_sign:' . $date;
    $redis->setbit($key, $userId, 1);
}

function getUserSignCount($date) {
    global $redis;
    $key = 'user_sign:' . $date;
    $count = $redis->bitcount($key);
    return $count;
}

$user1 = 1;
$user2 = 2;
$date = date('Ymd');
userSign($user1, $date);
userSign($user2, $date);

$count = getUserSignCount($date);
echo "用户签到统计数为:{$count}";
?>

The script defines userSign to set a bit for a specific user ID on a given date, and getUserSignCount to count all bits set to 1 for that date, effectively yielding the number of users who signed in.

4. Conclusion

Redis bitmap operations provide a high‑performance way to implement precise counting features. By leveraging BITSET / SETBIT, BITGET, and BITCOUNT, developers can efficiently store and query large sets of boolean states, as illustrated by the PHP sign‑in example.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BitmapPHPbitwise operations
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.