Added cache to leaderboard

pull/23/head
Frederico Santos 6 months ago
parent b113ffceee
commit 0f543a10e5

@ -18,9 +18,11 @@
package db
import (
"fmt"
"math"
"github.com/pagefaultgames/rogueserver/defs"
cache "github.com/pagefaultgames/rogueserver/shared"
)
func TryAddDailyRun(seed string) (string, error) {
@ -54,7 +56,11 @@ func AddOrUpdateAccountDailyRun(uuid []byte, score int, wave int) error {
func FetchRankings(category int, page int) ([]defs.DailyRanking, error) {
var rankings []defs.DailyRanking
cacheKey := fmt.Sprintf("dailyRankings:%d:%d", category, page)
if cachedRankings, found := cache.Get(cacheKey); found {
return cachedRankings.([]defs.DailyRanking), nil
}
offset := (page - 1) * 10
var query string
@ -82,10 +88,17 @@ func FetchRankings(category int, page int) ([]defs.DailyRanking, error) {
rankings = append(rankings, ranking)
}
cache.Set(cacheKey, rankings)
return rankings, nil
}
func FetchRankingPageCount(category int) (int, error) {
cacheKey := fmt.Sprintf("dailyRankingsPageCount:%d", category)
if cachedPageCount, found := cache.Get(cacheKey); found {
return cachedPageCount.(int), nil
}
var query string
switch category {
case 0:
@ -99,6 +112,7 @@ func FetchRankingPageCount(category int) (int, error) {
if err != nil {
return 0, err
}
return int(math.Ceil(float64(recordCount) / 10)), nil
pageCount := int(math.Ceil(float64(recordCount) / 10))
cache.Set(cacheKey, pageCount)
return pageCount, nil
}

@ -4,6 +4,7 @@ go 1.22
require (
github.com/go-sql-driver/mysql v1.7.1
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/robfig/cron/v3 v3.0.1
golang.org/x/crypto v0.16.0
)

@ -1,5 +1,7 @@
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/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
golang.org/x/crypto v0.16.0 h1:mMMrFzRSCF0GvB7Ne27XVtVAaXLrPmgPC7/v0tkwHaY=

@ -0,0 +1,39 @@
/*
Copyright (C) 2024 Pagefault Games
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cache
import (
"time"
"github.com/patrickmn/go-cache"
)
var c *cache.Cache
func init() {
// Create a new cache with a default expiration time of 5 minutes, and which purges expired items every 10 minutes
c = cache.New(5*time.Minute, 10*time.Minute)
}
func Set(key string, value interface{}) {
c.Set(key, value, cache.DefaultExpiration)
}
func Get(key string) (interface{}, bool) {
return c.Get(key)
}
Loading…
Cancel
Save