From b7d5b21f5526e734e3886e639011a3b8b6fec43e Mon Sep 17 00:00:00 2001 From: gurrium Date: Thu, 14 Dec 2023 22:51:40 +0900 Subject: [PATCH] memcache -> freecache --- webapp/go/app.go | 128 +++++++++++++++++++++-------------------------- webapp/go/go.mod | 6 ++- webapp/go/go.sum | 7 +++ 3 files changed, 68 insertions(+), 73 deletions(-) diff --git a/webapp/go/app.go b/webapp/go/app.go index 7b7c93054..2579dec5c 100644 --- a/webapp/go/app.go +++ b/webapp/go/app.go @@ -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" @@ -32,8 +33,8 @@ import ( var ( db *sqlx.DB store *gsm.MemcacheStore - memcacheClient *memcache.Client deletedUserIDs []int + cache *freecache.Cache ) const ( @@ -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) } @@ -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 { @@ -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 } @@ -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 { @@ -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 } @@ -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" @@ -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 @@ -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) } @@ -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( @@ -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) @@ -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) } diff --git a/webapp/go/go.mod b/webapp/go/go.mod index d8f2cbab3..dc881b3b0 100644 --- a/webapp/go/go.mod +++ b/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 @@ -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 ) diff --git a/webapp/go/go.sum b/webapp/go/go.sum index 21096fab9..639a6b64e 100644 --- a/webapp/go/go.sum +++ b/webapp/go/go.sum @@ -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= @@ -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=