mirror of
https://github.com/mindoc-org/mindoc.git
synced 2025-10-20 19:07:25 +08:00
1、优化文档缓存逻辑
2、新增标签管理功能 3、优化部分页面显示效果
This commit is contained in:
@@ -22,6 +22,8 @@ type Book struct {
|
||||
Identify string `orm:"column(identify);size(100);unique" json:"identify"`
|
||||
//是否是自动发布 0 否/1 是
|
||||
AutoRelease int `orm:"column(auto_release);type(int);default(0)" json:"auto_release"`
|
||||
//是否开启下载功能 0 是/1 否
|
||||
IsDownload int `orm:"column(is_download);type(int);default(0)" json:"is_download"`
|
||||
OrderIndex int `orm:"column(order_index);type(int);default(0)" json:"order_index"`
|
||||
// Description 项目描述.
|
||||
Description string `orm:"column(description);size(2000)" json:"description"`
|
||||
@@ -129,7 +131,7 @@ func (m *Book) Update(cols ...string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if (m.Label + temp.Label) != "" {
|
||||
if m.Label != "" || temp.Label != ""{
|
||||
|
||||
go NewLabel().InsertOrUpdateMulti(m.Label + "," + temp.Label)
|
||||
}
|
||||
@@ -314,16 +316,16 @@ func (m *Book) FindForHomeToPager(pageIndex, pageSize, member_id int) (books []*
|
||||
}
|
||||
|
||||
//分页全局搜索.
|
||||
func (m *Book) FindForLabelToPager(keyword string, pageIndex, pageSize, member_id int) (books []*BookResult, totalCount int, err error) {
|
||||
func (m *Book) FindForLabelToPager(keyword string, pageIndex, pageSize, memberId int) (books []*BookResult, totalCount int, err error) {
|
||||
o := orm.NewOrm()
|
||||
|
||||
keyword = "%" + keyword + "%"
|
||||
offset := (pageIndex - 1) * pageSize
|
||||
//如果是登录用户
|
||||
if member_id > 0 {
|
||||
if memberId > 0 {
|
||||
sql1 := "SELECT COUNT(*) FROM md_books AS book LEFT JOIN md_relationship AS rel ON rel.book_id = book.book_id AND rel.member_id = ? WHERE (relationship_id > 0 OR book.privately_owned = 0) AND book.label LIKE ?"
|
||||
|
||||
err = o.Raw(sql1, member_id, keyword).QueryRow(&totalCount)
|
||||
err = o.Raw(sql1, memberId, keyword).QueryRow(&totalCount)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -333,7 +335,7 @@ func (m *Book) FindForLabelToPager(keyword string, pageIndex, pageSize, member_i
|
||||
LEFT JOIN md_members AS member ON rel1.member_id = member.member_id
|
||||
WHERE (rel.relationship_id > 0 OR book.privately_owned = 0) AND book.label LIKE ? ORDER BY order_index DESC ,book.book_id DESC LIMIT ?,?`
|
||||
|
||||
_, err = o.Raw(sql2, member_id, keyword, offset, pageSize).QueryRows(&books)
|
||||
_, err = o.Raw(sql2, memberId, keyword, offset, pageSize).QueryRows(&books)
|
||||
|
||||
return
|
||||
|
||||
@@ -359,12 +361,12 @@ func (m *Book) FindForLabelToPager(keyword string, pageIndex, pageSize, member_i
|
||||
}
|
||||
|
||||
//重置文档数量
|
||||
func (m *Book) ResetDocumentNumber(book_id int) {
|
||||
func (m *Book) ResetDocumentNumber(bookId int) {
|
||||
o := orm.NewOrm()
|
||||
|
||||
totalCount, err := o.QueryTable(NewDocument().TableNameWithPrefix()).Filter("book_id", book_id).Count()
|
||||
totalCount, err := o.QueryTable(NewDocument().TableNameWithPrefix()).Filter("book_id", bookId).Count()
|
||||
if err == nil {
|
||||
o.Raw("UPDATE md_books SET doc_count = ? WHERE book_id = ?", int(totalCount), book_id).Exec()
|
||||
o.Raw("UPDATE md_books SET doc_count = ? WHERE book_id = ?", int(totalCount), bookId).Exec()
|
||||
} else {
|
||||
beego.Error(err)
|
||||
}
|
||||
|
@@ -51,6 +51,7 @@ type BookResult struct {
|
||||
|
||||
LastModifyText string `json:"last_modify_text"`
|
||||
IsDisplayComment bool `json:"is_display_comment"`
|
||||
IsDownload bool `json:"is_download"`
|
||||
}
|
||||
|
||||
func NewBookResult() *BookResult {
|
||||
@@ -174,6 +175,7 @@ func (m *BookResult) ToBookResult(book Book) *BookResult {
|
||||
m.AutoRelease = book.AutoRelease == 1
|
||||
m.Publisher = book.Publisher
|
||||
m.HistoryCount = book.HistoryCount
|
||||
m.IsDownload = book.IsDownload == 0
|
||||
|
||||
if book.Theme == "" {
|
||||
m.Theme = "default"
|
||||
|
@@ -14,7 +14,6 @@ import (
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
"github.com/lifei6671/mindoc/cache"
|
||||
"encoding/json"
|
||||
"qiniupkg.com/x/errors.v7"
|
||||
)
|
||||
|
||||
// Document struct.
|
||||
@@ -65,9 +64,7 @@ func (m *Document) Find(id int) (*Document, error) {
|
||||
if id <= 0 {
|
||||
return m, ErrInvalidParameter
|
||||
}
|
||||
if m,err := m.FromCacheById(id); err == nil {
|
||||
return m,nil
|
||||
}
|
||||
|
||||
o := orm.NewOrm()
|
||||
|
||||
err := o.QueryTable(m.TableNameWithPrefix()).Filter("document_id", id).One(m)
|
||||
@@ -75,7 +72,7 @@ func (m *Document) Find(id int) (*Document, error) {
|
||||
if err == orm.ErrNoRows {
|
||||
return m, ErrDataNotExist
|
||||
}
|
||||
m.PutToCache()
|
||||
|
||||
return m, nil
|
||||
}
|
||||
|
||||
@@ -93,36 +90,16 @@ func (m *Document) InsertOrUpdate(cols ...string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
m.PutToCache()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
//根据指定字段查询一条文档.
|
||||
func (m *Document) FindByFieldFirst(field string, v interface{}) (*Document, error) {
|
||||
|
||||
if field == "identify" {
|
||||
if identify,ok := v.(string);ok {
|
||||
if m,err := m.FromCacheByIdentify(identify);err == nil{
|
||||
return m,nil
|
||||
}
|
||||
}
|
||||
}else if field == "document_id" {
|
||||
if id,ok := v.(int); ok && id > 0 {
|
||||
if m,err := m.FromCacheById(id);err == nil{
|
||||
return m,nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
o := orm.NewOrm()
|
||||
|
||||
err := o.QueryTable(m.TableNameWithPrefix()).Filter(field, v).One(m)
|
||||
|
||||
if err == nil {
|
||||
m.PutToCache()
|
||||
}
|
||||
|
||||
return m, err
|
||||
}
|
||||
|
||||
@@ -165,7 +142,7 @@ func (m *Document) ReleaseContent(bookId int) {
|
||||
o := orm.NewOrm()
|
||||
|
||||
var docs []*Document
|
||||
_, err := o.QueryTable(m.TableNameWithPrefix()).Filter("book_id", bookId).All(&docs, "document_id", "content")
|
||||
_, err := o.QueryTable(m.TableNameWithPrefix()).Filter("book_id", bookId).All(&docs, "document_id","identify", "content")
|
||||
|
||||
if err != nil {
|
||||
beego.Error("发布失败 => ", err)
|
||||
@@ -214,7 +191,12 @@ func (m *Document) ReleaseContent(bookId int) {
|
||||
beego.Error(fmt.Sprintf("发布失败 => %+v", item), err)
|
||||
}else {
|
||||
//当文档发布后,需要清除已缓存的转换文档和文档缓存
|
||||
item.PutToCache()
|
||||
if doc,err := NewDocument().Find(item.DocumentId); err == nil {
|
||||
doc.PutToCache()
|
||||
}else{
|
||||
doc.RemoveCache()
|
||||
}
|
||||
|
||||
os.RemoveAll(filepath.Join(conf.WorkingDirectory,"uploads","books",strconv.Itoa(bookId)))
|
||||
}
|
||||
}
|
||||
@@ -222,38 +204,54 @@ func (m *Document) ReleaseContent(bookId int) {
|
||||
|
||||
//将文档写入缓存
|
||||
func (m *Document) PutToCache(){
|
||||
if v,err := json.Marshal(m);err == nil {
|
||||
if m.Identify == "" {
|
||||
if err := cache.Put("Document.Id."+strconv.Itoa(m.DocumentId), v, time.Second*3600); err != nil {
|
||||
beego.Info("文档缓存失败:", m)
|
||||
}
|
||||
}else{
|
||||
if err := cache.Put("Document.Identify."+ m.Identify, v, time.Second*3600); err != nil {
|
||||
beego.Info("文档缓存失败:", m)
|
||||
go func(m Document) {
|
||||
if v,err := json.Marshal(&m);err == nil {
|
||||
if m.Identify == "" {
|
||||
|
||||
if err := cache.Put("Document.Id." + strconv.Itoa(m.DocumentId), v, time.Second*3600); err != nil {
|
||||
beego.Info("文档缓存失败:", m.DocumentId)
|
||||
}
|
||||
}else{
|
||||
if err := cache.Put("Document.Identify."+ m.Identify, v, time.Second*3600); err != nil {
|
||||
beego.Info("文档缓存失败:", m.DocumentId)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}(*m)
|
||||
}
|
||||
//清除缓存
|
||||
func (m *Document) RemoveCache() {
|
||||
go func(m Document) {
|
||||
cache.Put("Document.Id." + strconv.Itoa(m.DocumentId), m, time.Second*3600);
|
||||
|
||||
if m.Identify != "" {
|
||||
cache.Put("Document.Identify."+ m.Identify, m, time.Second*3600);
|
||||
}
|
||||
}(*m)
|
||||
}
|
||||
|
||||
//从缓存获取
|
||||
func (m *Document) FromCacheById(id int) (*Document,error) {
|
||||
b := cache.Get("Document.Id." + strconv.Itoa(id))
|
||||
if v,ok := b.([]byte); ok {
|
||||
beego.Info("从缓存中获取文档信息成功")
|
||||
|
||||
if err := json.Unmarshal(v,m);err == nil{
|
||||
beego.Info("从缓存中获取文档信息成功",m.DocumentId)
|
||||
return m,nil
|
||||
}
|
||||
}
|
||||
return nil,errors.New("Cache not exists")
|
||||
return m.Find(id)
|
||||
}
|
||||
//根据文档标识从缓存中查询文档
|
||||
func (m *Document) FromCacheByIdentify(identify string) (*Document,error) {
|
||||
b := cache.Get("Document.Identify." + identify)
|
||||
if v,ok := b.([]byte); ok {
|
||||
beego.Info("从缓存中获取文档信息成功")
|
||||
if err := json.Unmarshal(v,m);err == nil{
|
||||
beego.Info("从缓存中获取文档信息成功",m.DocumentId)
|
||||
return m,nil
|
||||
}
|
||||
}
|
||||
return nil,errors.New("Cache not exists")
|
||||
return m.FindByFieldFirst("identify",identify)
|
||||
}
|
||||
|
||||
//根据项目ID查询文档列表.
|
||||
|
@@ -72,6 +72,16 @@ func (m *Label) InsertOrUpdateMulti(labels string) {
|
||||
}
|
||||
}
|
||||
}
|
||||
//删除标签
|
||||
func (m *Label) Delete() error {
|
||||
o := orm.NewOrm()
|
||||
_,err := o.Raw("DELETE FROM " + m.TableNameWithPrefix() + " WHERE label_id= ?",m.LabelId).Exec()
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
//分页查找标签.
|
||||
func (m *Label) FindToPager(pageIndex, pageSize int) (labels []*Label, totalCount int, err error) {
|
||||
@@ -90,3 +100,7 @@ func (m *Label) FindToPager(pageIndex, pageSize int) (labels []*Label, totalCoun
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
@@ -9,6 +9,7 @@ import (
|
||||
type MemberRelationshipResult struct {
|
||||
MemberId int `json:"member_id"`
|
||||
Account string `json:"account"`
|
||||
RealName string `json:"real_name"`
|
||||
Description string `json:"description"`
|
||||
Email string `json:"email"`
|
||||
Phone string `json:"phone"`
|
||||
@@ -47,6 +48,7 @@ func (m *MemberRelationshipResult) FromMember(member *Member) *MemberRelationshi
|
||||
m.Status = member.Status
|
||||
m.CreateTime = member.CreateTime
|
||||
m.CreateAt = member.CreateAt
|
||||
m.RealName = member.RealName
|
||||
|
||||
return m
|
||||
}
|
||||
|
Reference in New Issue
Block a user