master
Han Joker 12 months ago
commit d6dce245cf

@ -0,0 +1,32 @@
package main
import (
"context"
"fmt"
"github.com/go-redis/redis/v9"
)
func main() {
// redis 客户端
// 此时不会建立连接
//client := redis.NewClient(&redis.Options{
// Addr: "192.168.157.135:3679",
// Username: "default", // ACL 的默认用户名
// Password: "yourPassword",
// DB: 0,
// DialTimeout: 1 * time.Second,
//})
// URL 方式配置选项
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)
fmt.Println(client)
// 执行操作时,需要获取连接
status := client.Ping(context.Background())
fmt.Println(status.Result())
}

@ -0,0 +1,181 @@
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())
}

@ -0,0 +1,38 @@
package main
import (
"context"
"fmt"
"github.com/go-redis/redis/v9"
"time"
)
func main() {
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()
//ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
fmt.Println("begin")
//defer cancel()
//select {
//case <-ctx.Done():
// fmt.Println(ctx.Err())
//}
_, err = client.TxPipelined(ctx, func(pipe redis.Pipeliner) error {
client.Do(ctx, "HTRRLEN", "KEY", "FIELD")
s1 := pipe.Get(ctx, "key1")
fmt.Println(s1.Result())
time.Sleep(3 * time.Second)
s2 := pipe.Get(ctx, "key2")
fmt.Println(s2.Result())
return nil
})
if err != nil {
fmt.Println(err)
}
}

@ -0,0 +1,157 @@
package main
import (
"context"
"fmt"
"github.com/go-redis/redis/v9"
)
func main() {
// 添加和获取信息
//geoAddGet()
// 搜索地点成员
geoSearch()
}
func geoSearch() {
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, "cities")
cities := []*redis.GeoLocation{
{Name: "changsha", Longitude: 113.00000, Latitude: 28.21667},
{Name: "beijing", Longitude: 116.41667, Latitude: 39.91667},
{Name: "shanghai", Longitude: 121.43333, Latitude: 34.50000},
{Name: "guangzhou", Longitude: 113.23333, Latitude: 23.16667},
{Name: "shenzhen", Longitude: 114.06667, Latitude: 22.61667},
{Name: "tianjin", Longitude: 117.20000, Latitude: 39.13333},
{Name: "hangzhou", Longitude: 120.20000, Latitude: 30.26667},
{Name: "chengdu", Longitude: 104.06667, Latitude: 30.66667},
{Name: "xian", Longitude: 108.95000, Latitude: 34.26667},
{Name: "changchun", Longitude: 125.35000, Latitude: 43.88333},
{Name: "wulumuqi", Longitude: 87.68333, Latitude: 43.76667},
}
client.GeoAdd(ctx, "cities", cities...).Result()
// 搜索某个距离成员距离范围的其他成员
// 一,圆的范围,使用成员确定圆心
fmt.Println(client.GeoSearch(ctx, "cities", &redis.GeoSearchQuery{
Member: "hangzhou",
Radius: 1000,
RadiusUnit: "km",
}).Result())
// 可以返回 坐标、距离和Hash
fmt.Println(client.GeoSearchLocation(ctx, "cities", &redis.GeoSearchLocationQuery{
GeoSearchQuery: redis.GeoSearchQuery{
Member: "hangzhou",
Radius: 1000,
RadiusUnit: "km",
},
WithCoord: true,
WithDist: true,
WithHash: true,
}).Result())
// 二,圆的范围,使用坐标确定圆心
fmt.Println(client.GeoSearch(ctx, "cities", &redis.GeoSearchQuery{
Longitude: 120.20000,
Latitude: 30.26667,
Radius: 1000,
RadiusUnit: "km",
}).Result())
// 三,矩形的范围,使用坐标(成员)确定中心
fmt.Println(client.GeoSearch(ctx, "cities", &redis.GeoSearchQuery{
Member: "hangzhou",
// OR
//Longitude: 120.20000,
//Latitude: 30.26667,
// 宽高
BoxWidth: 1000,
BoxHeight: 1000,
BoxUnit: "km",
}).Result())
// 四,搜素结果过滤
// 可以返回 坐标、距离和Hash
fmt.Println(client.GeoSearchLocation(ctx, "cities", &redis.GeoSearchLocationQuery{
GeoSearchQuery: redis.GeoSearchQuery{
Member: "hangzhou",
Radius: 1000,
RadiusUnit: "km",
},
WithCoord: true,
WithDist: true,
WithHash: true,
}).Result())
fmt.Println(client.GeoSearchLocation(ctx, "cities", &redis.GeoSearchLocationQuery{
GeoSearchQuery: redis.GeoSearchQuery{
Member: "hangzhou",
Radius: 1000,
RadiusUnit: "km",
//Sort: "DESC", // ASC, DESC
Count: 2,
CountAny: true,
},
WithCoord: true,
WithDist: true,
WithHash: true,
}).Result())
}
func geoAddGet() {
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, "cities")
// 添加
fmt.Println(client.GeoAdd(ctx, "cities", &redis.GeoLocation{
Name: "changsha",
Longitude: 113.00001,
Latitude: 28.21667,
}).Result())
cities := []*redis.GeoLocation{
{Name: "changsha", Longitude: 113.00000, Latitude: 28.21667},
{Name: "beijing", Longitude: 116.41667, Latitude: 39.91667},
{Name: "shanghai", Longitude: 121.43333, Latitude: 34.50000},
{Name: "guangzhou", Longitude: 113.23333, Latitude: 23.16667},
{Name: "shenzhen", Longitude: 114.06667, Latitude: 22.61667},
{Name: "tianjin", Longitude: 117.20000, Latitude: 39.13333},
{Name: "hangzhou", Longitude: 120.20000, Latitude: 30.26667},
{Name: "chengdu", Longitude: 104.06667, Latitude: 30.66667},
{Name: "xian", Longitude: 108.95000, Latitude: 34.26667},
{Name: "changchun", Longitude: 125.35000, Latitude: 43.88333},
{Name: "wulumuqi", Longitude: 87.68333, Latitude: 43.76667},
}
fmt.Println(client.GeoAdd(ctx, "cities", cities...).Result())
// 获取信息
// 获取坐标
poses, _ := client.GeoPos(ctx, "cities", "changsha", "beijing").Result()
for _, pos := range poses {
fmt.Println(pos)
}
// 获取 geohash 位置
fmt.Println(client.GeoHash(ctx, "cities", "changsha", "beijing").Result())
// 获取距离
fmt.Println(client.GeoDist(ctx, "cities", "changsha", "beijing", "km").Result()) // m km mi ft
}

