This Go program demonstrates the usage of Redis bitfields to store and retrieve voting information for citizens. Each citizen is assigned a unique ID, and their vote is stored as a bit in a Redis bitfield.
Make sure you have Go installed on your machine. You also need a Redis server running and accessible.
go get -u github.com/go-redis/redis/v8
- Clone this repository:
git clone https://github.com/naruepanart/redis-bitfields.git
- Navigate to the project directory:
cd naruepanart/redis-bitfields
- Update the Redis server details in the
main.go
file:
const (
redisAddress = "your-redis-address"
redisPassword = "your-redis-password"
cacheKey = "vote"
)
- Run the program:
go run main.go
The program will generate a random Citizen ID and Vote Number, set the corresponding bit in the Redis bitfield, and then retrieve and print the bit value.
redisAddress
: Address of the Redis server.redisPassword
: Password for connecting to the Redis server.cacheKey
: Redis key used for storing bitfield data.
getRandomNumber(min, max int) int
: Generates a random number betweenmin
andmax
(inclusive).setBitField(client *redis.Client, cacheKey string, position int, value int) error
: Sets the bit at the given position in the Redis bitfield.getBitField(client *redis.Client, cacheKey string, position int) (int64, error)
: Retrieves the bit at the given position from the Redis bitfield.
This project is licensed under the MIT License - see the LICENSE file for details.