Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions api/middleware/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,7 @@ func getCacheStrategyTrendingReposByRequest(c *gin.Context) (bool, cache.Strateg
CacheDuration: 2 * time.Minute,
}
}

func CacheRepoInfo() cache.Option {
return cache.WithCacheStrategyByRequest(getCacheStrategyRepoInfoByRequest)
}
12 changes: 12 additions & 0 deletions api/middleware/cache_ce.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//go:build !saas

package middleware

import (
cache "github.com/chenyahui/gin-cache"
"github.com/gin-gonic/gin"
)

func getCacheStrategyRepoInfoByRequest(c *gin.Context) (bool, cache.Strategy) {
return false, cache.Strategy{}
}
15 changes: 9 additions & 6 deletions api/router/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ func createModelRoutes(config *config.Config,
modelsGroup.GET("", cache.Cache(memoryStore, time.Minute, middleware.CacheStrategyTrendingRepos()), modelHandler.Index)
modelsGroup.PUT("/:namespace/:name", middlewareCollection.Auth.NeedLogin, modelHandler.Update)
modelsGroup.DELETE("/:namespace/:name", middlewareCollection.Auth.NeedLogin, modelHandler.Delete)
modelsGroup.GET("/:namespace/:name", modelHandler.Show)
modelsGroup.GET("/:namespace/:name", cache.Cache(memoryStore, time.Minute*2, middleware.CacheRepoInfo()), modelHandler.Show)
modelsGroup.GET("/:namespace/:name/relations", modelHandler.Relations)
modelsGroup.PUT("/:namespace/:name/relations", middlewareCollection.Auth.NeedAdmin, modelHandler.SetRelations)
modelsGroup.POST("/:namespace/:name/relations/dataset", middlewareCollection.Auth.NeedPhoneVerified, modelHandler.AddDatasetRelation)
Expand All @@ -611,7 +611,7 @@ func createModelRoutes(config *config.Config,
modelsGroup.GET("/:namespace/:name/tags", repoCommonHandler.Tags)
modelsGroup.POST("/:namespace/:name/preupload/:revision", middlewareCollection.Auth.NeedPhoneVerified, repoCommonHandler.Preupload)
// update tags of a certain category
modelsGroup.GET("/:namespace/:name/all_files", repoCommonHandler.AllFiles)
modelsGroup.GET("/:namespace/:name/all_files", cache.Cache(memoryStore, time.Minute*2, middleware.CacheRepoInfo()), repoCommonHandler.AllFiles)
modelsGroup.POST("/:namespace/:name/tags/:category", middlewareCollection.Auth.NeedPhoneVerified, repoCommonHandler.UpdateTags)
modelsGroup.GET("/:namespace/:name/last_commit", repoCommonHandler.LastCommit)
modelsGroup.GET("/:namespace/:name/commit/:commit_id", repoCommonHandler.CommitWithDiff)
Expand All @@ -623,7 +623,7 @@ func createModelRoutes(config *config.Config,
modelsGroup.GET("/:namespace/:name/refs/:ref/remote_tree/*path", repoCommonHandler.RemoteTree)
modelsGroup.GET("/:namespace/:name/refs/:ref/logs_tree/*path", repoCommonHandler.LogsTree)
modelsGroup.GET("/:namespace/:name/commits", repoCommonHandler.Commits)
modelsGroup.GET("/:namespace/:name/raw/*file_path", repoCommonHandler.FileRaw)
modelsGroup.GET("/:namespace/:name/raw/*file_path", cache.Cache(memoryStore, time.Minute*2, middleware.CacheRepoInfo()), repoCommonHandler.FileRaw)
modelsGroup.GET("/:namespace/:name/blob/*file_path", repoCommonHandler.FileInfo)
// The DownloadFile method differs from the SDKDownload interface in a few ways

Expand Down Expand Up @@ -735,6 +735,9 @@ func createDatasetRoutes(
dsHandler *handler.DatasetHandler,
repoCommonHandler *handler.RepoHandler,
) {
// gin cache
memoryStore := persist.NewMemoryStore(2 * time.Minute)

datasetsGroup := apiGroup.Group("/datasets")
// allow access without login
datasetsGroup.GET("", dsHandler.Index)
Expand All @@ -744,13 +747,13 @@ func createDatasetRoutes(
datasetsGroup.POST("", middlewareCollection.Auth.NeedPhoneVerified, dsHandler.Create)
datasetsGroup.PUT("/:namespace/:name", middleware.MustLogin(), dsHandler.Update)
datasetsGroup.DELETE("/:namespace/:name", middleware.MustLogin(), dsHandler.Delete)
datasetsGroup.GET("/:namespace/:name", dsHandler.Show)
datasetsGroup.GET("/:namespace/:name", cache.Cache(memoryStore, time.Minute*2, middleware.CacheRepoInfo()), dsHandler.Show)
datasetsGroup.GET("/:namespace/:name/relations", middleware.MustLogin(), dsHandler.Relations)
datasetsGroup.GET("/:namespace/:name/branches", middleware.MustLogin(), repoCommonHandler.Branches)
datasetsGroup.GET("/:namespace/:name/tags", middleware.MustLogin(), repoCommonHandler.Tags)
datasetsGroup.POST("/:namespace/:name/preupload/:revision", middlewareCollection.Auth.NeedPhoneVerified, repoCommonHandler.Preupload)
// update tags of a certain category
datasetsGroup.GET("/:namespace/:name/all_files", middleware.MustLogin(), repoCommonHandler.AllFiles)
datasetsGroup.GET("/:namespace/:name/all_files", cache.Cache(memoryStore, time.Minute*2, middleware.CacheRepoInfo()), middleware.MustLogin(), repoCommonHandler.AllFiles)
datasetsGroup.POST("/:namespace/:name/tags/:category", middleware.MustLogin(), repoCommonHandler.UpdateTags)
datasetsGroup.GET("/:namespace/:name/last_commit", repoCommonHandler.LastCommit)
datasetsGroup.GET("/:namespace/:name/commit/:commit_id", middleware.MustLogin(), repoCommonHandler.CommitWithDiff)
Expand All @@ -763,7 +766,7 @@ func createDatasetRoutes(
datasetsGroup.GET("/:namespace/:name/refs/:ref/logs_tree/*path", middleware.MustLogin(), repoCommonHandler.LogsTree)
datasetsGroup.GET("/:namespace/:name/commits", middleware.MustLogin(), repoCommonHandler.Commits)
datasetsGroup.POST("/:namespace/:name/raw/*file_path", middlewareCollection.Auth.NeedPhoneVerified, repoCommonHandler.CreateFile)
datasetsGroup.GET("/:namespace/:name/raw/*file_path", middleware.MustLogin(), repoCommonHandler.FileRaw)
datasetsGroup.GET("/:namespace/:name/raw/*file_path", cache.Cache(memoryStore, time.Minute*2, middleware.CacheRepoInfo()), middleware.MustLogin(), repoCommonHandler.FileRaw)
datasetsGroup.GET("/:namespace/:name/blob/*file_path", repoCommonHandler.FileInfo)
datasetsGroup.GET("/:namespace/:name/download/*file_path", middleware.MustLogin(), repoCommonHandler.DownloadFile)
datasetsGroup.GET("/:namespace/:name/resolve/*file_path", middleware.MustLogin(), repoCommonHandler.ResolveDownload)
Expand Down
Loading