@ -0,0 +1,54 @@
package main
import (
"context"
"fmt"
"github.com/go-redis/redis/v9"
)
func main() {
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()
result := client.HSet(ctx, "students", "42001", "Mashibing", "42002", "GoLang", "42003", "Redis")
fmt.Println(result.Result())
fmt.Println(client.HGetAll(ctx, "students").Result())
fmt.Println(
client.HLen(ctx, "students").
Result()) // 3 <nil>
fmt.Println(
client.HRandField(ctx, "students", 2).
Result()) // [42003 42001] <nil>, [42002 42003] <nil>
fmt.Println(
client.HRandField(ctx, "students", -2).
Result()) // [42003 42001] <nil>, [42003 42003] <nil>
fmt.Println(
client.HRandFieldWithValues(ctx, "students", 2).
Result()) // [{42002 GoLang} {42003 Redis}] <nil>
fmt.Println(
client.HExists(ctx, "students", "42003").
Result()) // true <nil>
fmt.Println(
client.HExists(ctx, "students", "42006").
Result()) // false <nil>
fmt.Println(
client.HDel(ctx, "students", "42003").
Result()) // 1 <nil>
fmt.Println(
client.HExists(ctx, "students", "42003").
Result()) // false <nil>
fmt.Println(
client.Do(ctx, "HSTRLEN", "students", "42001").
Result()) // 9 <nil>
}

@ -0,0 +1,36 @@
package main
import (
"context"
"fmt"
"github.com/go-redis/redis/v9"
)
func main() {
hyperloglogOp()
}
func hyperloglogOp() {
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, "ViewsUser")
// 添加成员
client.PFAdd(ctx, "ViewsUser", "zhao", "qian", "sun")
// 获取成员数量
fmt.Println(client.PFCount(ctx, "ViewsUser").Result())
client.PFAdd(ctx, "ViewsUser", "li", "zhou", "sun")
fmt.Println(client.PFCount(ctx, "ViewsUser").Result())
// 合并
client.PFAdd(ctx, "user1", "zhao", "qian", "sun")
client.PFAdd(ctx, "user2", "li", "zhou", "sun")
client.PFMerge(ctx, "users", "user1", "user2")
fmt.Println(client.PFCount(ctx, "users").Result())
}

