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.
36 lines
861 B
36 lines
861 B
3 years ago
|
package storage
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"github.com/go-redis/redis/v8"
|
||
|
"gorm.io/gorm"
|
||
|
"strconv"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
|
var DB *gorm.DB
|
||
|
var Redis *redis.Client
|
||
|
var Registration map[int64][]byte
|
||
|
|
||
|
func SetState(ctx context.Context, id int64, state string, metadata ...string) error {
|
||
|
for _, current := range metadata {
|
||
|
state += "|" + current
|
||
|
}
|
||
|
return Redis.Set(ctx, strconv.FormatInt(id, 10), state, 0).Err()
|
||
|
}
|
||
|
|
||
|
func GetState(ctx context.Context, id int64) (string, []string, error) {
|
||
|
cursor := Redis.Get(ctx, strconv.FormatInt(id, 10))
|
||
|
if cursor.Err() == redis.Nil {
|
||
|
return "", make([]string, 0), nil
|
||
|
} else if cursor.Err() != nil {
|
||
|
return "", nil, cursor.Err()
|
||
|
}
|
||
|
result := strings.Split(cursor.Val(), "|")
|
||
|
return result[0], result[1:], nil
|
||
|
}
|
||
|
|
||
|
func ClearState(ctx context.Context, id int64) error {
|
||
|
return Redis.Del(ctx, strconv.FormatInt(id, 10)).Err()
|
||
|
}
|