|
|
package main
|
|
|
|
|
|
import (
|
|
|
"context"
|
|
|
"fmt"
|
|
|
"github.com/go-redis/redis/v9"
|
|
|
)
|
|
|
|
|
|
// bitmap 操作测试
|
|
|
|
|
|
func main() {
|
|
|
|
|
|
// 设置获取
|
|
|
//bitmapSetGet()
|
|
|
|
|
|
// 位运算
|
|
|
//bitmapOP()
|
|
|
|
|
|
// 字符串操作
|
|
|
bitmapString()
|
|
|
|
|
|
}
|
|
|
|
|
|
func bitmapString() {
|
|
|
opt, err := redis.ParseURL("redis://default:yourPassword@192.168.157.135:6379/0?dial_timeout=1")
|
|
|
if err != nil {
|
|
|
panic(err)
|
|
|
}
|
|
|
|
|
|
client := redis.NewClient(opt)
|
|
|
ctx := context.Background()
|
|
|
|
|
|
client.Del(ctx, "keyString")
|
|
|
//client.SetBit(ctx, "keyString", 1, 1)
|
|
|
// 01
|
|
|
// 01000000
|
|
|
client.SetBit(ctx, "keyString", 0, 1)
|
|
|
// 1
|
|
|
// 10000000
|
|
|
fmt.Println(client.Get(ctx, "keyString").Result())
|
|
|
}
|
|
|
|
|
|
func bitmapOP() {
|
|
|
opt, err := redis.ParseURL("redis://default:yourPassword@192.168.157.135:6379/0?dial_timeout=1")
|
|
|
if err != nil {
|
|
|
panic(err)
|
|
|
}
|
|
|
|
|
|
client := redis.NewClient(opt)
|
|
|
ctx := context.Background()
|
|
|
|
|
|
client.Del(ctx, "keyOne")
|
|
|
client.Del(ctx, "keyTwo")
|
|
|
|
|
|
client.SetBit(ctx, "keyOne", 0, 1)
|
|
|
client.SetBit(ctx, "keyOne", 1, 0)
|
|
|
client.SetBit(ctx, "keyOne", 2, 0)
|
|
|
client.SetBit(ctx, "keyOne", 3, 1)
|
|
|
client.SetBit(ctx, "keyOne", 4, 1)
|
|
|
|
|
|
client.SetBit(ctx, "keyTwo", 0, 1)
|
|
|
client.SetBit(ctx, "keyTwo", 1, 1)
|
|
|
client.SetBit(ctx, "keyTwo", 2, 0)
|
|
|
client.SetBit(ctx, "keyTwo", 3, 0)
|
|
|
client.SetBit(ctx, "keyTwo", 4, 1)
|
|
|
|
|
|
// and
|
|
|
// 10011
|
|
|
// 11001
|
|
|
// 10001
|
|
|
client.BitOpAnd(ctx, "destKey", "keyOne", "keyTwo")
|
|
|
fmt.Println("And:")
|
|
|
fmt.Println(client.GetBit(ctx, "destKey", 0).Result())
|
|
|
fmt.Println(client.GetBit(ctx, "destKey", 1).Result())
|
|
|
fmt.Println(client.GetBit(ctx, "destKey", 2).Result())
|
|
|
fmt.Println(client.GetBit(ctx, "destKey", 3).Result())
|
|
|
fmt.Println(client.GetBit(ctx, "destKey", 4).Result())
|
|
|
|
|
|
// Or
|
|
|
// 10011
|
|
|
// 11001
|
|
|
// 11011
|
|
|
client.BitOpOr(ctx, "destKey", "keyOne", "keyTwo")
|
|
|
fmt.Println("Or:")
|
|
|
fmt.Println(client.GetBit(ctx, "destKey", 0).Result())
|
|
|
fmt.Println(client.GetBit(ctx, "destKey", 1).Result())
|
|
|
fmt.Println(client.GetBit(ctx, "destKey", 2).Result())
|
|
|
fmt.Println(client.GetBit(ctx, "destKey", 3).Result())
|
|
|
fmt.Println(client.GetBit(ctx, "destKey", 4).Result())
|
|
|
|
|
|
// Xor
|
|
|
// 10011
|
|
|
// 11001
|
|
|
// 01010
|
|
|
client.BitOpXor(ctx, "destKey", "keyOne", "keyTwo")
|
|
|
fmt.Println("Xor:")
|
|
|
fmt.Println(client.GetBit(ctx, "destKey", 0).Result())
|
|
|
fmt.Println(client.GetBit(ctx, "destKey", 1).Result())
|
|
|
fmt.Println(client.GetBit(ctx, "destKey", 2).Result())
|
|
|
fmt.Println(client.GetBit(ctx, "destKey", 3).Result())
|
|
|
fmt.Println(client.GetBit(ctx, "destKey", 4).Result())
|
|
|
|
|
|
// Xor
|
|
|
// 10011
|
|
|
// 01100
|
|
|
client.BitOpNot(ctx, "destKey", "keyOne")
|
|
|
fmt.Println("Not:")
|
|
|
fmt.Println(client.GetBit(ctx, "destKey", 0).Result())
|
|
|
fmt.Println(client.GetBit(ctx, "destKey", 1).Result())
|
|
|
fmt.Println(client.GetBit(ctx, "destKey", 2).Result())
|
|
|
fmt.Println(client.GetBit(ctx, "destKey", 3).Result())
|
|
|
fmt.Println(client.GetBit(ctx, "destKey", 4).Result())
|
|
|
|
|
|
}
|
|
|
|
|
|
func bitmapSetGet() {
|
|
|
opt, err := redis.ParseURL("redis://default:yourPassword@192.168.157.135:6379/0?dial_timeout=1")
|
|
|
if err != nil {
|
|
|
panic(err)
|
|
|
}
|
|
|
|
|
|
client := redis.NewClient(opt)
|
|
|
ctx := context.Background()
|
|
|
|
|
|
//// 设置
|
|
|
client.Del(ctx, "userLog")
|
|
|
//// 10011
|
|
|
//client.SetBit(ctx, "userLog", 0, 1)
|
|
|
//client.SetBit(ctx, "userLog", 1, 0)
|
|
|
//client.SetBit(ctx, "userLog", 2, 0)
|
|
|
//client.SetBit(ctx, "userLog", 3, 1)
|
|
|
//client.SetBit(ctx, "userLog", 4, 1)
|
|
|
//
|
|
|
//// 获取
|
|
|
//fmt.Println(client.GetBit(ctx, "userLog", 0).Result())
|
|
|
//fmt.Println(client.GetBit(ctx, "userLog", 1).Result())
|
|
|
//fmt.Println(client.GetBit(ctx, "userLog", 2).Result())
|
|
|
//fmt.Println(client.GetBit(ctx, "userLog", 3).Result())
|
|
|
//fmt.Println(client.GetBit(ctx, "userLog", 4).Result())
|
|
|
//
|
|
|
//// 未设置的位,为:0
|
|
|
//fmt.Println(client.GetBit(ctx, "userLog", 5).Result())
|
|
|
//fmt.Println(client.GetBit(ctx, "userLog", 1<<10-1).Result())
|
|
|
|
|
|
// 大 offset
|
|
|
// 2^32-1
|
|
|
// 0000000 ..... 00000001
|
|
|
// setBit 会分配内存空间,第一次使用较大的(很大的)offset时,会消耗部分时间,进行内存分配
|
|
|
//client.SetBit(ctx, "big", 1<<32-1, 1)
|
|
|
//fmt.Println(client.GetBit(ctx, "userLog", 1<<32-1).Result())
|
|
|
//fmt.Println(client.GetBit(ctx, "userLog", 1<<32-2).Result())
|
|
|
// memory usage <key>
|
|
|
// 671,088,688
|
|
|
// overflow
|
|
|
//result := client.SetBit(ctx, "bigOver", 1<<32, 1)
|
|
|
//fmt.Println(result.Result())
|
|
|
|
|
|
// 统计 1 的数量(1 set bit , 0 clear bit)
|
|
|
client.SetBit(ctx, "userLog", 0, 1)
|
|
|
client.SetBit(ctx, "userLog", 1, 0)
|
|
|
client.SetBit(ctx, "userLog", 2, 0)
|
|
|
client.SetBit(ctx, "userLog", 3, 1)
|
|
|
client.SetBit(ctx, "userLog", 4, 1)
|
|
|
client.SetBit(ctx, "userLog", 7, 1)
|
|
|
client.SetBit(ctx, "userLog", 8, 1)
|
|
|
client.SetBit(ctx, "userLog", 15, 1)
|
|
|
client.SetBit(ctx, "userLog", 16, 1)
|
|
|
|
|
|
fmt.Println(client.BitCount(ctx, "userLog", &redis.BitCount{
|
|
|
// 以字节为单位,控制统计范围,闭区间
|
|
|
// 1bytes == 8bits
|
|
|
// 默认第一个字节
|
|
|
//Start: 0,
|
|
|
//End: 0,
|
|
|
|
|
|
// 统计全部
|
|
|
Start: 0,
|
|
|
End: -1, // -1 最后一个字节,-2 倒数第二
|
|
|
}).Result())
|
|
|
|
|
|
}
|