Skip to content

Commit

Permalink
themeをgoキャッシュ 21153
Browse files Browse the repository at this point in the history
  • Loading branch information
シン・ウー馬場ーイー222 committed Nov 25, 2023
1 parent a2da90b commit 78cbad6
Show file tree
Hide file tree
Showing 6 changed files with 382 additions and 341 deletions.
18 changes: 18 additions & 0 deletions home/isucon/webapp/go/main.go
Expand Up @@ -116,6 +116,15 @@ func connectDB(logger echo.Logger) (*sqlx.DB, error) {
}
rows.Close()

var themeModels []ThemeModel
if err := db.Select(&themeModels, "SELECT * FROM themes"); err != nil {
return nil, err
}
for _, t := range themeModels {
t := t
themeMap.Store(t.UserID, t)
}

if err := db.Ping(); err != nil {
return nil, err
}
Expand All @@ -129,6 +138,15 @@ func initializeHandler(c echo.Context) error {
return echo.NewHTTPError(http.StatusInternalServerError, "failed to initialize: "+err.Error())
}
hashMap = sync.Map{}
themeMap = sync.Map{}
var themeModels []ThemeModel
if err := dbConn.Select(&themeModels, "SELECT * FROM themes"); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "failed to initialize themeMap: "+err.Error())
}
for _, t := range themeModels {
t := t
themeMap.Store(t.UserID, t)
}

