(prd period, limit int, withCache ...bool)
| 69 | } |
| 70 | |
| 71 | func (r *ReadingTime) Sort(prd period, limit int, withCache ...bool) (users []ReadingSortedUser) { |
| 72 | var b []byte |
| 73 | cache := false |
| 74 | if len(withCache) > 0 { |
| 75 | cache = withCache[0] |
| 76 | } |
| 77 | file := fmt.Sprintf(readingTimeCacheFmt, prd, limit) |
| 78 | if cache { |
| 79 | if info, err := os.Stat(file); err == nil && time.Now().Sub(info.ModTime()).Seconds() <= cacheTime { |
| 80 | // 文件存在,且在缓存时间内 |
| 81 | if b, err = ioutil.ReadFile(file); err == nil { |
| 82 | json.Unmarshal(b, &users) |
| 83 | if len(users) > 0 { |
| 84 | return |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | sqlSort := "SELECT t.uid,sum(t.duration) sum_time,m.account,m.avatar,m.nickname FROM `md_reading_time` t left JOIN md_members m on t.uid=m.member_id WHERE m.no_rank=0 and t.day>=? and t.day<=? GROUP BY t.uid ORDER BY sum_time desc limit ?" |
| 91 | start, end := getTimeRange(time.Now(), prd) |
| 92 | orm.NewOrm().Raw(sqlSort, start, end, limit).QueryRows(&users) |
| 93 | |
| 94 | if cache && len(users) > 0 { |
| 95 | b, _ = json.Marshal(users) |
| 96 | ioutil.WriteFile(file, b, os.ModePerm) |
| 97 | } |
| 98 | return |
| 99 | } |
no test coverage detected