Bitmaps in Redis
Redis Bitmaps provide a compact way to store binary data using individual bits (0 or 1) inside regular Redis strings. Each bit has its own index and can be set, cleared, or read efficiently. This makes bitmaps ideal for tracking simple yes/no states at scale—such as daily user activity or feature flags—while using very little memory. Bitwise operations also allow fast aggregate calculations like counting active users.
Tracking Daily User Logins with Bitmaps
Suppose you want to track which users log in each day using Redis bitmaps. Each bit in the bitmap represents a user; a value of 1 means the user logged in, and 0 means they did not.
You assign each user a unique numeric ID, starting from 0.
Marking a User Login
When a user logs in, you use the SETBIT command to set their bit to 1:
SETBIT logins:2024-06-01 42 1
This sets the bit at offset 42 in the bitmap logins:2024-06-01 to 1, meaning user 42 logged in on June 1, 2024.
Checking If a User Logged In
To check if user 42 logged in on June 1, use the GETBIT command:
GETBIT logins:2024-06-01 42
This returns 1 if the user logged in, or 0 if they did not.
Counting Total Logins for the Day
To find out how many users logged in on June 1, use the BITCOUNT command:
BITCOUNT logins:2024-06-01
This returns the total number of bits set to 1 in the bitmap, which is the number of users who logged in that day.
These commands help you efficiently store and analyze large-scale user activity data using minimal memory.
Danke für Ihr Feedback!
Fragen Sie AI
Fragen Sie AI
Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen
Can you explain how to track logins across multiple days?
How do I reset or clear the bitmap for a new day?
What are some common use cases for Redis bitmaps besides user logins?
Awesome!
Completion rate improved to 9.09
Bitmaps in Redis
Swipe um das Menü anzuzeigen
Redis Bitmaps provide a compact way to store binary data using individual bits (0 or 1) inside regular Redis strings. Each bit has its own index and can be set, cleared, or read efficiently. This makes bitmaps ideal for tracking simple yes/no states at scale—such as daily user activity or feature flags—while using very little memory. Bitwise operations also allow fast aggregate calculations like counting active users.
Tracking Daily User Logins with Bitmaps
Suppose you want to track which users log in each day using Redis bitmaps. Each bit in the bitmap represents a user; a value of 1 means the user logged in, and 0 means they did not.
You assign each user a unique numeric ID, starting from 0.
Marking a User Login
When a user logs in, you use the SETBIT command to set their bit to 1:
SETBIT logins:2024-06-01 42 1
This sets the bit at offset 42 in the bitmap logins:2024-06-01 to 1, meaning user 42 logged in on June 1, 2024.
Checking If a User Logged In
To check if user 42 logged in on June 1, use the GETBIT command:
GETBIT logins:2024-06-01 42
This returns 1 if the user logged in, or 0 if they did not.
Counting Total Logins for the Day
To find out how many users logged in on June 1, use the BITCOUNT command:
BITCOUNT logins:2024-06-01
This returns the total number of bits set to 1 in the bitmap, which is the number of users who logged in that day.
These commands help you efficiently store and analyze large-scale user activity data using minimal memory.
Danke für Ihr Feedback!