DataCache and changes for game update
Recent game update changed pokememes view in pokedeks, so we need to reflect it by updating parser. Introducing DataCache - a silver bullet for eliminating lags linked to database queries. Less queries, more in RAM, faster work. Needs testing in production environment.
This commit is contained in:
45
lib/datacache/locations.go
Normal file
45
lib/datacache/locations.go
Normal file
@@ -0,0 +1,45 @@
|
||||
// i2_bot – Instinct PokememBro Bot
|
||||
// Copyright (c) 2018 Vladimir "fat0troll" Hodakov
|
||||
|
||||
package datacache
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"git.wtfteam.pro/fat0troll/i2_bot/lib/dbmapping"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func (dc *DataCache) initLocations() {
|
||||
c.Log.Info("Initializing Locations storage...")
|
||||
dc.locations = make(map[int]*dbmapping.Location)
|
||||
}
|
||||
|
||||
func (dc *DataCache) loadLocations() {
|
||||
c.Log.Info("Load current Locations data from database to DataCache...")
|
||||
locations := []dbmapping.Location{}
|
||||
err := c.Db.Select(&locations, "SELECT * FROM locations")
|
||||
if err != nil {
|
||||
// This is critical error and we need to stop immediately!
|
||||
c.Log.Fatal(err.Error())
|
||||
}
|
||||
|
||||
dc.locationsMutex.Lock()
|
||||
for i := range locations {
|
||||
dc.locations[locations[i].ID] = &locations[i]
|
||||
}
|
||||
c.Log.Info("Loaded locations in DataCache: " + strconv.Itoa(len(dc.locations)))
|
||||
dc.locationsMutex.Unlock()
|
||||
}
|
||||
|
||||
func (dc *DataCache) findLocationIDByName(name string) (int, error) {
|
||||
dc.locationsMutex.Lock()
|
||||
for i := range dc.locations {
|
||||
if dc.locations[i].Name == name {
|
||||
dc.locationsMutex.Unlock()
|
||||
return i, nil
|
||||
}
|
||||
}
|
||||
|
||||
dc.locationsMutex.Unlock()
|
||||
return 0, errors.New("There is no location with name = " + name)
|
||||
}
|
||||
Reference in New Issue
Block a user