@ -0,0 +1,146 @@
package main
import (
"context"
"fmt"
"github.com/go-redis/redis/v9"
"time"
)
func main() {
// 插入和获取头部和尾部左边和右边L和R
//pushPop()
// 基于索引操作
//index()
// 两个队列元素传递
//two()
// 支持阻塞的操作
block()
}
func block() {
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()
// non-block
client.Del(ctx, "list-src", "list-dest")
//client.LPush(ctx, "list-src", "GO", "Redis", "MySQL")
fmt.Println(client.LPop(ctx, "list-src").Result())
// block
fmt.Println(client.BLPop(ctx, 10*time.Second, "list-src").Result())
}
func two() {
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, "list-src", "list-dest")
client.LPush(ctx, "list-src", "GO", "Redis", "MySQL")
client.LPush(ctx, "list-dest", "Docker", "K8S", "CI/CD")
// rpoplpush
//client.RPopLPush(ctx, "list-src", "list-dest")
//fmt.Println(client.LRange(ctx, "list-src", 0, -1).Result())
//fmt.Println(client.LRange(ctx, "list-dest", 0, -1).Result())
// move
client.LMove(ctx, "list-src", "list-dest", "RIGHT", "LEFT")
fmt.Println(client.LRange(ctx, "list-src", 0, -1).Result())
fmt.Println(client.LRange(ctx, "list-dest", 0, -1).Result())
}
// 基于索引操作
func index() {
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, "subjects")
client.LPush(ctx, "subjects", "GO", "Redis", "MySQL", "Go-Redis", "Go-Redis", "K8S", "CI/CD")
fmt.Println(client.LRange(ctx, "subjects", 0, -1).Result())
fmt.Println(client.LIndex(ctx, "subjects", 3))
client.LSet(ctx, "subjects", 3, "Go-Redis")
fmt.Println(client.LRange(ctx, "subjects", 0, -1).Result())
//client.LInsert(ctx, "subjects", "BEFORE", "Go-Redis", "NewSubject")
client.LInsert(ctx, "subjects", "AFTER", "Go-Redis", "NewSubject")
fmt.Println(client.LRange(ctx, "subjects", 0, -1).Result())
client.LTrim(ctx, "subjects", 2, 4)
fmt.Println(client.LRange(ctx, "subjects", 0, -1).Result())
}
// 插入和获取头部和尾部左边和右边L和R
func pushPop() {
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.LTrim(ctx, "subjects", -1, 0)
client.Del(ctx, "subjects")
// 插入
client.LPush(ctx, "subjects", "GO")
// 获取长度
//fmt.Println(client.LLen(ctx, "subjects").Result())
client.LPush(ctx, "subjects", "Redis", "MySQL")
client.LPush(ctx, "subjects", "Redis", "MySQL")
client.LPush(ctx, "subjects", "Redis", "MySQL")
client.RPush(ctx, "subjects", "Docker")
// 获取长度
//fmt.Println(client.LLen(ctx, "subjects").Result())
client.RPush(ctx, "subjects", "K8S", "CI/CD")
// 获取长度
//fmt.Println(client.LLen(ctx, "subjects").Result())
// 查看
fmt.Println(client.LRange(ctx, "subjects", 0, -1).Result())
// 删除元素
// count:
// >0, 从头部开始删除 count 个
// <0, 从尾部开始删除 count 个
// =0, 删除全部
client.LRem(ctx, "subjects", 0, "MySQL")
// 插入,前提是 key 要存在
//fmt.Println(client.LPushX(ctx, "subjects", "GO").Result())
// 查看
fmt.Println(client.LRange(ctx, "subjects", 0, -1).Result())
// 取出
// Get and Del
//fmt.Println(client.LPop(ctx, "subjects").Result())
//fmt.Println(client.LRange(ctx, "subjects", 0, -1).Result())
//fmt.Println(client.RPop(ctx, "subjects").Result())
//fmt.Println(client.LRange(ctx, "subjects", 0, -1).Result())
//fmt.Println(client.RPopCount(ctx, "subjects", 3).Result())
//fmt.Println(client.LRange(ctx, "subjects", 0, -1).Result())
}

