go time.Time beego 时间显示 到模板引擎 redis 缓存后显示 beego.AddFuncMap("stringtotime", FormatTime)
func Stringtotime(str interface{}) string{ switch str.(type) { case string: t ,err := time.ParseInLocation(timeFormart,str.(string),time.Local) if err != nil { return "" } return timeago.Chinese.Format(t) case time.Time: return timeago.Chinese.Format(str.(time.Time)) } return "" }
<span>{{.InTime | timeago}}</span>
改
<span>{{.InTime | stringtotimeago}}</span>
只缓存前两页数据 不缓存带关键字的。信息 带关键字的 后期用 elasticsearch 去检索不做redis 缓存
package models import ( "encoding/json" "github.com/astaxie/beego/orm" "utils" "strconv" "time" ) type Topic struct { Id int `orm:"pk;auto"` Title string `orm:"unique"` Content string `orm:"type(text);null"` InTime time.Time `orm:"auto_now_add"` User *User `orm:"rel(fk)"` Section *Section `orm:"rel(fk)"` View int `orm:"default(0)"` ReplyCount int `orm:"default(0)"` LastReplyUser *User `orm:"rel(fk);null"` LastReplyTime time.Time `orm:"auto_now_add"` } const ( timeFormart = "2006-01-02 15:04:05" ) type Time time.Time func (t *Time) UnmarshalJSON(data []byte) (err error) { now, err := time.ParseInLocation(`"`+timeFormart+`"`, string(data), time.Local) *t = Time(now) return } func (t Time) MarshalJSON() ([]byte, error) { b := make([]byte, 0, len(timeFormart)+2) b = append(b, '"') b = time.Time(t).AppendFormat(b, timeFormart) b = append(b, '"') return b, nil } func (u *Topic) MarshalJSON() ([]byte, error) { type Alias Topic user := &struct { InTime Time `json:"InTime" ` LastReplyTime Time `json:"LastReplyTime" ` *Alias }{Time(u.InTime),Time(u.LastReplyTime), (*Alias)(u)} return json.Marshal(user) } func (u *Topic) UnmarshalJSON(data []byte) (err error) { type Alias Topic user := &struct { InTime Time `json:"InTime" ` LastReplyTime Time `json:"LastReplyTime" ` *Alias }{Time(u.InTime), Time(u.LastReplyTime),(*Alias)(u)} err = json.Unmarshal(data, user) if err != nil { return err } user.Alias.InTime = time.Time(user.InTime) user.Alias.LastReplyTime =time.Time(user.LastReplyTime) *u = Topic(*user.Alias) return nil } func SaveTopic(topic *Topic) int64 { o := orm.NewOrm() id, _ := o.Insert(topic) return id } func FindTopicById(id int) Topic { o := orm.NewOrm() var topic Topic o.QueryTable(topic).RelatedSel().Filter("Id", id).One(&topic) return topic } func PageTopic(p int, size int, section *Section, searchKey string) utils.Page { o := orm.NewOrm() var topic Topic var list []Topic qs := o.QueryTable(topic) if section.Id > 0 { qs = qs.Filter("Section", section) } count, _ := qs.Limit(-1).Count() //qs.RelatedSel().Filter("title__icontains", searchKey).OrderBy("-InTime").Limit(size).Offset((p - 1) * size).All(&list) cond := orm.NewCondition() searchKeyCond := cond.And("title__icontains", searchKey).Or("content__contains", searchKey) qs = qs.SetCond(searchKeyCond) if section.Id > 0 { cond2 := cond.AndCond(searchKeyCond).AndCond(cond.And("Section", section.Id)) qs = qs.SetCond(cond2) } qs.RelatedSel().OrderBy("-InTime").Limit(size).Offset((p - 1) * size).All(&list) c, _ := strconv.Atoi(strconv.FormatInt(count, 10)) return utils.PageUtil(c, p, size, list) } func IncrView(topic *Topic) { o := orm.NewOrm() topic.View = topic.View + 1 o.Update(topic, "View") } func IncrReplyCount(topic *Topic) { o := orm.NewOrm() topic.ReplyCount = topic.ReplyCount + 1 o.Update(topic, "ReplyCount") } func ReduceReplyCount(topic *Topic) { o := orm.NewOrm() topic.ReplyCount = topic.ReplyCount - 1 o.Update(topic, "ReplyCount") } func FindTopicByUser(user *User, limit int) []*Topic { o := orm.NewOrm() var topic Topic var topics []*Topic o.QueryTable(topic).RelatedSel().Filter("User", user).OrderBy("-LastReplyTime", "-InTime").Limit(limit).All(&topics) return topics } func UpdateTopic(topic *Topic) { o := orm.NewOrm() o.Update(topic) } func DeleteTopic(topic *Topic) { o := orm.NewOrm() o.Delete(topic) } func DeleteTopicByUser(user *User) { o := orm.NewOrm() o.Raw("delete from topic where user_id = ?", user.Id).Exec() } func FindTopicByTitle(title string) Topic { o := orm.NewOrm() var topic Topic o.QueryTable(topic).Filter("title__icontains", title).One(&topic) return topic }