mirror of
https://github.com/mindoc-org/mindoc.git
synced 2025-10-21 03:17:25 +08:00
实现文档缓存
This commit is contained in:
@@ -2,7 +2,6 @@ package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"bytes"
|
||||
"fmt"
|
||||
"github.com/astaxie/beego"
|
||||
@@ -13,6 +12,9 @@ import (
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
"github.com/lifei6671/mindoc/cache"
|
||||
"encoding/json"
|
||||
"qiniupkg.com/x/errors.v7"
|
||||
)
|
||||
|
||||
// Document struct.
|
||||
@@ -63,6 +65,9 @@ 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)
|
||||
@@ -70,55 +75,85 @@ func (m *Document) Find(id int) (*Document, error) {
|
||||
if err == orm.ErrNoRows {
|
||||
return m, ErrDataNotExist
|
||||
}
|
||||
m.PutToCache()
|
||||
return m, nil
|
||||
}
|
||||
|
||||
//插入和更新文档.
|
||||
func (m *Document) InsertOrUpdate(cols ...string) error {
|
||||
o := orm.NewOrm()
|
||||
|
||||
var err error
|
||||
if m.DocumentId > 0 {
|
||||
_, err := o.Update(m)
|
||||
return err
|
||||
_, err = o.Update(m)
|
||||
} else {
|
||||
_, err := o.Insert(m)
|
||||
_, err = o.Insert(m)
|
||||
NewBook().ResetDocumentNumber(m.BookId)
|
||||
}
|
||||
if err != nil {
|
||||
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
|
||||
}
|
||||
|
||||
//递归删除一个文档.
|
||||
func (m *Document) RecursiveDocument(doc_id int) error {
|
||||
func (m *Document) RecursiveDocument(docId int) error {
|
||||
|
||||
o := orm.NewOrm()
|
||||
|
||||
if doc, err := m.Find(doc_id); err == nil {
|
||||
if doc, err := m.Find(docId); err == nil {
|
||||
o.Delete(doc)
|
||||
NewDocumentHistory().Clear(doc.DocumentId)
|
||||
}
|
||||
//
|
||||
//var docs []*Document
|
||||
//
|
||||
//_, err := o.QueryTable(m.TableNameWithPrefix()).Filter("parent_id", doc_id).All(&docs)
|
||||
|
||||
var docs []*Document
|
||||
|
||||
_, err := o.QueryTable(m.TableNameWithPrefix()).Filter("parent_id", doc_id).All(&docs)
|
||||
var maps []orm.Params
|
||||
|
||||
_, err := o.Raw("SELECT document_id FROM " + m.TableNameWithPrefix() + " WHERE parent_id=" + strconv.Itoa(docId)).Values(&maps)
|
||||
if err != nil {
|
||||
beego.Error("RecursiveDocument => ", err)
|
||||
return err
|
||||
}
|
||||
|
||||
for _, item := range docs {
|
||||
doc_id := item.DocumentId
|
||||
o.QueryTable(m.TableNameWithPrefix()).Filter("document_id", doc_id).Delete()
|
||||
m.RecursiveDocument(doc_id)
|
||||
for _, item := range maps {
|
||||
if docId,ok := item["document_id"].(string); ok{
|
||||
id,_ := strconv.Atoi(docId)
|
||||
o.QueryTable(m.TableNameWithPrefix()).Filter("document_id", id).Delete()
|
||||
m.RecursiveDocument(id)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -178,16 +213,54 @@ func (m *Document) ReleaseContent(bookId int) {
|
||||
if err != nil {
|
||||
beego.Error(fmt.Sprintf("发布失败 => %+v", item), err)
|
||||
}else {
|
||||
//当文档发布后,需要清除已缓存的转换文档和文档缓存
|
||||
item.PutToCache()
|
||||
os.RemoveAll(filepath.Join(conf.WorkingDirectory,"uploads","books",strconv.Itoa(bookId)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//将文档写入缓存
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
//从缓存获取
|
||||
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{
|
||||
return m,nil
|
||||
}
|
||||
}
|
||||
return nil,errors.New("Cache not exists")
|
||||
}
|
||||
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{
|
||||
return m,nil
|
||||
}
|
||||
}
|
||||
return nil,errors.New("Cache not exists")
|
||||
}
|
||||
|
||||
//根据项目ID查询文档列表.
|
||||
func (m *Document) FindListByBookId(book_id int) (docs []*Document, err error) {
|
||||
func (m *Document) FindListByBookId(bookId int) (docs []*Document, err error) {
|
||||
o := orm.NewOrm()
|
||||
|
||||
_, err = o.QueryTable(m.TableNameWithPrefix()).Filter("book_id", book_id).OrderBy("order_sort").All(&docs)
|
||||
_, err = o.QueryTable(m.TableNameWithPrefix()).Filter("book_id", bookId).OrderBy("order_sort").All(&docs)
|
||||
|
||||
return
|
||||
}
|
||||
|
Reference in New Issue
Block a user