Using bitmaps
A bitmap (also known as a bit array, or bit vector) is an array of bits. A Redis bitmap is not a new data type; its actual underlying data type is string. Since a string is inherently a binary blob, it can be viewed as a bitmap. A bitmap saves enormous memory space for storing boolean information under certain circumstances.
In this recipe, we will use a bitmap to store a flag, whether or not a user has ever used a feature in Relp. Suppose that in Relp every user has a unique and incremental id, which can be denoted by the bitmap offset. The flag, which is a boolean attribute, can be denoted by the bit value in the bitmap.
Getting ready…
You need to finish the installation of the Redis Server as we described in the Downloading and installing Redis recipe in Chapter 1, Getting Started with Redis.
How to do it…
The steps for using bitmaps are as follows:
- Open a Terminal and connect to Redis with
redis-
cli - Use
SETBIT
to set the bit value in a bitmap at the specified offset.
For example...