MCPcopy Index your code
hub / github.com/TruthHun/BookStack / SortedByPeriod

Method SortedByPeriod

models/sign.go:241–269  ·  view source on GitHub ↗
(limit int, prd period, withCache ...bool)

Source from the content-addressed store, hash-verified

239}
240
241func (m *Sign) SortedByPeriod(limit int, prd period, withCache ...bool) (members []Member) {
242
243 var b []byte
244 cache := false
245 if len(withCache) > 0 {
246 cache = withCache[0]
247 }
248 file := fmt.Sprintf(signCacheFmt, "month-"+prd, limit)
249 if cache {
250 if info, err := os.Stat(file); err == nil && time.Now().Sub(info.ModTime()).Seconds() <= cacheTime {
251 // 文件存在,且在缓存时间内
252 if b, err = ioutil.ReadFile(file); err == nil {
253 json.Unmarshal(b, &members)
254 if len(members) > 0 {
255 return
256 }
257 }
258 }
259 }
260
261 sqlSort := "SELECT t.uid member_id,count(t.id) total_sign,m.account,m.avatar,m.nickname FROM `md_sign` t left JOIN md_members m on t.uid=m.member_id WHERE t.day>=? and t.day<=? GROUP BY t.uid ORDER BY total_sign desc limit ?"
262 start, end := getTimeRange(time.Now(), prd)
263 orm.NewOrm().Raw(sqlSort, start, end, limit).QueryRows(&members)
264 if cache && len(members) > 0 {
265 b, _ = json.Marshal(members)
266 ioutil.WriteFile(file, b, os.ModePerm)
267 }
268 return
269}

Callers 2

IndexMethod · 0.95
RankMethod · 0.95

Calls 1

getTimeRangeFunction · 0.85

Tested by

no test coverage detected