From b94628369619d15c9bf2f5c2e62ff30d5303df79 Mon Sep 17 00:00:00 2001 From: yuzuki999 Date: Wed, 2 Nov 2022 14:54:24 +0800 Subject: [PATCH] optimization redis recorder --- api/iprecoder/redis.go | 12 +++++------- api/iprecoder/redis_test.go | 2 +- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/api/iprecoder/redis.go b/api/iprecoder/redis.go index 04b25a5..ded63c6 100644 --- a/api/iprecoder/redis.go +++ b/api/iprecoder/redis.go @@ -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") diff --git a/api/iprecoder/redis_test.go b/api/iprecoder/redis_test.go index 12c173c..5e08f8e 100644 --- a/api/iprecoder/redis_test.go +++ b/api/iprecoder/redis_test.go @@ -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)