@ -0,0 +1,32 @@
package main
import (
"context"
"fmt"
"github.com/go-redis/redis/v9"
)
func main() {
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()
val, _ := client.Get(ctx, "someKey").Result()
fmt.Printf("%#v", val) // ""
val, err := client.Get(ctx, "someKey").Result()
switch {
case err == redis.Nil:
fmt.Println("key does not exist")
case err != nil:
fmt.Println("Get failed", err)
case val == "":
fmt.Println("value is empty")
}
}

@ -0,0 +1,91 @@
package main
import (
"context"
"fmt"
"github.com/go-redis/redis/v9"
)
func main() {
// basic
//basic()
// 集合运算
op()
}
func op() {
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, "A")
client.Del(ctx, "A")
client.SAdd(ctx, "A", "1", "2")
client.SAdd(ctx, "B", "2", "3")
// 交集
fmt.Println(client.SInter(ctx, "A", "B").Result())
// 并集
fmt.Println(client.SUnion(ctx, "A", "B").Result())
// 差集
fmt.Println(client.SDiff(ctx, "A", "B").Result())
fmt.Println(client.SDiff(ctx, "B", "A").Result())
// 运算完存储
client.SInterStore(ctx, "dest", "A", "B")
fmt.Println(client.SMembers(ctx, "dest").Result())
}
func basic() {
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, "ips")
// 添加
client.SAdd(ctx, "ips", "129.66.122.123")
fmt.Println(client.SMembers(ctx, "ips").Result())
client.SAdd(ctx, "ips", "129.66.122.123", "129.66.122.124", "129.66.122.125")
// 获取测试
fmt.Println(client.SMembers(ctx, "ips").Result())
// 随机获取一个
fmt.Println(client.SRandMember(ctx, "ips").Result())
fmt.Println(client.SRandMember(ctx, "ips").Result())
fmt.Println(client.SRandMember(ctx, "ips").Result())
// 多个
fmt.Println(client.SRandMemberN(ctx, "ips", 2).Result())
fmt.Println(client.SRandMemberN(ctx, "ips", 2).Result())
fmt.Println(client.SRandMemberN(ctx, "ips", 2).Result())
// 获取全部
fmt.Println(client.SMembers(ctx, "ips").Result())
// 取出成员,获取成员,并删除
//fmt.Println(client.SPop(ctx, "ips").Result())
//fmt.Println(client.SMembers(ctx, "ips").Result())
// 统计成员信息
fmt.Println(client.SCard(ctx, "ips").Result())
// 删除成员
client.SRem(ctx, "ips", "129.66.122.124")
fmt.Println(client.SMembers(ctx, "ips").Result())
fmt.Println(client.SCard(ctx, "ips").Result())
fmt.Println(client.SIsMember(ctx, "ips", "129.66.122.124").Result())
fmt.Println(client.SIsMember(ctx, "ips", "129.66.122.123").Result())
fmt.Println(client.SMIsMember(ctx, "ips", "129.66.122.124", "129.66.122.123").Result())
}