c.Request().Header.Add("Content-Type", "application/json;charset=utf-8")
return c.JSON(http.StatusOK, InitializeResponse{
Expand Down
12 changes: 10 additions & 2 deletions home/isucon/webapp/go/top_handler.go
Expand Up @@ -73,10 +73,18 @@ func getStreamerThemeHandler(c echo.Context) error {
return echo.NewHTTPError(http.StatusInternalServerError, "failed to get user: "+err.Error())
}

themeModel := ThemeModel{}
if err := tx.GetContext(ctx, &themeModel, "SELECT * FROM themes WHERE user_id = ?", userModel.ID); err != nil {
t, ok := themeMap.Load(userModel.ID)
if !ok {
return echo.NewHTTPError(http.StatusInternalServerError, "failed to get user theme: "+err.Error())
}
themeModel := t.(ThemeModel)

/*
themeModel := ThemeModel{}
if err := tx.GetContext(ctx, &themeModel, "SELECT * FROM themes WHERE user_id = ?", userModel.ID); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "failed to get user theme: "+err.Error())
}
*/

if err := tx.Commit(); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "failed to commit: "+err.Error())
Expand Down
16 changes: 13 additions & 3 deletions home/isucon/webapp/go/user_handler.go
Expand Up @@ -87,6 +87,7 @@ type PostIconResponse struct {
}

var hashMap sync.Map
var themeMap sync.Map

func getIconHandler(c echo.Context) error {
ctx := c.Request().Context()
Expand Down Expand Up @@ -289,6 +290,7 @@ func registerHandler(c echo.Context) error {
if _, err := tx.NamedExecContext(ctx, "INSERT INTO themes (user_id, dark_mode) VALUES(:user_id, :dark_mode)", themeModel); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "failed to insert user theme: "+err.Error())
}
themeMap.Store(userID, themeModel)

if out, err := exec.Command("pdnsutil", "add-record", "u.isucon.dev", req.Name, "A", "60", powerDNSSubdomainAddress).CombinedOutput(); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, string(out)+": "+err.Error())
Expand Down Expand Up @@ -433,10 +435,18 @@ func verifyUserSession(c echo.Context) error {
}

func fillUserResponse(ctx context.Context, tx *sqlx.Tx, userModel UserModel) (User, error) {
themeModel := ThemeModel{}
if err := tx.GetContext(ctx, &themeModel, "SELECT * FROM themes WHERE user_id = ?", userModel.ID); err != nil {
return User{}, err
t, ok := themeMap.Load(userModel.ID)
if !ok {
return User{}, fmt.Errorf("no theme")
}
themeModel := t.(ThemeModel)

/*
themeModel := ThemeModel{}
if err := tx.GetContext(ctx, &themeModel, "SELECT * FROM themes WHERE user_id = ?", userModel.ID); err != nil {
return User{}, err
}
*/

v, ok := hashMap.Load(userModel.ID)
var iconHash string
Expand Down
289 changes: 147 additions & 142 deletions result/db.log

Large diffs are not rendered by default.

24 changes: 12 additions & 12 deletions result/mysqltuner.log
Expand Up @@ -54,7 +54,7 @@
[OK] NO SECURITY CVE FOUND FOR YOUR VERSION

-------- Performance Metrics -----------------------------------------------------------------------
[--] Up for: 1h 9m 48s (2M q [610.484 qps], 12K conn, TX: 883M, RX: 600M)
[--] Up for: 1h 28m 51s (2M q [550.635 qps], 14K conn, TX: 1G, RX: 689M)
[--] Reads / Writes: 95% / 5%
[--] Binary logging is disabled
[--] Physical Memory : 3.6G
Expand All @@ -66,18 +66,18 @@
[OK] Maximum reached memory usage: 232.3M (6.28% of installed RAM)
[OK] Maximum possible memory usage: 459.1M (12.42% of installed RAM)
[OK] Overall possible memory usage with other process is compatible with memory available
[!!] Slow queries: 115% (2M/2M)
[!!] Slow queries: 115% (3M/2M)
[OK] Highest usage of available connections: 19% (30/151)
[OK] Aborted connections: 0.01% (1/12444)
[OK] Aborted connections: 0.03% (4/14209)
[!!] name resolution is active : a reverse name resolution is made for each new connection and can reduce performance
[--] Query cache have been removed in MySQL 8
[OK] Sorts requiring temporary tables: 0% (26 temp sorts / 12K sorts)
[!!] Joins performed without indexes: 92
[OK] Temporary tables created on disk: 0% (0 on disk / 247K total)
[OK] Thread cache hit rate: 99% (114 created / 12K connections)
[OK] Table cache hit rate: 40% (1K open / 2K opened)
[OK] Sorts requiring temporary tables: 0% (28 temp sorts / 15K sorts)
[!!] Joins performed without indexes: 121
[OK] Temporary tables created on disk: 0% (0 on disk / 308K total)
[OK] Thread cache hit rate: 99% (127 created / 14K connections)
[OK] Table cache hit rate: 39% (1K open / 3K opened)
[OK] Open file limit used: 0% (1/10K)
[OK] Table locks acquired immediately: 100% (20 immediate / 20 locks)
[OK] Table locks acquired immediately: 100% (26 immediate / 26 locks)

-------- Performance schema ------------------------------------------------------------------------
[--] Memory used by P_S: 72B
Expand All @@ -98,9 +98,9 @@
[OK] InnoDB buffer pool instances: 1
[--] Number of InnoDB Buffer Pool Chunk : 1 for 1 Buffer Pool Instance(s)
[OK] Innodb_buffer_pool_size aligned with Innodb_buffer_pool_chunk_size & Innodb_buffer_pool_instances
[OK] InnoDB Read buffer efficiency: 100.00% (119143234 hits/ 119146125 total)
[!!] InnoDB Write Log efficiency: 89.66% (1692821 hits/ 1888109 total)
[OK] InnoDB log waits: 0.00% (0 waits / 195288 writes)
[OK] InnoDB Read buffer efficiency: 100.00% (138709331 hits/ 138712308 total)
[OK] InnoDB Write log efficiency: 90.10% (2098889 hits/ 2329425 total)
[OK] InnoDB log waits: 0.00% (0 waits / 230536 writes)

-------- AriaDB Metrics ----------------------------------------------------------------------------
[--] AriaDB is disabled.
Expand Down

0 comments on commit 78cbad6

Please sign in to comment.