From e495bc44e9c0a3d01ee37c6aea28d960a83b2a61 Mon Sep 17 00:00:00 2001 From: withchao <993506633@qq.com> Date: Wed, 30 Oct 2024 15:18:31 +0800 Subject: [PATCH] feat: add ApplicationVersion --- go.mod | 2 +- go.sum | 4 ++-- internal/api/router.go | 1 - internal/rpc/third/application.go | 24 ++++--------------- pkg/common/storage/cache/application.go | 2 +- .../storage/cache/cachekey/application.go | 10 ++------ pkg/common/storage/cache/redis/application.go | 10 ++++---- pkg/common/storage/controller/application.go | 6 ++--- pkg/common/storage/database/application.go | 2 +- .../storage/database/mgo/application.go | 5 ++-- 10 files changed, 21 insertions(+), 45 deletions(-) diff --git a/go.mod b/go.mod index 57d01c38f..5e65409bc 100644 --- a/go.mod +++ b/go.mod @@ -12,7 +12,7 @@ require ( github.com/gorilla/websocket v1.5.1 github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 github.com/mitchellh/mapstructure v1.5.0 - github.com/openimsdk/protocol v0.0.72-alpha.48 + github.com/openimsdk/protocol v0.0.72-alpha.49 github.com/openimsdk/tools v0.0.50-alpha.16 github.com/pkg/errors v0.9.1 // indirect github.com/prometheus/client_golang v1.18.0 diff --git a/go.sum b/go.sum index 94553cedd..aca9ec9bc 100644 --- a/go.sum +++ b/go.sum @@ -319,8 +319,8 @@ github.com/onsi/gomega v1.25.0 h1:Vw7br2PCDYijJHSfBOWhov+8cAnUf8MfMaIOV323l6Y= github.com/onsi/gomega v1.25.0/go.mod h1:r+zV744Re+DiYCIPRlYOTxn0YkOLcAnW8k1xXdMPGhM= github.com/openimsdk/gomake v0.0.14-alpha.5 h1:VY9c5x515lTfmdhhPjMvR3BBRrRquAUCFsz7t7vbv7Y= github.com/openimsdk/gomake v0.0.14-alpha.5/go.mod h1:PndCozNc2IsQIciyn9mvEblYWZwJmAI+06z94EY+csI= -github.com/openimsdk/protocol v0.0.72-alpha.48 h1:DVeT8Kej6OjB9bsxQ0q6FU160anwfPuVmAL/1J6VzqM= -github.com/openimsdk/protocol v0.0.72-alpha.48/go.mod h1:OZQA9FR55lseYoN2Ql1XAHYKHJGu7OMNkUbuekrKCM8= +github.com/openimsdk/protocol v0.0.72-alpha.49 h1:p2uDLLoVc8ONrwPkHTuhQUdXgSj2RPsCHmkvljC6swA= +github.com/openimsdk/protocol v0.0.72-alpha.49/go.mod h1:OZQA9FR55lseYoN2Ql1XAHYKHJGu7OMNkUbuekrKCM8= github.com/openimsdk/tools v0.0.50-alpha.16 h1:bC1AQvJMuOHtZm8LZRvN8L5mH1Ws2VYdL+TLTs1iGSc= github.com/openimsdk/tools v0.0.50-alpha.16/go.mod h1:h1cYmfyaVtgFbKmb1Cfsl8XwUOMTt8ubVUQrdGtsUh4= github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM= diff --git a/internal/api/router.go b/internal/api/router.go index 17c998912..da9772e84 100644 --- a/internal/api/router.go +++ b/internal/api/router.go @@ -298,5 +298,4 @@ var Whitelist = []string{ "/auth/get_admin_token", "/auth/parse_token", "/application/latest_version", - "/application/page_versions", } diff --git a/internal/rpc/third/application.go b/internal/rpc/third/application.go index fd58e4071..c72393a19 100644 --- a/internal/rpc/third/application.go +++ b/internal/rpc/third/application.go @@ -36,33 +36,17 @@ func (t *thirdServer) db2pbApplication(val *model.Application) *third.Applicatio } } -func (t *thirdServer) getLatestApplicationVersion(ctx context.Context, platform string, hot bool) (*third.ApplicationVersion, error) { - res, err := t.applicationDatabase.LatestVersion(ctx, platform, hot) +func (t *thirdServer) LatestApplicationVersion(ctx context.Context, req *third.LatestApplicationVersionReq) (*third.LatestApplicationVersionResp, error) { + res, err := t.applicationDatabase.LatestVersion(ctx, req.Platform) if err == nil { - return t.db2pbApplication(res), nil + return &third.LatestApplicationVersionResp{Version: t.db2pbApplication(res)}, nil } else if IsNotFound(err) { - return nil, nil + return &third.LatestApplicationVersionResp{}, nil } else { return nil, err } } -func (t *thirdServer) LatestApplicationVersion(ctx context.Context, req *third.LatestApplicationVersionReq) (*third.LatestApplicationVersionResp, error) { - var ( - resp third.LatestApplicationVersionResp - err error - ) - resp.Version, err = t.getLatestApplicationVersion(ctx, req.Platform, false) - if err != nil { - return nil, err - } - resp.Hot, err = t.getLatestApplicationVersion(ctx, req.Platform, true) - if err != nil { - return nil, err - } - return &resp, nil -} - func (t *thirdServer) AddApplicationVersion(ctx context.Context, req *third.AddApplicationVersionReq) (*third.AddApplicationVersionResp, error) { if err := authverify.CheckAdmin(ctx, t.config.Share.IMAdminUserID); err != nil { return nil, err diff --git a/pkg/common/storage/cache/application.go b/pkg/common/storage/cache/application.go index bb23a3a17..588732ec8 100644 --- a/pkg/common/storage/cache/application.go +++ b/pkg/common/storage/cache/application.go @@ -6,6 +6,6 @@ import ( ) type ApplicationCache interface { - LatestVersion(ctx context.Context, platform string, hot bool) (*model.Application, error) + LatestVersion(ctx context.Context, platform string) (*model.Application, error) DeleteCache(ctx context.Context, platforms []string) error } diff --git a/pkg/common/storage/cache/cachekey/application.go b/pkg/common/storage/cache/cachekey/application.go index d7835b289..032adba3c 100644 --- a/pkg/common/storage/cache/cachekey/application.go +++ b/pkg/common/storage/cache/cachekey/application.go @@ -4,12 +4,6 @@ const ( ApplicationLatestVersion = "APPLICATION_LATEST_VERSION:" ) -func GetApplicationLatestVersionKey(platform string, hot bool) string { - var hotStr string - if hot { - hotStr = "1:" - } else { - hotStr = "0:" - } - return ApplicationLatestVersion + hotStr + platform +func GetApplicationLatestVersionKey(platform string) string { + return ApplicationLatestVersion + platform } diff --git a/pkg/common/storage/cache/redis/application.go b/pkg/common/storage/cache/redis/application.go index a858fa25b..f556bbfe9 100644 --- a/pkg/common/storage/cache/redis/application.go +++ b/pkg/common/storage/cache/redis/application.go @@ -26,9 +26,9 @@ type ApplicationRedisCache struct { expireTime time.Duration } -func (a *ApplicationRedisCache) LatestVersion(ctx context.Context, platform string, hot bool) (*model.Application, error) { - return getCache(ctx, a.rcClient, cachekey.GetApplicationLatestVersionKey(platform, hot), a.expireTime, func(ctx context.Context) (*model.Application, error) { - return a.db.LatestVersion(ctx, platform, hot) +func (a *ApplicationRedisCache) LatestVersion(ctx context.Context, platform string) (*model.Application, error) { + return getCache(ctx, a.rcClient, cachekey.GetApplicationLatestVersionKey(platform), a.expireTime, func(ctx context.Context) (*model.Application, error) { + return a.db.LatestVersion(ctx, platform) }) } @@ -36,9 +36,9 @@ func (a *ApplicationRedisCache) DeleteCache(ctx context.Context, platforms []str if len(platforms) == 0 { return nil } - keys := make([]string, 0, len(platforms)*2) + keys := make([]string, 0, len(platforms)) for _, platform := range platforms { - keys = append(keys, cachekey.GetApplicationLatestVersionKey(platform, true), cachekey.GetApplicationLatestVersionKey(platform, false)) + keys = append(keys, cachekey.GetApplicationLatestVersionKey(platform)) } return a.deleter.ExecDelWithKeys(ctx, keys) } diff --git a/pkg/common/storage/controller/application.go b/pkg/common/storage/controller/application.go index 388805772..72bca07ef 100644 --- a/pkg/common/storage/controller/application.go +++ b/pkg/common/storage/controller/application.go @@ -10,7 +10,7 @@ import ( ) type ApplicationDatabase interface { - LatestVersion(ctx context.Context, platform string, hot bool) (*model.Application, error) + LatestVersion(ctx context.Context, platform string) (*model.Application, error) AddVersion(ctx context.Context, val *model.Application) error UpdateVersion(ctx context.Context, id primitive.ObjectID, update map[string]any) error DeleteVersion(ctx context.Context, id []primitive.ObjectID) error @@ -26,8 +26,8 @@ type applicationDatabase struct { cache cache.ApplicationCache } -func (a *applicationDatabase) LatestVersion(ctx context.Context, platform string, hot bool) (*model.Application, error) { - return a.cache.LatestVersion(ctx, platform, hot) +func (a *applicationDatabase) LatestVersion(ctx context.Context, platform string) (*model.Application, error) { + return a.cache.LatestVersion(ctx, platform) } func (a *applicationDatabase) AddVersion(ctx context.Context, val *model.Application) error { diff --git a/pkg/common/storage/database/application.go b/pkg/common/storage/database/application.go index 50796be62..c98ae74c8 100644 --- a/pkg/common/storage/database/application.go +++ b/pkg/common/storage/database/application.go @@ -8,7 +8,7 @@ import ( ) type Application interface { - LatestVersion(ctx context.Context, platform string, hot bool) (*model.Application, error) + LatestVersion(ctx context.Context, platform string) (*model.Application, error) AddVersion(ctx context.Context, val *model.Application) error UpdateVersion(ctx context.Context, id primitive.ObjectID, update map[string]any) error DeleteVersion(ctx context.Context, id []primitive.ObjectID) error diff --git a/pkg/common/storage/database/mgo/application.go b/pkg/common/storage/database/mgo/application.go index e28965247..e59c0560a 100644 --- a/pkg/common/storage/database/mgo/application.go +++ b/pkg/common/storage/database/mgo/application.go @@ -18,7 +18,6 @@ func NewApplicationMgo(db *mongo.Database) (*ApplicationMgo, error) { Keys: bson.D{ {Key: "platform", Value: 1}, {Key: "version", Value: 1}, - {Key: "hot", Value: 1}, }, Options: options.Index().SetUnique(true), }, @@ -42,8 +41,8 @@ func (a *ApplicationMgo) sort() any { return bson.D{{"latest", -1}, {"_id", -1}} } -func (a *ApplicationMgo) LatestVersion(ctx context.Context, platform string, hot bool) (*model.Application, error) { - return mongoutil.FindOne[*model.Application](ctx, a.coll, bson.M{"platform": platform, "hot": hot}, options.FindOne().SetSort(a.sort())) +func (a *ApplicationMgo) LatestVersion(ctx context.Context, platform string) (*model.Application, error) { + return mongoutil.FindOne[*model.Application](ctx, a.coll, bson.M{"platform": platform}, options.FindOne().SetSort(a.sort())) } func (a *ApplicationMgo) AddVersion(ctx context.Context, val *model.Application) error {