@ -0,0 +1,381 @@
package main
import (
"context"
"fmt"
"github.com/go-redis/redis/v9"
)
func main() {
// add
//add()
//get()
//getByLex()
// pop
//pop()
//remove
//remove()
// count
//count()
// 交集差集并集
setOp()
}
func setOp() {
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, "A")
client.ZAdd(ctx, "A",
redis.Z{Score: 1, Member: "one"},
redis.Z{Score: 2, Member: "two"},
redis.Z{Score: 3, Member: "three"},
)
client.Del(ctx, "B")
client.ZAdd(ctx, "B",
redis.Z{Score: 22, Member: "two"},
redis.Z{Score: 33, Member: "three"},
redis.Z{Score: 44, Member: "four"},
)
// 交集
fmt.Println(client.ZInterWithScores(ctx, &redis.ZStore{
// 哪些key参与
Keys: []string{"A", "B"},
// 权重,聚合时,为每个集合成员增加的权重
Weights: []float64{2, 3},
// 聚合方法,默认为 SUM
Aggregate: "SUM", // 和
//Aggregate: "MIN", // 使用小的
//Aggregate: "MAX", // 使用大的
}).Result())
// 并集
fmt.Println(client.ZUnionWithScores(ctx, redis.ZStore{
// 哪些key参与
Keys: []string{"A", "B"},
// 权重,聚合时,为每个集合成员增加的权重
Weights: []float64{2, 3},
// 聚合方法,默认为 SUM
Aggregate: "SUM", // 和
//Aggregate: "MIN", // 使用小的
//Aggregate: "MAX", // 使用大的
}).Result())
// 差集
// 不需要聚合运算,保留本集合内的成员集合
fmt.Println(client.ZDiffWithScores(ctx, "A", "B").Result())
fmt.Println(client.ZDiffWithScores(ctx, "B", "A").Result())
}
func count() {
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, "contentViews")
client.ZAdd(ctx, "contentViews",
redis.Z{Score: 4, Member: "jia"},
redis.Z{Score: 2, Member: "yi"},
redis.Z{Score: 5, Member: "bing"},
redis.Z{Score: 1, Member: "ding"},
redis.Z{Score: 0, Member: "wu"},
redis.Z{Score: 7, Member: "ji"},
redis.Z{Score: 2, Member: "geng"},
redis.Z{Score: 1, Member: "xin"},
redis.Z{Score: 8, Member: "ren"},
redis.Z{Score: 6, Member: "gui"},
)
fmt.Println(client.ZCard(ctx, "contentViews").Result())
fmt.Println(client.ZCount(ctx, "contentViews", "0", "5").Result())
//fmt.Println(client.ZCount(ctx, "contentViews", "0", "5").Result())
// 获取某个成员的排名
fmt.Println(client.ZRank(ctx, "contentViews", "xin"))
}
func remove() {
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, "contentViews")
client.ZAdd(ctx, "contentViews",
redis.Z{Score: 0, Member: "jia"},
redis.Z{Score: 0, Member: "yi"},
redis.Z{Score: 0, Member: "bing"},
redis.Z{Score: 0, Member: "ding"},
redis.Z{Score: 0, Member: "wu"},
redis.Z{Score: 0, Member: "ji"},
redis.Z{Score: 0, Member: "geng"},
redis.Z{Score: 0, Member: "xin"},
redis.Z{Score: 0, Member: "ren"},
redis.Z{Score: 0, Member: "gui"},
)
fmt.Println(client.ZRangeWithScores(ctx, "contentViews", 0, -1).Result())
// 基于确定成员删除
//client.ZRem(ctx, "contentViews", "ji", "bing", "geng")
//fmt.Println(client.ZRangeWithScores(ctx, "contentViews", 0, -1).Result())
// 基于分值删除范围
//client.ZRemRangeByScore(ctx, "contentViews", "0", "5")
//client.ZRemRangeByRank(ctx, "contentViews", 3, 6)
// 注意全部成员分值要是0
//client.ZRemRangeByLex(ctx, "contentViews", "[a", "[h")
//fmt.Println(client.ZRangeWithScores(ctx, "contentViews", 0, -1).Result())
}
func pop() {
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, "contentViews")
client.ZAdd(ctx, "contentViews",
redis.Z{Score: 4, Member: "jia"},
redis.Z{Score: 2, Member: "yi"},
redis.Z{Score: 5, Member: "bing"},
redis.Z{Score: 1, Member: "ding"},
redis.Z{Score: 0, Member: "wu"},
redis.Z{Score: 7, Member: "ji"},
redis.Z{Score: 2, Member: "geng"},
redis.Z{Score: 1, Member: "xin"},
redis.Z{Score: 8, Member: "ren"},
redis.Z{Score: 6, Member: "gui"},
)
// 基于score
fmt.Println(client.ZPopMin(ctx, "contentViews", 4).Result())
fmt.Println(client.ZRange(ctx, "contentViews", 0, -1).Result())
}
func getByLex() {
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, "contentViews")
client.ZAdd(ctx, "contentViews",
redis.Z{Score: 0, Member: "jia"},
redis.Z{Score: 0, Member: "yi"},
redis.Z{Score: 0, Member: "bing"},
redis.Z{Score: 0, Member: "ding"},
redis.Z{Score: 0, Member: "wu"},
redis.Z{Score: 0, Member: "ji"},
redis.Z{Score: 0, Member: "geng"},
redis.Z{Score: 0, Member: "xin"},
redis.Z{Score: 0, Member: "ren"},
redis.Z{Score: 0, Member: "gui"},
)
fmt.Println(client.ZRangeArgs(ctx, redis.ZRangeArgs{
Key: "contentViews",
// [a, h]
//Start: "[a",
//Stop: "[h",
//
Start: "-",
Stop: "+",
// 基于成员(名字)获取
ByLex: true,
}).Result())
}
func get() {
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, "contentViews")
client.ZAdd(ctx, "contentViews",
redis.Z{Score: 4, Member: "jia"},
redis.Z{Score: 2, Member: "yi"},
redis.Z{Score: 5, Member: "bing"},
redis.Z{Score: 1, Member: "ding"},
redis.Z{Score: 0, Member: "wu"},
redis.Z{Score: 7, Member: "ji"},
redis.Z{Score: 2, Member: "geng"},
redis.Z{Score: 1, Member: "xin"},
redis.Z{Score: 8, Member: "ren"},
redis.Z{Score: 6, Member: "gui"},
)
// Range获取范围
//fmt.Println(client.ZRange(ctx, "contentViews", 0, -1).Result())
//fmt.Println(client.ZRangeWithScores(ctx, "contentViews", 0, -1).Result())
//fmt.Println(client.ZRangeArgsWithScores(ctx, redis.ZRangeArgs{
// Key: "contentViews",
// Start: nil,
// Stop: nil,
// // 基于分值获取
// ByScore: false,
// // 基于成员(名字)获取
// ByLex: false,
// Rev: false,
// // 偏移
// Offset: 0,
// // 总计0表示全部
// Count: 0,
//}))
fmt.Println(client.ZRangeArgsWithScores(ctx, redis.ZRangeArgs{
Key: "contentViews",
// [3, 7]
//Start: 3,
//Stop: 7,
// [3, 7)
//Start: 3,
//Stop: "(7",
// -inf, 7)
Start: "-inf",
Stop: "(7",
// 基于分值获取
ByScore: true,
// 偏移
Offset: 3,
// 总计0表示全部
Count: 3,
}).Result())
//fmt.Println(client.ZRangeArgs())
//// 随机获取
//fmt.Println(client.ZRandMemberWithScores(ctx, "contentViews", 3).Result())
//fmt.Println(client.ZRandMemberWithScores(ctx, "contentViews", 3).Result())
//fmt.Println(client.ZRandMemberWithScores(ctx, "contentViews", 3).Result())
//
//// 获取某个成员的分值
//fmt.Println(client.ZMScore(ctx, "contentViews", "gui", "jia", "ji").Result())
}
func add() {
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, "contentViews")
// 快速添加
//client.ZAdd(ctx, "contentViews", redis.Z{
// Score: 4,
// Member: "jia",
//}, redis.Z{
// Score: 3,
// Member: "yi",
//}, redis.Z{
// Score: 2,
// Member: "bing",
//}, redis.Z{
// Score: 10,
// Member: "ding",
//})
// 完整参数版的 ZAdd相当于 ZADD 命令
client.ZAddArgs(ctx, "contentViews", redis.ZAddArgs{
// 仅仅添加成员,不会去更新当前成员
NX: false,
// 仅仅更新存在的成员,不会去添加成员
XX: false,
// 只有当成员的新Score小于原有Score时才去更新。成员是已经存在的
LT: false,
// 只有当成员的新Score大于原有Score时才去更新。成员是已经存在的
GT: false,
// 返回修改的成员数量,包含(添加的和更新的)。默认仅返回添加的
Ch: false,
// []Z
Members: []redis.Z{
{
Score: 4,
Member: "jia",
}, {
Score: 3,
Member: "yi",
}, {
Score: 2,
Member: "bing",
}, {
Score: 10,
Member: "ding",
}},
})
// 查看
fmt.Println(client.ZRangeWithScores(ctx, "contentViews", 0, -1).Result())
//result := client.ZAddArgs(ctx, "contentViews", redis.ZAddArgs{
// // 仅仅添加成员,不会去更新当前成员
// //NX: true,
// // 仅仅更新存在的成员,不会去添加成员
// //XX: true,
// // 只有当成员的新Score小于原有Score时才去更新。成员是已经存在的
// //LT: true,
// // 只有当成员的新Score大于原有Score时才去更新。成员是已经存在的
// //GT: true,
// // 返回修改的成员数量,包含(添加的和更新的)。默认仅返回添加的
// Ch: true,
// // []Z
// Members: []redis.Z{
// {
// Score: 44,
// Member: "jia",
// }, {
// Score: 20,
// Member: "wu",
// }},
//})
//fmt.Println(result.Result())
// 递增更新
//client.ZIncrBy(ctx, "contentViews", 20, "bing")
client.ZIncrBy(ctx, "contentViews", -20, "bing")
client.ZAddArgsIncr(ctx, "contentViews", redis.ZAddArgs{
NX: false,
XX: false,
LT: false,
GT: false,
Ch: false,
Members: nil,
})
// 查看
fmt.Println(client.ZRangeWithScores(ctx, "contentViews", 0, -1).Result())
}

