You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
524 lines
14 KiB
524 lines
14 KiB
package redis
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"github.com/redis/go-redis/v9"
|
|
red "github.com/redis/go-redis/v9"
|
|
"matchmaking-system/internal/conf"
|
|
"matchmaking-system/internal/pkg/flags"
|
|
"matchmaking-system/internal/pkg/logging/applogger"
|
|
models "matchmaking-system/internal/pkg/model"
|
|
"reflect"
|
|
"strconv"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
type SetPrice struct {
|
|
PreStatus string
|
|
PreStartTime string
|
|
PreLimit string
|
|
PrePrices string
|
|
AfterStatus string
|
|
AfterStartTime string
|
|
AfterLimit string
|
|
AfterPrices string
|
|
}
|
|
|
|
func TestRedisHash(t *testing.T) {
|
|
client := redis.NewClient(&redis.Options{
|
|
Addr: "r-gs523wj7gcnshp3d1fpd.redis.singapore.rds.aliyuncs.com:6379", // Redis 服务器地址和端口号
|
|
DB: 0, // Redis 数据库编号,默认为 0
|
|
Password: "MRrfvtyujnb&hg56", // Redis 数据库密码
|
|
})
|
|
pong, err := client.Ping(context.Background()).Result()
|
|
if err != nil {
|
|
fmt.Println(pong, err)
|
|
return
|
|
}
|
|
|
|
symbol, err := client.HGetAll(context.Background(), fmt.Sprintf("%v%v", flags.StockMarketList, 14)).Result()
|
|
if err != nil {
|
|
applogger.Error("GetCacheKeepDecimal.HGetAll:%v", err)
|
|
return
|
|
}
|
|
|
|
// TODO: 开闭盘价试调
|
|
var amOpenTime, pmOpenTime, amCloseTime, pmCloseTime time.Time
|
|
for filed, value := range symbol {
|
|
switch filed {
|
|
case "am_open_time":
|
|
amOpenTime, _ = time.Parse(flags.LayoutOne, value)
|
|
case "am_close_time":
|
|
amCloseTime, _ = time.Parse(flags.LayoutOne, value)
|
|
case "pm_open_time":
|
|
pmOpenTime, _ = time.Parse(flags.LayoutOne, value)
|
|
case "pm_close_time":
|
|
pmCloseTime, _ = time.Parse(flags.LayoutOne, value)
|
|
default:
|
|
}
|
|
}
|
|
|
|
dateTime, _ := time.Parse(flags.LayoutOne, time.Now().Format(flags.LayoutOne))
|
|
fmt.Println("当前时间:", dateTime)
|
|
fmt.Println("上午开盘时间:", amOpenTime)
|
|
fmt.Println("上午闭盘时间:", amCloseTime)
|
|
fmt.Println("下午开盘时间:", pmOpenTime)
|
|
fmt.Println("下午闭盘时间:", pmCloseTime)
|
|
|
|
// 判定上午盘
|
|
checkAmTime := dateTime.Equal(amOpenTime) || dateTime.Equal(amCloseTime)
|
|
if amOpenTime.Before(amCloseTime) {
|
|
fmt.Println("上午同天盘判定。。。。。")
|
|
// 同一天
|
|
if dateTime.After(amOpenTime) && dateTime.Before(amCloseTime) || checkAmTime {
|
|
fmt.Println("上午正常的开盘时间。。。。。")
|
|
}
|
|
} else {
|
|
fmt.Println("上午跨天盘判定。。。。。")
|
|
// 跨天
|
|
if dateTime.After(amOpenTime) || dateTime.Before(amCloseTime) || checkAmTime {
|
|
fmt.Println("上午正常的开盘时间。。。。。")
|
|
}
|
|
}
|
|
// 判定下午盘
|
|
checkPmTime := dateTime.Equal(pmOpenTime) || dateTime.Equal(pmCloseTime)
|
|
if pmOpenTime.Before(pmCloseTime) {
|
|
fmt.Println("下午同天盘判定。。。。。")
|
|
// 同一天
|
|
if dateTime.After(pmOpenTime) && dateTime.Before(pmCloseTime) || checkPmTime {
|
|
fmt.Println("下午正常的开盘时间。。。。。")
|
|
}
|
|
} else {
|
|
fmt.Println("下午跨天盘判定。。。。。")
|
|
// 跨天
|
|
if dateTime.After(pmOpenTime) || dateTime.Before(pmCloseTime) || checkPmTime {
|
|
fmt.Println("下午正常的开盘时间。。。。。")
|
|
}
|
|
}
|
|
|
|
// TODO: 杠杆试调
|
|
//pryNum, err := client.Get(context.Background(), fmt.Sprintf("%v%v", flags.StockTradePryNumSet, 3)).Result()
|
|
//if err != nil {
|
|
// pryNum, err = client.Get(context.Background(), fmt.Sprintf("%v%v", flags.StockTradePryNumSet, 0)).Result()
|
|
// if err != nil {
|
|
// pryNum = flags.SetOne
|
|
// }
|
|
//}
|
|
//pryNumValue := decimal.RequireFromString(pryNum)
|
|
//fmt.Println("杠杆数据:", pryNumValue)
|
|
return
|
|
|
|
keySymbol := fmt.Sprintf("STOCK_PRICES:3:AAPL")
|
|
symbolMap, err := client.HGetAll(context.Background(), keySymbol).Result()
|
|
if err != nil {
|
|
fmt.Println("数据信息:", err)
|
|
return
|
|
}
|
|
setModel := &SetPrice{}
|
|
|
|
for key, value := range symbolMap {
|
|
applogger.Warn("数据展示:%v---%v", key, value)
|
|
switch key {
|
|
case "pre_status": // 盘前标识
|
|
setModel.PreStatus = value
|
|
case "pre_start_time":
|
|
setModel.PreStartTime = value
|
|
case "pre_limit":
|
|
setModel.PreLimit = value
|
|
case "pre_prices":
|
|
setModel.PrePrices = value
|
|
case "after_status": // 盘后标识
|
|
setModel.AfterStatus = value
|
|
case "after_start_time":
|
|
setModel.AfterStartTime = value
|
|
case "after_limit":
|
|
setModel.AfterLimit = value
|
|
case "after_prices":
|
|
setModel.AfterPrices = value
|
|
}
|
|
}
|
|
|
|
// 设置哈希字段值
|
|
err = client.HSet(context.Background(), "myhash", "field1", "value1").Err()
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
err = client.HSet(context.Background(), "myhash", "field2", "value2").Err()
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
|
|
// 获取哈希字段值
|
|
value, err := client.HGet(context.Background(), "myhash", "field1").Result()
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
fmt.Println(value)
|
|
|
|
// 获取所有哈希字段和值
|
|
hashList, err := client.HGetAll(context.Background(), "myhash").Result()
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
for field, value := range hashList {
|
|
fmt.Printf("Field: %s, Value: %s\n", field, value)
|
|
}
|
|
|
|
// 删除哈希字段
|
|
err = client.HDel(context.Background(), "myhash", "field1").Err()
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
}
|
|
|
|
func TestRedisFeeSetting(t *testing.T) {
|
|
client := redis.NewClient(&redis.Options{
|
|
Addr: "18.142.112.77:6378", // Redis 服务器地址和端口号
|
|
DB: 0, // Redis 数据库编号,默认为 0
|
|
Password: "rfvtyujnbhg56", // Redis 数据库密码
|
|
})
|
|
// 获取股票手续费设置
|
|
allStock, err := client.HGetAll(context.Background(), "TRADE:FEE:STOCK").Result()
|
|
if err != nil {
|
|
fmt.Println("股票手续费:", err)
|
|
return
|
|
}
|
|
for key, value := range allStock {
|
|
fmt.Println(key, ":", value)
|
|
}
|
|
|
|
// 获取现货手续费设置
|
|
allSpots, err := client.HGetAll(context.Background(), "TRADE:FEE:DIGITAL").Result()
|
|
if err != nil {
|
|
fmt.Println("现货手续费:", err)
|
|
return
|
|
}
|
|
for key, value := range allSpots {
|
|
fmt.Println(key, ":", value)
|
|
}
|
|
|
|
// 获取合约手续费设置
|
|
allContract, err := client.HGetAll(context.Background(), "TRADE:FEE:CONTRACT").Result()
|
|
if err != nil {
|
|
fmt.Println("合约手续费:", err)
|
|
return
|
|
}
|
|
for key, value := range allContract {
|
|
fmt.Println(key, ":", value)
|
|
}
|
|
}
|
|
|
|
func TestRedisUserLever(t *testing.T) {
|
|
client := redis.NewClient(&redis.Options{
|
|
Addr: "18.142.112.77:6378", // Redis 服务器地址和端口号
|
|
DB: 0, // Redis 数据库编号,默认为 0
|
|
Password: "rfvtyujnbhg56", // Redis 数据库密码
|
|
})
|
|
|
|
//keyData := fmt.Sprintf("USER:LEVEL:%v", 9)
|
|
//allStock, err := client.HGetAll(context.Background(), keyData).Result()
|
|
//if err != nil {
|
|
// fmt.Println("获取用户返佣层级错误:", err)
|
|
// return
|
|
//}
|
|
//for key, value := range allStock {
|
|
// valueId, err := strconv.Atoi(value)
|
|
// if err != nil {
|
|
// return
|
|
// }
|
|
// fmt.Println(key, ":", valueId)
|
|
//}
|
|
|
|
keyData := fmt.Sprintf("%v%v", flags.UserLevel, 100)
|
|
allStock, err := client.HGetAll(context.Background(), keyData).Result()
|
|
if err != nil {
|
|
fmt.Println("获取用户返佣层级错误:", err)
|
|
return
|
|
}
|
|
fmt.Println("数据展示:", keyData)
|
|
var level models.BotUserLevel
|
|
for key, value := range allStock {
|
|
valueId, err := strconv.Atoi(value)
|
|
if err != nil {
|
|
fmt.Println("转换错误:", err)
|
|
return
|
|
}
|
|
fmt.Println(key, ":", valueId)
|
|
//continue
|
|
switch key {
|
|
case "user_id":
|
|
level.UserId = valueId
|
|
case "parent_id":
|
|
level.ParentId = valueId
|
|
case "grandpa_id":
|
|
level.GrandpaId = valueId
|
|
case "top_id":
|
|
level.TopId = valueId
|
|
}
|
|
}
|
|
if level.UserId != 0 {
|
|
fmt.Println("查询用户返佣层级关系:", &level)
|
|
}
|
|
|
|
}
|
|
|
|
func TestRedisZset(t *testing.T) {
|
|
// 创建 Redis 客户端
|
|
client := redis.NewClient(&redis.Options{
|
|
Addr: "13.251.125.27:6378", // Redis 服务器地址和端口号
|
|
DB: 0, // Redis 数据库编号,默认为 0
|
|
Password: "rfvtyujnbhg56", // Redis 数据库密码
|
|
})
|
|
|
|
// 向有序集合中添加成员和分数
|
|
err := client.ZAdd(context.Background(), "myzset",
|
|
redis.Z{Score: float64(1.0), Member: "Alice"},
|
|
redis.Z{Score: float64(2.0), Member: "Bob"},
|
|
redis.Z{Score: float64(3.0), Member: "Charlie"}).Err()
|
|
if err != nil {
|
|
fmt.Println("无法向有序集合添加成员和分数:", err)
|
|
return
|
|
}
|
|
|
|
// 获取有序集合的成员总数
|
|
count, err := client.ZCard(context.Background(), "myzset").Result()
|
|
if err != nil {
|
|
fmt.Println("无法获取有序集合的成员总数:", err)
|
|
return
|
|
}
|
|
fmt.Println("有序集合的成员总数:", count)
|
|
|
|
// 根据分数范围获取有序集合中的成员
|
|
members, err := client.ZRangeByScore(context.Background(), "myzset", &redis.ZRangeBy{
|
|
Min: "-inf",
|
|
Max: "+inf",
|
|
}).Result()
|
|
if err != nil {
|
|
fmt.Println("无法根据分数范围获取有序集合中的成员:", err)
|
|
return
|
|
}
|
|
fmt.Println("有序集合中的成员:", members)
|
|
|
|
// 修改有序集合中的成员分数
|
|
err = client.ZIncrBy(context.Background(), "myzset", float64(1.0), "Alice").Err()
|
|
if err != nil {
|
|
fmt.Println("无法修改有序集合中的成员分数:", err)
|
|
return
|
|
}
|
|
|
|
// 删除有序集合中的一个或多个元素
|
|
deletedCount, err := client.ZRem(context.Background(), "myzset", "Alice").Result()
|
|
if err != nil {
|
|
fmt.Println("无法删除有序集合中的元素:", err)
|
|
return
|
|
}
|
|
fmt.Println("已删除的元素数量:", deletedCount)
|
|
|
|
// 获取成员的分数
|
|
score, err := client.ZScore(context.Background(), "myzset", "Alice").Result()
|
|
if err != nil {
|
|
fmt.Println("无法获取有序集合中成员的分数:", err)
|
|
return
|
|
}
|
|
fmt.Println("有序集合中成员 Alice 的分数:", score)
|
|
}
|
|
|
|
func TestRedisList(t *testing.T) {
|
|
// 创建一个 Redis 客户端
|
|
client := redis.NewClient(&redis.Options{
|
|
Addr: "13.251.125.27:6378", // Redis 服务器地址和端口号
|
|
DB: 0, // Redis 数据库编号,默认为 0
|
|
Password: "rfvtyujnbhg56", // Redis 数据库密码
|
|
})
|
|
|
|
// 添加元素到列表的尾部
|
|
err := client.RPush(context.Background(), "mylist", "a", "b", "c").Err()
|
|
if err != nil {
|
|
fmt.Println("添加元素失败:", err)
|
|
return
|
|
}
|
|
|
|
// 获取列表的长度
|
|
length, err := client.LLen(context.Background(), "mylist").Result()
|
|
if err != nil {
|
|
fmt.Println("获取列表长度失败:", err)
|
|
return
|
|
}
|
|
fmt.Println("列表长度:", length)
|
|
|
|
// 获取列表中的所有元素
|
|
elements, err := client.LRange(context.Background(), "mylist", 0, -1).Result()
|
|
if err != nil {
|
|
fmt.Println("获取列表中的元素失败:", err)
|
|
return
|
|
}
|
|
fmt.Println("列表中的元素:", elements)
|
|
|
|
// 删除指定元素
|
|
removedCount, err := client.LRem(context.Background(), "mylist", 1, "b").Result()
|
|
if err != nil {
|
|
fmt.Println("删除元素失败:", err)
|
|
return
|
|
}
|
|
fmt.Printf("成功删除了 %d 个元素\n", removedCount)
|
|
|
|
// 从列表头部弹出一个元素
|
|
element, err := client.LPop(context.Background(), "mylist").Result()
|
|
if err != nil {
|
|
fmt.Println("弹出元素失败:", err)
|
|
return
|
|
}
|
|
fmt.Println("弹出的元素:", element)
|
|
}
|
|
|
|
func TestGetCacheCount(t *testing.T) {
|
|
type args struct {
|
|
ctx context.Context
|
|
redisDB *red.Client
|
|
key string
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
want int64
|
|
wantErr bool
|
|
}{
|
|
// TODO: Add test cases.
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := GetCacheCount(tt.args.ctx, tt.args.redisDB, tt.args.key)
|
|
if (err != nil) != tt.wantErr {
|
|
t.Errorf("GetCacheCount() error = %v, wantErr %v", err, tt.wantErr)
|
|
return
|
|
}
|
|
if got != tt.want {
|
|
t.Errorf("GetCacheCount() got = %v, want %v", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestGetCacheData(t *testing.T) {
|
|
type args struct {
|
|
ctx context.Context
|
|
redisDB *red.Client
|
|
key string
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
want string
|
|
wantErr bool
|
|
}{
|
|
// TODO: Add test cases.
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := GetCacheData(tt.args.ctx, tt.args.redisDB, tt.args.key)
|
|
if (err != nil) != tt.wantErr {
|
|
t.Errorf("GetCacheData() error = %v, wantErr %v", err, tt.wantErr)
|
|
return
|
|
}
|
|
if got != tt.want {
|
|
t.Errorf("GetCacheData() got = %v, want %v", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestGetCacheKeys(t *testing.T) {
|
|
type args struct {
|
|
ctx context.Context
|
|
redisDB *red.Client
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
want []string
|
|
wantErr bool
|
|
}{
|
|
// TODO: Add test cases.
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := GetCacheKeys(tt.args.ctx, tt.args.redisDB)
|
|
if (err != nil) != tt.wantErr {
|
|
t.Errorf("GetCacheKeys() error = %v, wantErr %v", err, tt.wantErr)
|
|
return
|
|
}
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
t.Errorf("GetCacheKeys() got = %v, want %v", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestNewRedis(t *testing.T) {
|
|
type args struct {
|
|
c *conf.Data
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
want *red.Client
|
|
}{
|
|
// TODO: Add test cases.
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if got := NewRedis(tt.args.c); !reflect.DeepEqual(got, tt.want) {
|
|
t.Errorf("NewRedis() = %v, want %v", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestSetCacheData(t *testing.T) {
|
|
type args struct {
|
|
ctx context.Context
|
|
redisDB *red.Client
|
|
key string
|
|
value interface{}
|
|
td int
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
wantErr bool
|
|
}{
|
|
// TODO: Add test cases.
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if err := SetCacheData(tt.args.ctx, tt.args.redisDB, tt.args.key, tt.args.value, tt.args.td); (err != nil) != tt.wantErr {
|
|
t.Errorf("SetCacheData() error = %v, wantErr %v", err, tt.wantErr)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestSetCacheValue(t *testing.T) {
|
|
type args struct {
|
|
ctx context.Context
|
|
redisDB *red.Client
|
|
key string
|
|
value string
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
wantErr bool
|
|
}{
|
|
// TODO: Add test cases.
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if err := SetCacheValue(tt.args.ctx, tt.args.redisDB, tt.args.key, tt.args.value); (err != nil) != tt.wantErr {
|
|
t.Errorf("SetCacheValue() error = %v, wantErr %v", err, tt.wantErr)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|