go time.Time beego 时间显示 到模板引擎 redis 缓存后显示

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
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值