@ -0,0 +1,145 @@
package main
import (
"context"
"fmt"
"github.com/go-redis/redis/v9"
"time"
)
func main() {
// 设置获取
//setGet()
// 递增、递减、追加
//appendIncrDecr()
// 子串操作
sub()
}
// 子串操作
func sub() {
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.Set(ctx, "name", "MaShiBing", 0)
// GetRange
//fmt.Println(client.GetRange(ctx, "name", 0, 4).Result())
//fmt.Println(client.GetRange(ctx, "name", -4, -1).Result())
//fmt.Println(client.GetRange(ctx, "name", -4, 16).Result())
//fmt.Println(client.GetRange(ctx, "name", -16, 16).Result())
//// 全部内容
//fmt.Println(client.GetRange(ctx, "name", 0, -1).Result())
// SetRange
//client.SetRange(ctx, "name", 0, "Go")
//fmt.Println(client.Get(ctx, "name").Result())
//client.SetRange(ctx, "name", 4, "Go")
//fmt.Println(client.Get(ctx, "name").Result())
client.SetRange(ctx, "name", 4, "GoRedisGo")
fmt.Println(client.Get(ctx, "name").Result())
}
// 递增、递减、追加
func appendIncrDecr() {
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()
// append
//client.Set(ctx, "name", "Ma", 0)
//// "Ma" + "Shi"
//client.Append(ctx, "name", "Shi")
//status := client.Append(ctx, "name", "Bing")
//fmt.Println(status.Result())
//result := client.Get(ctx, "name")
//fmt.Println(result.Result())
// incr, decr
client.Set(ctx, "counter", "0", 0)
client.Incr(ctx, "counter")
client.Incr(ctx, "counter")
client.Incr(ctx, "counter")
result := client.Get(ctx, "counter")
fmt.Println(result.Result())
client.IncrBy(ctx, "counter", 3)
client.IncrBy(ctx, "counter", 3)
fmt.Println(client.Get(ctx, "counter").Result())
client.Decr(ctx, "counter")
client.Decr(ctx, "counter")
fmt.Println(client.Get(ctx, "counter").Result())
client.DecrBy(ctx, "counter", 3)
client.DecrBy(ctx, "counter", 3)
fmt.Println(client.Get(ctx, "counter").Result())
}
// 设置获取
func setGet() {
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()
// 设置
//status := client.Set(ctx, "name", "", 3*time.Second)
// 有效期
// 0 表示永不过期
//status := client.Set(ctx, "name", "MaShiBing", 0)
// KeepTTL -1,表示保持原有的有效期
// 仅仅需要修改值,而不去更新有效期的话
//status := client.Set(ctx, "name", "MaShiBing", redis.KeepTTL)
// 设置条件
// NX, key 不存在时设置
// XX, key 存在时设置
status := client.SetArgs(ctx, "name", "mashibing", redis.SetArgs{
// 不存在才设置
//Mode: "NX",
// 存在时才设置
Mode: "XX",
// Mode "" 表示存在则更新,不存在则添加,是默认的模式
// 有效期,时间周期
TTL: 0,
// 有效期,时间点
ExpireAt: time.Time{},
// 是否返回原有值
Get: false,
// 是否保持原有有效期
KeepTTL: false,
})
fmt.Println(status.Result())
// 获取
// 执行命令
//result := client.Get(ctx, "name")
//// 获取结果,以及命令的执行结果(是否有错误)
//val, err := result.Result()
//
//// 判定 key 是否存在(错误)
//if err == redis.Nil {
// fmt.Println("key not exists")
//} else if err != nil {
// fmt.Println(err)
//} else if val == "" {
// fmt.Println("value is empty")
//} else {
// fmt.Println("get value:", val)
//}
}

