Files
timerx/cmd/main.go
T

135 lines
2.8 KiB
Go
Raw Normal View History

2023-08-27 23:39:58 +08:00
package main
import (
"context"
"fmt"
"time"
"code.yun.ink/open/timer"
"github.com/go-redis/redis/v8"
)
func main() {
// m := make(map[string]time.Time)
// m["sss"] = time.Now()
// b, _ := json.Marshal(m)
// fmt.Println(string(b))
// mm := make(map[string]time.Time)
// json.Unmarshal(b, &mm)
// fmt.Println(mm)
re()
2023-09-02 13:32:04 +08:00
// d()
2023-08-27 23:39:58 +08:00
}
func re() {
client := redis.NewClient(&redis.Options{
Addr: "127.0.0.1" + ":" + "6379",
Password: "", // no password set
DB: 0, // use default DB
})
if client == nil {
fmt.Println("redis init error")
return
}
ctx := context.Background()
cl := timer.InitCluster(ctx, client)
2023-09-02 13:32:04 +08:00
cl.AddTimer(ctx, "test1", 1*time.Millisecond, aa, timer.ExtendParams{
2023-08-27 23:39:58 +08:00
Params: map[string]interface{}{
"test": "text1",
},
})
2023-09-02 13:32:04 +08:00
cl.AddTimer(ctx, "test2", 1*time.Millisecond, aa, timer.ExtendParams{
Params: map[string]interface{}{
"test": "text2",
},
})
cl.AddTimer(ctx, "test3", 1*time.Millisecond, aa, timer.ExtendParams{
Params: map[string]interface{}{
"test": "text3",
},
})
cl.AddTimer(ctx, "test4", 1*time.Millisecond, aa, timer.ExtendParams{
Params: map[string]interface{}{
"test": "text4",
},
})
cl.AddTimer(ctx, "test5", 1*time.Millisecond, aa, timer.ExtendParams{
Params: map[string]interface{}{
"test": "text5",
},
})
cl.AddTimer(ctx, "test6", 1*time.Millisecond, aa, timer.ExtendParams{
Params: map[string]interface{}{
"test": "text6",
},
})
cl.AddTimer(ctx, "test7", 1*time.Millisecond, aa, timer.ExtendParams{
Params: map[string]interface{}{
"test": "text7",
},
})
cl.AddTimer(ctx, "test8", 1*time.Millisecond, aa, timer.ExtendParams{
Params: map[string]interface{}{
"test": "text8",
},
})
cl.AddTimer(ctx, "test9", 1*time.Millisecond, aa, timer.ExtendParams{
Params: map[string]interface{}{
"test": "text9",
},
})
2023-08-27 23:39:58 +08:00
select {}
}
func aa(ctx context.Context) bool {
2023-08-28 09:34:59 +08:00
// fmt.Println(time.Now().Format(time.RFC3339))
// fmt.Println("gggggggggggggggggggggggggggg")
2023-08-27 23:39:58 +08:00
a, err := timer.GetExtendParams(ctx)
2023-08-28 09:34:59 +08:00
fmt.Printf("%+v %+v \n\n", a, err)
2023-09-09 15:42:13 +08:00
time.Sleep(time.Second*5)
2023-08-27 23:39:58 +08:00
return true
}
2023-09-02 13:32:04 +08:00
func d() {
client := redis.NewClient(&redis.Options{
Addr: "127.0.0.1" + ":" + "6379",
Password: "", // no password set
DB: 0, // use default DB
})
if client == nil {
fmt.Println("redis init error")
return
}
client.ZAdd(context.Background(), "lockx:test2", &redis.Z{
Score: 50,
Member: "test",
})
script := `
local token = redis.call('zrangebyscore',KEYS[1],ARGV[1],ARGV[2])
for i,v in ipairs(token) do
redis.call('zrem',KEYS[1],v)
redis.call('lpush',KEYS[2],v)
end
return "OK"
`
res, err := client.Eval(context.Background(), script, []string{"lockx:test2", "lockx:push"}, 0, 100).Result()
fmt.Println(res, err)
for i := 0; i < 10; i++ {
l, e := client.RPop(context.Background(), "lockx:push").Result()
fmt.Println(l, e)
}
}