optimization redis recorder

This commit is contained in:
yuzuki999 2022-11-02 14:54:24 +08:00
parent e76ec30c92
commit b946283696
2 changed files with 6 additions and 8 deletions

View File

@ -29,19 +29,17 @@ func NewRedis(c *conf.RedisConfig) *Redis {
func (r *Redis) SyncOnlineIp(Ips []dispatcher.UserIpList) ([]dispatcher.UserIpList, error) {
ctx := context.Background()
for i := range Ips {
if !r.client.SIsMember(ctx, "UserList", strconv.Itoa(Ips[i].Uid)).Val() {
err := r.client.SAdd(ctx, "UserList", Ips[i].Uid).Err()
if err != nil {
return nil, fmt.Errorf("add user failed: %s", err)
}
err := r.client.SAdd(ctx, "UserList", Ips[i].Uid).Err()
if err != nil {
return nil, fmt.Errorf("add user failed: %s", err)
}
r.client.Expire(ctx, "UserList", time.Duration(2)*time.Minute)
r.client.Expire(ctx, "UserList", 2*time.Minute)
for _, ip := range Ips[i].IpList {
err := r.client.SAdd(ctx, strconv.Itoa(Ips[i].Uid), ip).Err()
if err != nil {
return nil, fmt.Errorf("add ip failed: %s", err)
}
r.client.Expire(ctx, strconv.Itoa(Ips[i].Uid), time.Duration(2)*time.Minute)
r.client.Expire(ctx, strconv.Itoa(Ips[i].Uid), 2*time.Minute)
}
}
c := r.client.SMembers(ctx, "UserList")

View File

@ -14,7 +14,7 @@ func TestRedis_SyncOnlineIp(t *testing.T) {
Db: 0,
})
users, err := r.SyncOnlineIp([]dispatcher.UserIpList{
{2, []string{"3.3.3.3", "4.4.4.4"}},
{1, []string{"5.5.5.5", "4.4.4.4"}},
})
if err != nil {
t.Fatal(err)