Better Output!

This commit is contained in:
eccentric 2023-11-05 02:43:21 +00:00
parent 7ca1c05520
commit 2e8d2fbc95
7 changed files with 32 additions and 13 deletions

View File

@ -10,7 +10,7 @@ import (
func FiberLogger() fiber.Handler { func FiberLogger() fiber.Handler {
return logger.New(logger.Config{ return logger.New(logger.Config{
Format: "${status} ${path}\n", Format: "(${status}) (${latency}) ${path}\n",
}) })
} }

View File

@ -27,13 +27,15 @@ func PrintJSON(v interface{}) {
} }
func PrintTime(label string, functions ...func()) { func PrintTime(label string, functions ...func()) {
current := time.Now()
for _, f := range functions {
f()
}
if Config.Output.Level == "prod" { if Config.Output.Level == "prod" {
return return
} }
current := time.Now()
for _, f := range functions {
f()
}
fmt.Println(label + ":", time.Since(current)) fmt.Println(label + ":", time.Since(current))
} }

View File

@ -25,7 +25,6 @@ func PostProfileAction(c *fiber.Ctx) error {
if person == nil { if person == nil {
return c.Status(404).JSON(aid.ErrorBadRequest("No Account Found")) return c.Status(404).JSON(aid.ErrorBadRequest("No Account Found"))
} }
defer person.Save()
profile := person.GetProfileFromType(c.Query("profileId")) profile := person.GetProfileFromType(c.Query("profileId"))
defer profile.ClearProfileChanges() defer profile.ClearProfileChanges()
@ -79,7 +78,7 @@ func PostMarkItemSeenAction(c *fiber.Ctx, person *p.Person, profile *p.Profile)
} }
item.HasSeen = true item.HasSeen = true
item.Save() go item.Save()
} }
return nil return nil
@ -106,6 +105,9 @@ func PostEquipBattleRoyaleCustomizationAction(c *fiber.Ctx, person *p.Person, pr
if attr == nil { if attr == nil {
return c.Status(400).JSON(aid.ErrorBadRequest("Attribute not found")) return c.Status(400).JSON(aid.ErrorBadRequest("Attribute not found"))
} }
defer func() {
go attr.Save()
}()
switch body.SlotName { switch body.SlotName {
case "Dance": case "Dance":
@ -120,7 +122,5 @@ func PostEquipBattleRoyaleCustomizationAction(c *fiber.Ctx, person *p.Person, pr
attr.ValueJSON = aid.JSONStringify(item.ID) attr.ValueJSON = aid.JSONStringify(item.ID)
} }
attr.Save()
return nil return nil
} }

View File

@ -46,6 +46,12 @@ func init() {
aid.Print("Loaded person: " + person.DisplayName) aid.Print("Loaded person: " + person.DisplayName)
} }
}) })
aid.PrintTime("Loading all persons from cache", func() {
for _, person := range person.AllFromCache() {
aid.Print("Loaded person: " + person.DisplayName)
}
})
} }
func main() { func main() {

View File

@ -1,7 +1,6 @@
package person package person
import ( import (
"fmt"
"sync" "sync"
"time" "time"
) )
@ -45,7 +44,6 @@ func (m *PersonsCache) CacheKiller() {
func (m *PersonsCache) GetPerson(id string) *Person { func (m *PersonsCache) GetPerson(id string) *Person {
if p, ok := m.Load(id); ok { if p, ok := m.Load(id); ok {
fmt.Println("Cache hit", id)
cacheEntry := p.(*CacheEntry) cacheEntry := p.(*CacheEntry)
return cacheEntry.Entry return cacheEntry.Entry
} }

View File

@ -118,7 +118,6 @@ func findHelper(databasePerson *storage.DB_Person) *Person {
func AllFromDatabase() []*Person { func AllFromDatabase() []*Person {
var persons []*Person var persons []*Person
for _, person := range storage.Repo.GetAllPersons() { for _, person := range storage.Repo.GetAllPersons() {
persons = append(persons, Find(person.ID)) persons = append(persons, Find(person.ID))
} }
@ -126,6 +125,20 @@ func AllFromDatabase() []*Person {
return persons return persons
} }
func AllFromCache() []*Person {
if cache == nil {
cache = NewPersonsCacheMutex()
}
var persons []*Person
cache.RangeEntry(func(key string, value *CacheEntry) bool {
persons = append(persons, value.Entry)
return true
})
return persons
}
func (p *Person) GetProfileFromType(profileType string) *Profile { func (p *Person) GetProfileFromType(profileType string) *Profile {
switch profileType { switch profileType {
case "athena": case "athena":

View File

@ -2,7 +2,7 @@
# Snow # Snow
Performance first, universal Fortnite backend written in Go. Performance first, universal Fortnite private server backend written in Go.
## Features ## Features