Blog cache
This commit is contained in:
+17
-2
@@ -28,7 +28,6 @@ type Blog struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *BlogApp) ConvertBlog(blog *Blog) {
|
func (b *BlogApp) ConvertBlog(blog *Blog) {
|
||||||
//TODO: parse BBCode/Markdown from blog
|
|
||||||
if !blog.StaticPage {
|
if !blog.StaticPage {
|
||||||
blog.Blog = b.conv.HTMLConvert(blog.Blog)
|
blog.Blog = b.conv.HTMLConvert(blog.Blog)
|
||||||
}
|
}
|
||||||
@@ -48,6 +47,12 @@ func (b *BlogApp) GetAuthor(blog *Blog) (*Author, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *BlogApp) Blog(ID string) (*Blog, error) {
|
func (b *BlogApp) Blog(ID string) (*Blog, error) {
|
||||||
|
b.cacheMutex.RLock()
|
||||||
|
blog, has := b.blogCache[ID]
|
||||||
|
b.cacheMutex.RUnlock()
|
||||||
|
if has {
|
||||||
|
return &blog, nil
|
||||||
|
}
|
||||||
res := b.blogCol.FindOne(context.Background(), bson.M{"_id": ID, "draft": false})
|
res := b.blogCol.FindOne(context.Background(), bson.M{"_id": ID, "draft": false})
|
||||||
if res.Err() != nil {
|
if res.Err() != nil {
|
||||||
if res.Err() == mongo.ErrNoDocuments {
|
if res.Err() == mongo.ErrNoDocuments {
|
||||||
@@ -55,15 +60,25 @@ func (b *BlogApp) Blog(ID string) (*Blog, error) {
|
|||||||
}
|
}
|
||||||
return nil, res.Err()
|
return nil, res.Err()
|
||||||
}
|
}
|
||||||
var blog Blog
|
|
||||||
err := res.Decode(&blog)
|
err := res.Decode(&blog)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
b.ConvertBlog(&blog)
|
b.ConvertBlog(&blog)
|
||||||
|
b.cacheMutex.Lock()
|
||||||
|
b.blogCache[ID] = blog
|
||||||
|
b.cacheMutex.Unlock()
|
||||||
|
go b.CleanCache(ID)
|
||||||
return &blog, nil
|
return &blog, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *BlogApp) CleanCache(ID string) {
|
||||||
|
time.Sleep(5 * time.Minute)
|
||||||
|
b.cacheMutex.Lock()
|
||||||
|
delete(b.blogCache, ID)
|
||||||
|
b.cacheMutex.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
func (b *BlogApp) reqBlog(w http.ResponseWriter, r *http.Request) {
|
func (b *BlogApp) reqBlog(w http.ResponseWriter, r *http.Request) {
|
||||||
blogID := r.PathValue("blogID")
|
blogID := r.PathValue("blogID")
|
||||||
if blogID == "" {
|
if blogID == "" {
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package blog
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/CalebQ42/bbConvert"
|
"github.com/CalebQ42/bbConvert"
|
||||||
"github.com/CalebQ42/darkstorm-server/internal/backend"
|
"github.com/CalebQ42/darkstorm-server/internal/backend"
|
||||||
@@ -14,6 +15,9 @@ type BlogApp struct {
|
|||||||
authCol *mongo.Collection
|
authCol *mongo.Collection
|
||||||
portfolioCol *mongo.Collection
|
portfolioCol *mongo.Collection
|
||||||
conv bbConvert.ComboConverter
|
conv bbConvert.ComboConverter
|
||||||
|
|
||||||
|
cacheMutex *sync.RWMutex
|
||||||
|
blogCache map[string]Blog
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewBlogApp(db *mongo.Database) *BlogApp {
|
func NewBlogApp(db *mongo.Database) *BlogApp {
|
||||||
@@ -22,6 +26,8 @@ func NewBlogApp(db *mongo.Database) *BlogApp {
|
|||||||
authCol: db.Collection("author"),
|
authCol: db.Collection("author"),
|
||||||
portfolioCol: db.Collection("portfolio"),
|
portfolioCol: db.Collection("portfolio"),
|
||||||
conv: bbConvert.NewComboConverter(),
|
conv: bbConvert.NewComboConverter(),
|
||||||
|
cacheMutex: &sync.RWMutex{},
|
||||||
|
blogCache: make(map[string]Blog),
|
||||||
}
|
}
|
||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user