Skip to content

Commit

Permalink
memcache -> freecache
Browse files Browse the repository at this point in the history
  • Loading branch information
Gurrium committed Dec 14, 2023
1 parent e2c6758 commit b7d5b21
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 73 deletions.
128 changes: 56 additions & 72 deletions webapp/go/app.go
Expand Up @@ -17,8 +17,9 @@ import (
"strings"
"time"

"github.com/bradfitz/gomemcache/memcache"
m "github.com/bradfitz/gomemcache/memcache"
gsm "github.com/bradleypeabody/gorilla-sessions-memcache"
"github.com/coocood/freecache"
"github.com/go-chi/chi/v5"
_ "github.com/go-sql-driver/mysql"
"github.com/gorilla/sessions"
Expand All @@ -32,8 +33,8 @@ import (
var (
db *sqlx.DB
store *gsm.MemcacheStore
memcacheClient *memcache.Client
deletedUserIDs []int
cache *freecache.Cache
)

const (
Expand Down Expand Up @@ -78,8 +79,9 @@ func init() {
if memdAddr == "" {
memdAddr = "localhost:11211"
}
memcacheClient = memcache.New(memdAddr)
memcacheClient := m.New(memdAddr)
store = gsm.NewMemcacheStore(memcacheClient, "iscogram_", []byte("sendagaya"))
cache = freecache.NewCache(10 * 1024 * 1024)
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
}

Expand Down Expand Up @@ -193,40 +195,32 @@ type SimpleComment struct {
func makePosts(results []Post, csrfToken string, allComments bool) ([]Post, error) {
var posts []Post

cachedCommentCountKeysMap := make(map[int]string, len(results))
cachedCommentKeysMap := make(map[int]string, len(results))
cachedCommentCountKeys := make([]string, 0, len(results))
cachedCommentKeys := make([]string, 0, len(results))
postIDs := make([]int, 0, len(results))
cachedCommentCountKeysMap := make(map[int][]byte, len(results))
cachedCommentKeysMap := make(map[int][]byte, len(results))

for _, p := range results {
cachedCommentCountKeys = append(cachedCommentCountKeys, fmt.Sprintf("comment_count_%d", p.ID))
cachedCommentCountKeysMap[p.ID] = cachedCommentCountKeys[len(cachedCommentCountKeys)-1]

cachedCommentKeys = append(cachedCommentKeys, fmt.Sprintf("comments_%d_%t", p.ID, !allComments))
cachedCommentKeysMap[p.ID] = cachedCommentKeys[len(cachedCommentKeys)-1]

postIDs = append(postIDs, p.ID)
cachedCommentCountKeysMap[p.ID] = []byte(fmt.Sprintf("comment_count_%d", p.ID))
cachedCommentKeysMap[p.ID] = []byte(fmt.Sprintf("comments_%d_%t", p.ID, !allComments))
}

commentCounts := make(map[int]int, len(results))
var missCachedCommentCountPostIDs []int
cachedCommentCounts, err := memcacheClient.GetMulti(cachedCommentCountKeys)
if err == nil {
for _, p := range results {
cachedCommentCount, ok := cachedCommentCounts[cachedCommentCountKeysMap[p.ID]]
if ok {
commentCount, _ := strconv.Atoi(string(cachedCommentCount.Value))

commentCounts[p.ID] = commentCount
} else {
missCachedCommentCountPostIDs = append(missCachedCommentCountPostIDs, p.ID)
}
for _, p := range results {
key, ok := cachedCommentCountKeysMap[p.ID]
if !ok {
continue
}

value, err := cache.Get(key)
if err == nil {
commentCount, _ := strconv.Atoi(string(value))

commentCounts[p.ID] = commentCount
} else if err == freecache.ErrNotFound {
missCachedCommentCountPostIDs = append(missCachedCommentCountPostIDs, p.ID)
} else {
return nil, err
}
} else if err == memcache.ErrCacheMiss {
missCachedCommentCountPostIDs = postIDs
} else {
return nil, err
}

if len(missCachedCommentCountPostIDs) > 0 {
Expand All @@ -251,11 +245,7 @@ func makePosts(results []Post, csrfToken string, allComments bool) ([]Post, erro
for _, count := range counts {
commentCounts[count.PostID] = count.CommentCount

err := memcacheClient.Set(&memcache.Item{
Key: cachedCommentCountKeysMap[count.PostID],
Value: []byte(strconv.Itoa(count.CommentCount)),
Expiration: 10,
})
err := cache.Set([]byte(cachedCommentCountKeysMap[count.PostID]), []byte(strconv.Itoa(count.CommentCount)), 10)
if err != nil {
return nil, err
}
Expand All @@ -264,26 +254,26 @@ func makePosts(results []Post, csrfToken string, allComments bool) ([]Post, erro

comments := make(map[int][]SimpleComment, len(results))
var missCachedCommentsPostIDs []int
cachedComments, err := memcacheClient.GetMulti(cachedCommentKeys)
if err == nil {
for _, p := range results {
cachedComment, ok := cachedComments[cachedCommentKeysMap[p.ID]]
if ok {
var cs []SimpleComment
err := sonnet.Unmarshal(cachedComment.Value, &cs)
if err != nil {
return nil, err
}

comments[p.ID] = cs
} else {
missCachedCommentsPostIDs = append(missCachedCommentsPostIDs, p.ID)
for _, p := range results {
key, ok := cachedCommentKeysMap[p.ID]
if !ok {
continue
}

value, err := cache.Get(key)
if err == nil {
var cs []SimpleComment
err := sonnet.Unmarshal(value, &cs)
if err != nil {
return nil, err
}

comments[p.ID] = cs
} else if err == freecache.ErrNotFound {
missCachedCommentsPostIDs = append(missCachedCommentsPostIDs, p.ID)
} else {
return nil, err
}
} else if err == memcache.ErrCacheMiss {
missCachedCommentsPostIDs = postIDs
} else {
return nil, err
}

if len(missCachedCommentsPostIDs) > 0 {
Expand Down Expand Up @@ -332,11 +322,7 @@ func makePosts(results []Post, csrfToken string, allComments bool) ([]Post, erro
return nil, err
}

err = memcacheClient.Set(&memcache.Item{
Key: cachedCommentKeysMap[postID],
Value: b,
Expiration: 10,
})
err = cache.Set(cachedCommentKeysMap[postID], b, 10)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -530,19 +516,21 @@ func getLogout(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/", http.StatusFound)
}

var postsKey = []byte("posts")

func getIndex(w http.ResponseWriter, r *http.Request) {
me := getSessionUser(r)

results := []Post{}

cachedIndexPosts, err := memcacheClient.Get("posts")
cachedIndexPosts, err := cache.Get(postsKey)
if err == nil {
err := sonnet.Unmarshal(cachedIndexPosts.Value, &results)
err := sonnet.Unmarshal(cachedIndexPosts, &results)
if err != nil {
log.Print(err)
return
}
} else if err == memcache.ErrCacheMiss {
} else if err == freecache.ErrNotFound {
query := `
SELECT posts.id, posts.body, posts.mime, posts.created_at,
users.account_name AS "users.account_name", users.authority AS "users.authority"
Expand Down Expand Up @@ -572,11 +560,7 @@ func getIndex(w http.ResponseWriter, r *http.Request) {
return
}

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

memcacheClient.Delete("posts")
cache.Del(postsKey)

http.Redirect(w, r, "/posts/"+strconv.FormatInt(pid, 10), http.StatusFound)
}
Expand Down Expand Up @@ -1212,9 +1196,9 @@ func postComment(w http.ResponseWriter, r *http.Request) {
return
}

memcacheClient.Delete(fmt.Sprintf("comment_count_%d", postID))
memcacheClient.Delete(fmt.Sprintf("comments_%d_%t", postID, true))
memcacheClient.Delete(fmt.Sprintf("comments_%d_%t", postID, false))
cache.Del([]byte(fmt.Sprintf("comment_count_%d", postID)))
cache.Del([]byte(fmt.Sprintf("comments_%d_%t", postID, true)))
cache.Del([]byte(fmt.Sprintf("comments_%d_%t", postID, false)))

flag := 0
err = db.Get(
Expand All @@ -1236,7 +1220,7 @@ func postComment(w http.ResponseWriter, r *http.Request) {
}

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

http.Redirect(w, r, fmt.Sprintf("/posts/%d", postID), http.StatusFound)
Expand Down Expand Up @@ -1339,7 +1323,7 @@ func postAdminBanned(w http.ResponseWriter, r *http.Request) {
deletedUserIDs = append(deletedUserIDs, i)
}

memcacheClient.Delete("posts")
cache.Del(postsKey)

http.Redirect(w, r, "/admin/banned", http.StatusFound)
}
Expand Down
6 changes: 5 additions & 1 deletion webapp/go/go.mod
@@ -1,10 +1,13 @@
module github.com/catatsuy/private-isu/webapp/golang

go 1.19
go 1.21.0

toolchain go1.21.1

require (
github.com/bradfitz/gomemcache v0.0.0-20230905024940-24af94b03874
github.com/bradleypeabody/gorilla-sessions-memcache v0.0.0-20181103040241-659414f458e1
github.com/coocood/freecache v1.2.4
github.com/go-chi/chi/v5 v5.0.10
github.com/go-sql-driver/mysql v1.7.1
github.com/gorilla/sessions v1.2.2
Expand All @@ -13,6 +16,7 @@ require (
)

require (
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/gorilla/securecookie v1.1.2 // indirect
github.com/memcachier/mc v2.0.1+incompatible // indirect
)
7 changes: 7 additions & 0 deletions webapp/go/go.sum
Expand Up @@ -2,12 +2,17 @@ github.com/bradfitz/gomemcache v0.0.0-20230905024940-24af94b03874 h1:N7oVaKyGp8b
github.com/bradfitz/gomemcache v0.0.0-20230905024940-24af94b03874/go.mod h1:r5xuitiExdLAJ09PR7vBVENGvp4ZuTBeWTGtxuX3K+c=
github.com/bradleypeabody/gorilla-sessions-memcache v0.0.0-20181103040241-659414f458e1 h1:4QHxgr7hM4gVD8uOwrk8T1fjkKRLwaLjmTkU0ibhZKU=
github.com/bradleypeabody/gorilla-sessions-memcache v0.0.0-20181103040241-659414f458e1/go.mod h1:dkChI7Tbtx7H1Tj7TqGSZMOeGpMP5gLHtjroHd4agiI=
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/coocood/freecache v1.2.4 h1:UdR6Yz/X1HW4fZOuH0Z94KwG851GWOSknua5VUbb/5M=
github.com/coocood/freecache v1.2.4/go.mod h1:RBUWa/Cy+OHdfTGFEhEuE1pMCMX51Ncizj7rthiQ3vk=
github.com/go-chi/chi/v5 v5.0.10 h1:rLz5avzKpjqxrYwXNfmjkrYYXOyLJd37pz53UFHC6vk=
github.com/go-chi/chi/v5 v5.0.10/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI=
github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0=
github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/gorilla/securecookie v1.1.2 h1:YCIWL56dvtr73r6715mJs5ZvhtnY73hBvEF8kXD8ePA=
github.com/gorilla/securecookie v1.1.2/go.mod h1:NfCASbcHqRSY+3a8tlWJwsQap2VX5pwzwo4h3eOamfo=
github.com/gorilla/sessions v1.2.2 h1:lqzMYz6bOfvn2WriPUjNByzeXIlVzURcPmgMczkmTjY=
Expand All @@ -20,3 +25,5 @@ github.com/mattn/go-sqlite3 v1.14.6 h1:dNPt6NO46WmLVt2DLNpwczCmdV5boIZ6g/tlDrlRU
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/memcachier/mc v2.0.1+incompatible h1:s8EDz0xrJLP8goitwZOoq1vA/sm0fPS4X3KAF0nyhWQ=
github.com/memcachier/mc v2.0.1+incompatible/go.mod h1:7bkvFE61leUBvXz+yxsOnGBQSZpBSPIMUQSmmSHvuXc=
github.com/sugawarayuuta/sonnet v0.0.0-20231004000330-239c7b6e4ce8 h1:u+kxnRXxx+0O5SiefP3oTt4jeeIx+rYf1jkdW2qd2Ss=
github.com/sugawarayuuta/sonnet v0.0.0-20231004000330-239c7b6e4ce8/go.mod h1:6M53rd6DvbzoLbFnL3bjCsDSkCYh4i2yqW04hxr1/5o=

0 comments on commit b7d5b21

Please sign in to comment.