Skip to content
This repository was archived by the owner on Dec 3, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 133 additions & 8 deletions webapp/go/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,42 @@ import "fmt"

var supercache = make(map[string]interface{})

func setCache(key string, value interface{}) {
func setCache(key string, value int64) {
supercache[key] = value
}

func getCache(key string) interface{} {
return supercache[key]
func getCache(key string) int64 {
if supercache[key] == nil {
return 0
}
return supercache[key].(int64)
}

func incrCache(key string) {
currentValue := getCache(key)
if currentValue == nil {
if currentValue == 0 {
setCache(key, 1)
} else {
setCache(key, currentValue.(int64)+1)
setCache(key, currentValue+1)
}
}

func decrCache(key string) {
currentValue := getCache(key)
if currentValue != 0 {
setCache(key, currentValue-1)
}
}

func addCache(key string, value int64) {
currentValue := getCache(key)
if currentValue == nil {
setCache(key, currentValue+value)
}

func updateMaxValueIfNeeded(key string, value int64) {
currentValue := getCache(key)
if currentValue == 0 {
setCache(key, value)
} else {
setCache(key, currentValue.(int64)+value)
}
}

Expand All @@ -35,11 +48,31 @@ type reactionsAndUser struct {
ReactionsCount int64 `db:"reactions_count"`
}

type reactionsAndLivestream struct {
LivestreamID int64 `db:"livestream_id"`
ReactionsCount int64 `db:"reactions_count"`
}

type tipsAndUser struct {
ReacteeUserID int64 `db:"reactee_user_id"`
TipsCount int64 `db:"tips_count"`
}

type tipsAndLivestream struct {
LivestreamID int64 `db:"livestream_id"`
TipsCount int64 `db:"tips_count"`
}

type commentsAndLivestream struct {
LivestreamID int64 `db:"livestream_id"`
CommentsCount int64 `db:"comments_count"`
}

type reportsAndLivesream struct {
LivestreamID int64 `db:"livestream_id"`
ReportsCount int64 `db:"reports_count"`
}

func setupTipsAndReactionsCache() error {
var tipsAndReactions []reactionsAndUser
err := dbConn.Select(
Expand All @@ -50,6 +83,7 @@ func setupTipsAndReactionsCache() error {
count(*) as reactions_count
FROM livecomments
INNER JOIN livestreams ON livestreams.id = livecomments.livestream_id
GROUP BY reactee_user_id
`,
)
if err != nil {
Expand All @@ -69,6 +103,7 @@ func setupTipsAndReactionsCache() error {
sum(livecomments.tip) as tips_count
FROM livecomments
INNER JOIN livestreams ON livestreams.id = livecomments.livestream_id
GROUP BY reactee_user_id
`,
)
if err != nil {
Expand All @@ -78,5 +113,95 @@ func setupTipsAndReactionsCache() error {
addCache(fmt.Sprintf("user:%d:tips", tipsAndUser.ReacteeUserID), tipsAndUser.TipsCount)
}

var tipsAndLivestreams []tipsAndLivestream
err = dbConn.Select(
&tipsAndLivestreams,
`
SELECT
livestream_id,
IFNULL(MAX(tip), 0) as tips_count
FROM livecomments
GROUP BY livestream_id
`,
)
if err != nil {
return fmt.Errorf("failed to aggregate tips: %w", err)
}
for _, tipsAndLivestream := range tipsAndLivestreams {
updateMaxValueIfNeeded(fmt.Sprintf("livestream:%d:maxTip", tipsAndLivestream.LivestreamID), tipsAndLivestream.TipsCount)
}

var totalTipsAndLivestreams []tipsAndLivestream
err = dbConn.Select(
&totalTipsAndLivestreams,
`
SELECT
livestream_id,
SUM(tip) as tips_count
FROM livecomments
GROUP BY livestream_id
`,
)
if err != nil {
return fmt.Errorf("failed to aggregate tips: %w", err)
}
for _, tipsAndLivestream := range totalTipsAndLivestreams {
setCache(fmt.Sprintf("livestream:%d:tips", tipsAndLivestream.LivestreamID), tipsAndLivestream.TipsCount)
}

var reactionsAndLivestreams []reactionsAndLivestream
err = dbConn.Select(
&reactionsAndLivestreams,
`
SELECT
livestream_id,
count(*) as reactions_count
FROM reactions
GROUP BY livestream_id
`,
)
if err != nil {
return fmt.Errorf("failed to aggregate reactions: %w", err)
}
for _, reactionsAndLivestream := range reactionsAndLivestreams {
setCache(fmt.Sprintf("livestream:%d:reactions", reactionsAndLivestream.LivestreamID), reactionsAndLivestream.ReactionsCount)
}

var commentsAndLivestreams []commentsAndLivestream
err = dbConn.Select(
&commentsAndLivestreams,
`
SELECT
livestream_id,
count(*) as comments_count
FROM livecomments
GROUP BY livestream_id
`,
)
if err != nil {
return fmt.Errorf("failed to aggregate comments: %w", err)
}
for _, commentsAndLivestream := range commentsAndLivestreams {
setCache(fmt.Sprintf("livestream:%d:comments", commentsAndLivestream.LivestreamID), commentsAndLivestream.CommentsCount)
}

var reportsAndLivestreams []reportsAndLivesream
err = dbConn.Select(
&reportsAndLivestreams,
`
SELECT
livestream_id,
count(*) as reports_count
FROM livecomment_reports
GROUP BY livestream_id
`,
)
if err != nil {
return fmt.Errorf("failed to aggregate reports: %w", err)
}
for _, reportsAndLivestream := range reportsAndLivestreams {
setCache(fmt.Sprintf("livestream:%d:reports", reportsAndLivestream.LivestreamID), reportsAndLivestream.ReportsCount)
}

return nil
}
79 changes: 79 additions & 0 deletions webapp/go/livecomment_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,10 @@ func postLivecommentHandler(c echo.Context) error {
}

addCache(fmt.Sprintf("user:%d:tips", livestreamModel.UserID), req.Tip)
incrCache(fmt.Sprintf("user:%d:comments", livestreamModel.UserID))
addCache(fmt.Sprintf("livestream:%d:tips", livestreamModel.ID), req.Tip)
incrCache(fmt.Sprintf("livestream:%d:comments", livestreamModel.ID))
updateMaxValueIfNeeded(fmt.Sprintf("livestream:%d:maxTip", livestreamModel.ID), req.Tip)

if err := tx.Commit(); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "failed to commit: "+err.Error())
Expand Down Expand Up @@ -326,6 +330,8 @@ func reportLivecommentHandler(c echo.Context) error {
return echo.NewHTTPError(http.StatusInternalServerError, "failed to commit: "+err.Error())
}

incrCache(fmt.Sprintf("livestream:%d:reports", livestreamModel.ID))

return c.JSON(http.StatusCreated, report)
}

Expand Down Expand Up @@ -415,6 +421,79 @@ func moderateHandler(c echo.Context) error {
}
}

var tipsAndUsers []tipsAndUser
err = dbConn.Select(
&tipsAndUsers,
`
SELECT
livestreams.user_id as reactee_user_id,
sum(livecomments.tip) as tips_count
FROM livecomments
INNER JOIN livestreams ON livestreams.id = livecomments.livestream_id
GROUP BY reactee_user_id
`,
)
if err != nil {
return fmt.Errorf("failed to aggregate tips: %w", err)
}
for _, tipsAndUser := range tipsAndUsers {
addCache(fmt.Sprintf("user:%d:tips", tipsAndUser.ReacteeUserID), tipsAndUser.TipsCount)
}

var tipsAndLivestreams []tipsAndLivestream
err = dbConn.Select(
&tipsAndLivestreams,
`
SELECT
livestream_id,
IFNULL(MAX(tip), 0) as tips_count
FROM livecomments
GROUP BY livestream_id
`,
)
if err != nil {
return fmt.Errorf("failed to aggregate tips: %w", err)
}
for _, tipsAndLivestream := range tipsAndLivestreams {
updateMaxValueIfNeeded(fmt.Sprintf("livestream:%d:maxTip", tipsAndLivestream.LivestreamID), tipsAndLivestream.TipsCount)
}

var totalTipsAndLivestreams []tipsAndLivestream
err = dbConn.Select(
&totalTipsAndLivestreams,
`
SELECT
livestream_id,
SUM(tip) as tips_count
FROM livecomments
GROUP BY livestream_id
`,
)
if err != nil {
return fmt.Errorf("failed to aggregate tips: %w", err)
}
for _, tipsAndLivestream := range totalTipsAndLivestreams {
setCache(fmt.Sprintf("livestream:%d:tips", tipsAndLivestream.LivestreamID), tipsAndLivestream.TipsCount)
}

var commentsAndLivestreams []commentsAndLivestream
err = dbConn.Select(
&commentsAndLivestreams,
`
SELECT
livestream_id,
count(*) as comments_count
FROM livecomments
GROUP BY livestream_id
`,
)
if err != nil {
return fmt.Errorf("failed to aggregate comments: %w", err)
}
for _, commentsAndLivestream := range commentsAndLivestreams {
setCache(fmt.Sprintf("livestream:%d:comments", commentsAndLivestream.LivestreamID), commentsAndLivestream.CommentsCount)
}

if err := tx.Commit(); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "failed to commit: "+err.Error())
}
Expand Down
Loading