Databases 5 min read

Master Redis Bitmap Operations: PHP Guide for Precise User Sign‑In Stats

Redis bitmap commands enable efficient bit‑level storage and fast counting, and this article explains the core concepts, key commands like BITSET, BITGET, BITCOUNT, and provides a complete PHP example that tracks user sign‑ins and calculates daily totals using Predis.

php Courses
php Courses
php Courses
Master Redis Bitmap Operations: PHP Guide for Precise User Sign‑In Stats

Bitmap is a data structure used for compression and optimized storage, providing efficient bit‑level operations. Redis, a popular NoSQL database, supports bitmap operations that enable fast statistical functions.

1. Introduction to Redis Bitmap Operations

Bitmaps consist of consecutive binary bits where each bit can be 0 or 1. Redis offers a set of commands for reading, writing, and manipulating bits, such as BITSET, BITGET, and BITCOUNT, to create, retrieve, set, and count bits.

2. Common Redis Bitmap Commands

1. Create bitmap: BITSET sets a specific bit to a given value, e.g., BITSET key 0 1. 2. Get bitmap value: BITGET retrieves the value of a specific bit, e.g., BITGET key 0. 3. Count bits set to 1: BITCOUNT returns the number of bits set to 1 within a range, e.g., BITCOUNT key 0 9 to count bits from position 0 to 9.

3. Redis Bitmap Example

Below is a PHP code example that implements a user sign‑in statistics feature using Redis bitmap operations:

<?php
/**
 * Redis bitmap operation example: user sign‑in statistics
 */
// Include Redis library
require 'Predis/Autoloader.php';
PredisAutoloader::register();

// Connect to Redis
$redis = new PredisClient();

// Define user sign‑in function
function userSign($userId, $date)
{
    global $redis;
    $key = 'user_sign:' . $date;
    $redis->setbit($key, $userId, 1);
}

// Define function to get sign‑in count
function getUserSignCount($date)
{
    global $redis;
    $key = 'user_sign:' . $date;
    $count = $redis->bitcount($key);
    return $count;
}

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

// Get sign‑in count
$count = getUserSignCount($date);
echo "User sign‑in count: {$count}";
?>

The example sets bits for each user's sign‑in using setbit and then uses bitcount to count the number of bits set to 1, yielding the total sign‑ins for the specified date.

4. Conclusion

Redis bitmap operations provide an efficient way to achieve precise statistical functions. BITSET and BITGET manipulate specific bits, while BITCOUNT counts bits set to 1. The PHP example demonstrates how to use these commands to implement a user sign‑in counter.

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.

databaseredisBitmapPHPbitwise operationsPredis
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.