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:

<code>&lt;?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}";
?>
</code>

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.

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

login 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.