@ -0,0 +1 @@
package main

@ -0,0 +1,12 @@
services:
db:
container_name: simpleCmsMysql
image: mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: simpleCms
ports:
- "3316:3306"
volumes:
- ./data/db:/var/lib/mysql

@ -0,0 +1,9 @@
module mashibing/redisCli
go 1.19
require (
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/go-redis/redis/v9 v9.0.0-rc.1 // indirect
)

@ -0,0 +1,6 @@
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/go-redis/redis/v9 v9.0.0-rc.1 h1:/+bS+yeUnanqAbuD3QwlejzQZ+4eqgfUtFTG4b+QnXs=
github.com/go-redis/redis/v9 v9.0.0-rc.1/go.mod h1:8et+z03j0l8N+DvsVnclzjf3Dl/pFHgRk+2Ct1qw66A=

@ -0,0 +1,17 @@
package main
import "github.com/go-redis/redis/v9"
var citiesDemo = []*redis.GeoLocation{
{Name: "chansha", Longitude: 113.00000, Latitude: 28.21667},
{Name: "beijing", Longitude: 116.41667, Latitude: 39.91667},
{Name: "shanghai", Longitude: 121.43333, Latitude: 34.50000},
{Name: "guangzhou", Longitude: 113.23333, Latitude: 23.16667},
{Name: "shenzhen", Longitude: 114.06667, Latitude: 22.61667},
{Name: "tianjin", Longitude: 117.20000, Latitude: 39.13333},
{Name: "hangzhou", Longitude: 120.20000, Latitude: 30.26667},
{Name: "chengdu", Longitude: 104.06667, Latitude: 30.66667},
{Name: "xian", Longitude: 108.95000, Latitude: 34.26667},
{Name: "changchun", Longitude: 125.35000, Latitude: 43.88333},
{Name: "wulumuqi", Longitude: 87.68333, Latitude: 43.76667},
}

