Skip to content

Commit f6d0b3b

Browse files
committed
Add cache for model and dataset info API
1 parent 0115000 commit f6d0b3b

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

api/middleware/cache.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,7 @@ func getCacheStrategyTrendingReposByRequest(c *gin.Context) (bool, cache.Strateg
3333
CacheDuration: 2 * time.Minute,
3434
}
3535
}
36+
37+
func CacheRepoInfo() cache.Option {
38+
return cache.WithCacheStrategyByRequest(getCacheStrategyRepoInfoByRequest)
39+
}

api/middleware/cache_ce.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//go:build !saas
2+
3+
package middleware
4+
5+
import (
6+
cache "github.com/chenyahui/gin-cache"
7+
"github.com/gin-gonic/gin"
8+
)
9+
10+
func getCacheStrategyRepoInfoByRequest(c *gin.Context) (bool, cache.Strategy) {
11+
return false, cache.Strategy{}
12+
}

api/router/api.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ func createModelRoutes(config *config.Config,
597597
modelsGroup.GET("", cache.Cache(memoryStore, time.Minute, middleware.CacheStrategyTrendingRepos()), modelHandler.Index)
598598
modelsGroup.PUT("/:namespace/:name", middlewareCollection.Auth.NeedLogin, modelHandler.Update)
599599
modelsGroup.DELETE("/:namespace/:name", middlewareCollection.Auth.NeedLogin, modelHandler.Delete)
600-
modelsGroup.GET("/:namespace/:name", modelHandler.Show)
600+
modelsGroup.GET("/:namespace/:name", cache.Cache(memoryStore, time.Minute*2, middleware.CacheRepoInfo()), modelHandler.Show)
601601
modelsGroup.GET("/:namespace/:name/relations", modelHandler.Relations)
602602
modelsGroup.PUT("/:namespace/:name/relations", middlewareCollection.Auth.NeedAdmin, modelHandler.SetRelations)
603603
modelsGroup.POST("/:namespace/:name/relations/dataset", middlewareCollection.Auth.NeedPhoneVerified, modelHandler.AddDatasetRelation)
@@ -611,7 +611,7 @@ func createModelRoutes(config *config.Config,
611611
modelsGroup.GET("/:namespace/:name/tags", repoCommonHandler.Tags)
612612
modelsGroup.POST("/:namespace/:name/preupload/:revision", middlewareCollection.Auth.NeedPhoneVerified, repoCommonHandler.Preupload)
613613
// update tags of a certain category
614-
modelsGroup.GET("/:namespace/:name/all_files", repoCommonHandler.AllFiles)
614+
modelsGroup.GET("/:namespace/:name/all_files", cache.Cache(memoryStore, time.Minute*2, middleware.CacheRepoInfo()), repoCommonHandler.AllFiles)
615615
modelsGroup.POST("/:namespace/:name/tags/:category", middlewareCollection.Auth.NeedPhoneVerified, repoCommonHandler.UpdateTags)
616616
modelsGroup.GET("/:namespace/:name/last_commit", repoCommonHandler.LastCommit)
617617
modelsGroup.GET("/:namespace/:name/commit/:commit_id", repoCommonHandler.CommitWithDiff)
@@ -623,7 +623,7 @@ func createModelRoutes(config *config.Config,
623623
modelsGroup.GET("/:namespace/:name/refs/:ref/remote_tree/*path", repoCommonHandler.RemoteTree)
624624
modelsGroup.GET("/:namespace/:name/refs/:ref/logs_tree/*path", repoCommonHandler.LogsTree)
625625
modelsGroup.GET("/:namespace/:name/commits", repoCommonHandler.Commits)
626-
modelsGroup.GET("/:namespace/:name/raw/*file_path", repoCommonHandler.FileRaw)
626+
modelsGroup.GET("/:namespace/:name/raw/*file_path", cache.Cache(memoryStore, time.Minute*2, middleware.CacheRepoInfo()), repoCommonHandler.FileRaw)
627627
modelsGroup.GET("/:namespace/:name/blob/*file_path", repoCommonHandler.FileInfo)
628628
// The DownloadFile method differs from the SDKDownload interface in a few ways
629629

@@ -735,6 +735,9 @@ func createDatasetRoutes(
735735
dsHandler *handler.DatasetHandler,
736736
repoCommonHandler *handler.RepoHandler,
737737
) {
738+
// gin cache
739+
memoryStore := persist.NewMemoryStore(2 * time.Minute)
740+
738741
datasetsGroup := apiGroup.Group("/datasets")
739742
// allow access without login
740743
datasetsGroup.GET("", dsHandler.Index)
@@ -744,13 +747,13 @@ func createDatasetRoutes(
744747
datasetsGroup.POST("", middlewareCollection.Auth.NeedPhoneVerified, dsHandler.Create)
745748
datasetsGroup.PUT("/:namespace/:name", middleware.MustLogin(), dsHandler.Update)
746749
datasetsGroup.DELETE("/:namespace/:name", middleware.MustLogin(), dsHandler.Delete)
747-
datasetsGroup.GET("/:namespace/:name", dsHandler.Show)
750+
datasetsGroup.GET("/:namespace/:name", cache.Cache(memoryStore, time.Minute*2, middleware.CacheRepoInfo()), dsHandler.Show)
748751
datasetsGroup.GET("/:namespace/:name/relations", middleware.MustLogin(), dsHandler.Relations)
749752
datasetsGroup.GET("/:namespace/:name/branches", middleware.MustLogin(), repoCommonHandler.Branches)
750753
datasetsGroup.GET("/:namespace/:name/tags", middleware.MustLogin(), repoCommonHandler.Tags)
751754
datasetsGroup.POST("/:namespace/:name/preupload/:revision", middlewareCollection.Auth.NeedPhoneVerified, repoCommonHandler.Preupload)
752755
// update tags of a certain category
753-
datasetsGroup.GET("/:namespace/:name/all_files", middleware.MustLogin(), repoCommonHandler.AllFiles)
756+
datasetsGroup.GET("/:namespace/:name/all_files", cache.Cache(memoryStore, time.Minute*2, middleware.CacheRepoInfo()), middleware.MustLogin(), repoCommonHandler.AllFiles)
754757
datasetsGroup.POST("/:namespace/:name/tags/:category", middleware.MustLogin(), repoCommonHandler.UpdateTags)
755758
datasetsGroup.GET("/:namespace/:name/last_commit", repoCommonHandler.LastCommit)
756759
datasetsGroup.GET("/:namespace/:name/commit/:commit_id", middleware.MustLogin(), repoCommonHandler.CommitWithDiff)
@@ -763,7 +766,7 @@ func createDatasetRoutes(
763766
datasetsGroup.GET("/:namespace/:name/refs/:ref/logs_tree/*path", middleware.MustLogin(), repoCommonHandler.LogsTree)
764767
datasetsGroup.GET("/:namespace/:name/commits", middleware.MustLogin(), repoCommonHandler.Commits)
765768
datasetsGroup.POST("/:namespace/:name/raw/*file_path", middlewareCollection.Auth.NeedPhoneVerified, repoCommonHandler.CreateFile)
766-
datasetsGroup.GET("/:namespace/:name/raw/*file_path", middleware.MustLogin(), repoCommonHandler.FileRaw)
769+
datasetsGroup.GET("/:namespace/:name/raw/*file_path", cache.Cache(memoryStore, time.Minute*2, middleware.CacheRepoInfo()), middleware.MustLogin(), repoCommonHandler.FileRaw)
767770
datasetsGroup.GET("/:namespace/:name/blob/*file_path", repoCommonHandler.FileInfo)
768771
datasetsGroup.GET("/:namespace/:name/download/*file_path", middleware.MustLogin(), repoCommonHandler.DownloadFile)
769772
datasetsGroup.GET("/:namespace/:name/resolve/*file_path", middleware.MustLogin(), repoCommonHandler.ResolveDownload)

0 commit comments

Comments
 (0)