Fix some cache errors with editor

This commit is contained in:
Caleb Gardner
2024-11-14 11:37:23 -06:00
parent 7c7f3bd131
commit b37fb8a7cf
3 changed files with 14 additions and 10 deletions
+1 -1
View File
@@ -29,7 +29,7 @@ func latestBlogsHandle(w http.ResponseWriter, r *http.Request) {
} }
func blogHandle(w http.ResponseWriter, r *http.Request, blog string) { func blogHandle(w http.ResponseWriter, r *http.Request, blog string) {
bl, err := blogApp.Blog(r.Context(), blog) bl, err := blogApp.Blog(r.Context(), blog, true)
if err != nil { if err != nil {
if err == backend.ErrNotFound { if err == backend.ErrNotFound {
w.WriteHeader(404) w.WriteHeader(404)
+2 -2
View File
@@ -166,7 +166,7 @@ func editorEdit(w http.ResponseWriter, r *http.Request) {
if blogID == "new" { if blogID == "new" {
bl = &blog.Blog{} bl = &blog.Blog{}
} else { } else {
bl, err = blogApp.Blog(r.Context(), r.URL.Query().Get("blog")) bl, err = blogApp.Blog(r.Context(), r.URL.Query().Get("blog"), false)
if err != nil { if err != nil {
log.Println("error getting blog for editor:", err) log.Println("error getting blog for editor:", err)
sendContent(w, r, "ERROR", "", "") sendContent(w, r, "ERROR", "", "")
@@ -252,7 +252,7 @@ func editorPost(w http.ResponseWriter, r *http.Request) {
sendContent(w, r, "<p>Server error updating blog</p>", "", "") sendContent(w, r, "<p>Server error updating blog</p>", "", "")
return return
} }
old, err := blogApp.Blog(r.Context(), newBlog.ID) old, err := blogApp.Blog(r.Context(), newBlog.ID, false)
if err != nil { if err != nil {
log.Println("error getting old blog to be updated:", err) log.Println("error getting old blog to be updated:", err)
sendContent(w, r, "<p>Updated!</p>", "", "") sendContent(w, r, "<p>Updated!</p>", "", "")
+7 -3
View File
@@ -79,13 +79,17 @@ func (b *BlogApp) GetAuthor(ctx context.Context, blog *Blog) (*Author, error) {
return &author, err return &author, err
} }
func (b *BlogApp) Blog(ctx context.Context, ID string) (*Blog, error) { func (b *BlogApp) Blog(ctx context.Context, ID string, useCache bool) (*Blog, error) {
var blog Blog
if useCache {
b.cacheMutex.RLock() b.cacheMutex.RLock()
blog, has := b.blogCache[ID] var has bool
blog, has = b.blogCache[ID]
b.cacheMutex.RUnlock() b.cacheMutex.RUnlock()
if has { if has {
return &blog, nil return &blog, nil
} }
}
res := b.blogCol.FindOne(ctx, bson.M{"_id": ID, "draft": false}) res := b.blogCol.FindOne(ctx, bson.M{"_id": ID, "draft": false})
if res.Err() != nil { if res.Err() != nil {
if res.Err() == mongo.ErrNoDocuments { if res.Err() == mongo.ErrNoDocuments {
@@ -118,7 +122,7 @@ func (b *BlogApp) reqBlog(w http.ResponseWriter, r *http.Request) {
backend.ReturnError(w, http.StatusBadRequest, "badRequest", "Must provide a blogID") backend.ReturnError(w, http.StatusBadRequest, "badRequest", "Must provide a blogID")
return return
} }
blog, err := b.Blog(r.Context(), blogID) blog, err := b.Blog(r.Context(), blogID, true)
if err != nil { if err != nil {
if err == backend.ErrNotFound { if err == backend.ErrNotFound {
backend.ReturnError(w, http.StatusNotFound, "notFound", "Not blog found with the given ID") backend.ReturnError(w, http.StatusNotFound, "notFound", "Not blog found with the given ID")