@ -0,0 +1,13 @@
package main
import "github.com/go-redis/redis/v9"
func main() {
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)
//client.BitCount
}

81
t.go

@ -0,0 +1,81 @@
package main
import (
"fmt"
)
func main() {
n := 4
fmt.Printf("%f\n", 1+float64(n)/10)
fmt.Printf("%f\n", 1+float64(n)/10*0.9)
fmt.Printf("%f\n", 1-float64(n)/10)
//// 获取区块Hash的后5位
//lastFive := "4aef2"
//
//// 获取庄闲的值
//bankStr, playStr := lastFive[0:2], lastFive[3:]
//// 计算庄闲
//bank, play := 0, 0
//for _, i := range []int{0, 1} {
// switch bankStr[i] {
// case 'a', 'b', 'c', 'd', 'e', 'f':
// bank += 0
// case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
// v, _ := strconv.Atoi(string(bankStr[i]))
// bank += v
// }
//
// switch playStr[i] {
// case 'a', 'b', 'c', 'd', 'e', 'f':
// play += 0
// case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
// v, _ := strconv.Atoi(string(playStr[i]))
// play += v
// }
//}
//// 余数
//bank, play = bank%10, play%10
//fmt.Printf("TRX======猎物庄闲竞猜区块哈希:%s。庄%d%d\n", lastFive, bank, play)
// 获取区块Hash的后5位
//// 获取庄闲的值
//lastFive := "4aef2"
//bankStr, playStr := lastFive[0:3], lastFive[2:]
//// 计算庄闲
//bank, play := 0, 0
//for _, i := range []int{0, 1, 2} {
// switch bankStr[i] {
// case 'a', 'b', 'c', 'd', 'e', 'f':
// bank += 10
// case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
// v, _ := strconv.Atoi(string(bankStr[i]))
// bank += v
// }
//
// switch playStr[i] {
// case 'a', 'b', 'c', 'd', 'e', 'f':
// play += 10
// case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
// v, _ := strconv.Atoi(string(playStr[i]))
// play += v
// }
//}
//// 余数
//bank, play = bank%10, play%10
//if bank == 0 {
// bank = 10
//}
//if play == 10 {
// play = 10
//}
//fmt.Printf("TRX======猎物牛牛竞猜区块哈希:%s。庄%d%d\n", lastFive, bank, play)
////// 结果
//if bank > play {
// fmt.Println(1, bank) // 庄赢
//} else if bank < play {
// fmt.Println(2, bank) // 闲赢
//} else {
// fmt.Println(3, bank) // 和
//}
}
Loading…
Cancel
Save