Skip to content

Commit

Permalink
/ の投稿一覧をきゃっしゅする
Browse files Browse the repository at this point in the history
  • Loading branch information
Gurrium committed Dec 10, 2023
1 parent a8b878f commit a4384d8
Showing 1 changed file with 64 additions and 9 deletions.
73 changes: 64 additions & 9 deletions webapp/go/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,15 @@ func getIndex(w http.ResponseWriter, r *http.Request) {

results := []Post{}

query := `
cachedIndexPosts, err := memcacheClient.Get("posts")
if err == nil {
err := sonnet.Unmarshal(cachedIndexPosts.Value, &results)
if err != nil {
log.Print(err)
return
}
} else if err == memcache.ErrCacheMiss {
query := `
SELECT posts.id, posts.body, posts.mime, posts.created_at,
users.account_name AS "users.account_name", users.authority AS "users.authority"
FROM posts
Expand All @@ -519,15 +527,35 @@ func getIndex(w http.ResponseWriter, r *http.Request) {
LIMIT ?
`

query, args, err := sqlx.In(query, deletedUserIDs, postsPerPage)
if err != nil {
log.Print(err)
return
}
query, args, err := sqlx.In(query, deletedUserIDs, postsPerPage)
if err != nil {
log.Print(err)
return
}

query = db.Rebind(query)
err = db.Select(&results, query, args...)
if err != nil {
query = db.Rebind(query)
err = db.Select(&results, query, args...)
if err != nil {
log.Print(err)
return
}

b, err := sonnet.Marshal(results)
if err != nil {
log.Print(err)
return
}

err = memcacheClient.Set(&memcache.Item{
Key: "posts",
Value: b,
Expiration: 5,
})
if err != nil {
log.Print(err)
return
}
} else {
log.Print(err)
return
}
Expand Down Expand Up @@ -975,6 +1003,8 @@ func postIndex(w http.ResponseWriter, r *http.Request) {
return
}

memcacheClient.Delete("posts")

http.Redirect(w, r, "/posts/"+strconv.FormatInt(pid, 10), http.StatusFound)
}

Expand Down Expand Up @@ -1060,6 +1090,29 @@ func postComment(w http.ResponseWriter, r *http.Request) {
memcacheClient.Delete(fmt.Sprintf("comments_%d_%t", postID, true))
memcacheClient.Delete(fmt.Sprintf("comments_%d_%t", postID, false))

flag := 0
err = db.Get(
&flag,
`
WITH recent_posts AS (
SELECT id FROM posts
ORDER BY created_at DESC
LIMIT 20
)
SELECT COUNT(*) FROM recent_posts
WHERE id = ?
`,
postID,
)
if err != nil {
log.Print(err)
return
}

if flag == 1 {
memcacheClient.Delete("posts")
}

http.Redirect(w, r, fmt.Sprintf("/posts/%d", postID), http.StatusFound)
}

Expand Down Expand Up @@ -1160,6 +1213,8 @@ func postAdminBanned(w http.ResponseWriter, r *http.Request) {
deletedUserIDs = append(deletedUserIDs, i)
}

memcacheClient.Delete("posts")

http.Redirect(w, r, "/admin/banned", http.StatusFound)
}

Expand Down

0 comments on commit a4384d8

Please sign in to comment.