| 202 | } |
| 203 | |
| 204 | func (m *Sign) Sorted(limit int, orderField string, withCache ...bool) (members []Member) { |
| 205 | var b []byte |
| 206 | cache := false |
| 207 | if len(withCache) > 0 { |
| 208 | cache = withCache[0] |
| 209 | } |
| 210 | file := fmt.Sprintf(signCacheFmt, orderField, limit) |
| 211 | if cache { |
| 212 | if info, err := os.Stat(file); err == nil && time.Now().Sub(info.ModTime()).Seconds() <= cacheTime { |
| 213 | // 文件存在,且在缓存时间内 |
| 214 | if b, err = ioutil.ReadFile(file); err == nil { |
| 215 | json.Unmarshal(b, &members) |
| 216 | if len(members) > 0 { |
| 217 | return |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | member := NewMember() |
| 224 | o := orm.NewOrm() |
| 225 | fields := []string{"member_id", "account", "nickname", "total_continuous_sign", "total_sign", "total_reading_time", "history_total_continuous_sign"} |
| 226 | o.QueryTable(member).Filter("no_rank", 0).OrderBy("-"+orderField).Limit(limit).All(&members, fields...) |
| 227 | |
| 228 | if cache && len(members) > 0 { |
| 229 | b, _ = json.Marshal(members) |
| 230 | ioutil.WriteFile(file, b, os.ModePerm) |
| 231 | } |
| 232 | |
| 233 | return |
| 234 | } |
| 235 | |
| 236 | func (*Sign) LatestOne(uid int) (s Sign) { |
| 237 | orm.NewOrm().QueryTable(&s).Filter("uid", uid).OrderBy("-id").One(&s) |