|
|
@@ -4,9 +4,87 @@ import ( |
|
|
|
"code.gitea.io/gitea/models" |
|
|
|
"code.gitea.io/gitea/modules/log" |
|
|
|
"code.gitea.io/gitea/modules/setting" |
|
|
|
"encoding/json" |
|
|
|
"github.com/patrickmn/go-cache" |
|
|
|
"time" |
|
|
|
) |
|
|
|
|
|
|
|
var repoSquareCache = cache.New(2*time.Minute, 1*time.Minute) |
|
|
|
|
|
|
|
const ( |
|
|
|
RREFERED_CACHE = "PreferredRepos" |
|
|
|
REPO_BANNER_CACHE = "RepoBanner" |
|
|
|
TOPICS_CACHE = "RepoTopics" |
|
|
|
RECOMMEND_CACHE = "RecommendRepos" |
|
|
|
) |
|
|
|
|
|
|
|
func GetBanners() []map[string]string { |
|
|
|
v, success := repoSquareCache.Get(REPO_BANNER_CACHE) |
|
|
|
if success { |
|
|
|
log.Debug("GetBanners from cache,value = %v", v) |
|
|
|
if v == nil { |
|
|
|
return nil |
|
|
|
} |
|
|
|
r := v.([]map[string]string) |
|
|
|
return r |
|
|
|
} |
|
|
|
repoMap := getMapContent("repos/square_banner") |
|
|
|
repoSquareCache.Set(REPO_BANNER_CACHE, repoMap, 1*time.Minute) |
|
|
|
return repoMap |
|
|
|
} |
|
|
|
|
|
|
|
func GetTopics() []map[string]string { |
|
|
|
v, success := repoSquareCache.Get(TOPICS_CACHE) |
|
|
|
if success { |
|
|
|
log.Debug("GetTopics from cache,value = %v", v) |
|
|
|
if v == nil { |
|
|
|
return nil |
|
|
|
} |
|
|
|
r := v.([]map[string]string) |
|
|
|
return r |
|
|
|
} |
|
|
|
repoMap := getMapContent("repos/recommend_topics") |
|
|
|
repoSquareCache.Set(TOPICS_CACHE, repoMap, 1*time.Minute) |
|
|
|
return repoMap |
|
|
|
} |
|
|
|
|
|
|
|
func getMapContent(fileName string) []map[string]string { |
|
|
|
url := setting.RecommentRepoAddr + fileName |
|
|
|
result, err := RecommendContentFromPromote(url) |
|
|
|
remap := make([]map[string]string, 0) |
|
|
|
if err == nil { |
|
|
|
json.Unmarshal([]byte(result), &remap) |
|
|
|
} |
|
|
|
return remap |
|
|
|
} |
|
|
|
|
|
|
|
func GetRecommendRepos() []map[string]interface{} { |
|
|
|
v, success := repoSquareCache.Get(RECOMMEND_CACHE) |
|
|
|
if success { |
|
|
|
log.Debug("GetRecommendRepos from cache,value = %v", v) |
|
|
|
if v == nil { |
|
|
|
return nil |
|
|
|
} |
|
|
|
r := v.([]map[string]interface{}) |
|
|
|
return r |
|
|
|
} |
|
|
|
repoMap := getMapContent("home/projects") |
|
|
|
r, _ := GetRecommendRepoFromPromote(repoMap) |
|
|
|
repoSquareCache.Set(RECOMMEND_CACHE, r, 1*time.Minute) |
|
|
|
return r |
|
|
|
} |
|
|
|
|
|
|
|
func GetPreferredRepos() ([]*models.Repository4Card, error) { |
|
|
|
v, success := repoSquareCache.Get(RREFERED_CACHE) |
|
|
|
if success { |
|
|
|
log.Debug("GetPreferredRepos from cache,value = %v", v) |
|
|
|
if v == nil { |
|
|
|
return nil, nil |
|
|
|
} |
|
|
|
r := v.([]*models.Repository4Card) |
|
|
|
return r, nil |
|
|
|
} |
|
|
|
|
|
|
|
repos, err := models.GetSelectedRepos(models.FindSelectedReposOpts{ |
|
|
|
ListOptions: models.ListOptions{ |
|
|
|
PageSize: 10, |
|
|
@@ -21,6 +99,8 @@ func GetPreferredRepos() ([]*models.Repository4Card, error) { |
|
|
|
for i, r := range repos { |
|
|
|
result[i] = r.ToCardFormat() |
|
|
|
} |
|
|
|
|
|
|
|
repoSquareCache.Set(RREFERED_CACHE, result, 1*time.Minute) |
|
|
|
return result, nil |
|
|
|
} |
|
|
|
|
|
|
@@ -51,7 +131,7 @@ func GetHotPaperRepos() ([]*models.Repository4Card, error) { |
|
|
|
}, |
|
|
|
OrderBy: models.SearchOrderByLastMonthVisitsReverse + "," + models.SearchOrderByRecentUpdated, |
|
|
|
TopicOnly: true, |
|
|
|
TopicName: "openi-paper", |
|
|
|
TopicName: setting.PaperRepoTopicName, |
|
|
|
AllPublic: true, |
|
|
|
}) |
|
|
|
if err != nil { |
|
|
|