Add redis. Prep for new save file

This commit is contained in:
pakintada@gmail.com 2024-02-05 17:07:04 +07:00
parent 6c22be7d7c
commit 0604a3b77f
5 changed files with 295 additions and 87 deletions

View file

@ -27,10 +27,11 @@ type Data struct {
CurrentCountryID map[string]string
DefaultCountryMap []DefaultByCountry
AllRecipeFiles map[string][]helpers.RecipePath
currentRecipe map[string]*models.Recipe
CurrentRecipe map[string]*models.Recipe
recipeMap map[string]RecipeWithTimeStamps
Countries []helpers.CountryName
taoLogger *logger.TaoLogger
redisClient *RedisCli
}
type DefaultByCountry struct {
@ -53,7 +54,7 @@ var (
}
)
func NewData(taoLogger *logger.TaoLogger) *Data {
func NewData(taoLogger *logger.TaoLogger, redisClient *RedisCli) *Data {
allRecipeFiles := helpers.ScanRecipeFiles(countries)
@ -131,6 +132,8 @@ func NewData(taoLogger *logger.TaoLogger) *Data {
log.Panic("Error when read default recipe file for each country:", v.CountryShortName, err)
}
redisClient.SetToKey(v2.Name, defaultRecipe)
currentDefaultFileForEachCountry[v.CountryShortName] = defaultRecipe
break
}
@ -169,7 +172,7 @@ func NewData(taoLogger *logger.TaoLogger) *Data {
CurrentFile: currentFileMap,
CurrentCountryID: CurrentCountryIDMap,
AllRecipeFiles: allRecipeFiles,
currentRecipe: currentDefaultFileForEachCountry,
CurrentRecipe: currentDefaultFileForEachCountry,
recipeMap: map[string]RecipeWithTimeStamps{
defaultFile: {
Recipe: currentDefaultFileForEachCountry,
@ -179,6 +182,7 @@ func NewData(taoLogger *logger.TaoLogger) *Data {
Countries: countries,
taoLogger: taoLogger,
DefaultCountryMap: defaultForEachCountry,
redisClient: redisClient,
}
}
@ -189,17 +193,47 @@ func (d *Data) GetRecipe(countryID, filename string) *models.Recipe {
// TODO: concat submenu into recipe
if countryID == "" {
return d.currentRecipe["tha"]
d.taoLogger.Log.Debug("GetRecipe", zap.Any("EmptyCountryId", "return default country = tha"))
return d.CurrentRecipe["tha"]
}
if filename == "" || filename == d.CurrentFile[countryID] {
return d.currentRecipe[countryID]
if filename == "" {
d.taoLogger.Log.Debug("GetRecipe", zap.Any("EmptyFilename", filename))
return d.CurrentRecipe[countryID]
}
if recipe, ok := d.recipeMap[filename]; ok {
// do check if match the current pointer
if d.CurrentFile[countryID] == filename {
d.taoLogger.Log.Debug("GetRecipe", zap.Any("FileMatchCurrent", "return from stored "+filename), zap.Any("CurrentFile", d.CurrentFile))
// make sure recipe vesion is equal
currentConfig := d.CurrentRecipe[countryID].MachineSetting.ConfigNumber
// get requested version
requestedConfig, _ := strconv.Atoi(strings.Split(strings.Split(filename, "_")[1], ".")[0])
if currentConfig != requestedConfig {
d.taoLogger.Log.Debug("GetRecipe", zap.Any("ActualFileNotMatch", "Skip this!"))
} else {
// if equal, OK
return d.CurrentRecipe[countryID]
}
}
if recipe, ok := d.recipeMap[filename]; ok && d.redisClient.HealthCheck() != nil {
d.taoLogger.Log.Debug("GetRecipe", zap.Any("ValidOnStored", "return from stored "+filename))
d.CurrentFile[countryID] = filename
// d.CurrentCountryID[countryID] = countryID
return recipe.Recipe[countryID]
// make sure recipe vesion is equal
currentConfig := d.CurrentRecipe[countryID].MachineSetting.ConfigNumber
// get requested version
requestedConfig, _ := strconv.Atoi(strings.Split(strings.Split(filename, "_")[1], ".")[0])
if currentConfig != requestedConfig {
d.taoLogger.Log.Debug("GetRecipe", zap.Any("InvalidOnStored", "Skip this!"))
} else {
// if equal, OK
return recipe.Recipe[countryID]
}
}
// change current version and read new recipe
@ -208,17 +242,49 @@ func (d *Data) GetRecipe(countryID, filename string) *models.Recipe {
filename = d.CurrentFile[countryID]
}
// d.CurrentFile[countryID] = filename
d.taoLogger.Log.Debug("GetRecipe", zap.String("filename", filename), zap.String("countryID", countryID))
// d.CurrentCountryID[countryID] = countryID
// var recipe *models.Recipe = &models.Recipe{}
// do check if redis contains the recipe
var cached_recipe *models.Recipe = &models.Recipe{}
if err := d.redisClient.GetKeyTo(filename, cached_recipe); err != nil {
d.taoLogger.Log.Debug("GetRecipe.Cached", zap.Any("GetCacheRecipeError", err))
d.taoLogger.Log.Debug("GetRecipe", zap.String("filename", filename), zap.String("countryID", countryID))
// d.CurrentCountryID[countryID] = countryID
cached_recipe = nil
}
if cached_recipe != nil {
d.taoLogger.Log.Debug("GetRecipe", zap.Any("Check on cached recipe invalid", cached_recipe == nil), zap.Any("test config number", cached_recipe.MachineSetting.ConfigNumber))
}
recipe, err := helpers.ReadRecipeFile(countryID, filename)
if err != nil {
d.taoLogger.Log.Error("GetRecipe: Error when read recipe file, Return default recipe", zap.Error(err))
return d.currentRecipe[countryID]
d.taoLogger.Log.Debug("GetRecipe", zap.Any("ReadRecipeError -> return default", err))
return d.CurrentRecipe[countryID]
}
d.currentRecipe[countryID] = recipe
// cache to redis
d.redisClient.SetToKey(filename, recipe)
if err != nil {
d.taoLogger.Log.Error("GetRecipe: Error when read recipe file, Return default recipe", zap.Error(err))
return d.CurrentRecipe[countryID]
}
//. service is connected. Use from cache
// check healthcheck redis
err = d.redisClient.HealthCheck()
d.taoLogger.Log.Info("GetRecipe: HealthCheck", zap.Any("result", err))
if d.redisClient.HealthCheck() == nil && cached_recipe != nil {
d.taoLogger.Log.Debug("GetRecipeCached", zap.Any("cached_recipe", "yes"))
d.CurrentRecipe[countryID] = cached_recipe
} else {
d.taoLogger.Log.Debug("GetRecipeCached", zap.Any("cached_recipe", "no"))
d.CurrentRecipe[countryID] = recipe
}
// save to map
if len(d.recipeMap) > 5 { // limit keep in memory 5 version
@ -235,11 +301,11 @@ func (d *Data) GetRecipe(countryID, filename string) *models.Recipe {
}
d.recipeMap[filename] = RecipeWithTimeStamps{
Recipe: d.currentRecipe,
Recipe: d.CurrentRecipe,
TimeStamps: time.Now().Unix(),
}
return d.currentRecipe[countryID]
return d.CurrentRecipe[countryID]
}
// func (d *Data) GetRecipe01() []models.Recipe01 {
@ -272,7 +338,7 @@ func (d *Data) GetRecipe01ByProductCode(filename, countryID, productCode string)
// fmt.Println("GetRecipe01ByProductCode.ReadCurrent::CurrentFile", d.CurrentFile)
// fmt.Println("GetRecipe01ByProductCode.ReadCurrent::CurrentCountryID", d.CurrentCountryID)
for _, v := range d.currentRecipe[countryID].Recipe01 {
for _, v := range d.CurrentRecipe[countryID].Recipe01 {
if v.ProductCode == productCode {
return v, nil
} else if len(v.SubMenu) > 0 {
@ -320,26 +386,26 @@ func (d *Data) GetRecipe01ByProductCode(filename, countryID, productCode string)
}
}
recipe, err := helpers.ReadRecipeFile(countryID, filename)
recipe := d.GetRecipe(countryID, filename)
if err != nil {
d.taoLogger.Log.Error("GetRecipe01ByProductCode: Error when read recipe file, Return default recipe", zap.Error(err))
for _, v := range d.currentRecipe[countryID].Recipe01 {
if v.ProductCode == productCode {
return v, fmt.Errorf("[DEFAULT]-ERR")
} else if len(v.SubMenu) > 0 {
for _, subMenu := range v.SubMenu {
if subMenu.ProductCode == productCode {
return subMenu, fmt.Errorf("[DEFAULT]-ERR")
}
}
}
}
}
// if err != nil {
// d.taoLogger.Log.Error("GetRecipe01ByProductCode: Error when read recipe file, Return default recipe", zap.Error(err))
// for _, v := range d.CurrentRecipe[countryID].Recipe01 {
// if v.ProductCode == productCode {
// return v, fmt.Errorf("[DEFAULT]-ERR")
// } else if len(v.SubMenu) > 0 {
// for _, subMenu := range v.SubMenu {
// if subMenu.ProductCode == productCode {
// return subMenu, fmt.Errorf("[DEFAULT]-ERR")
// }
// }
// }
// }
// }
d.taoLogger.Log.Debug("GetRecipe01ByProductCode", zap.Any("productCode", productCode), zap.Any("version", recipe.MachineSetting.ConfigNumber))
d.currentRecipe[countryID] = recipe
d.CurrentRecipe[countryID] = recipe
// save to map
if len(d.recipeMap) > 5 { // limit keep in memory 5 version
@ -356,11 +422,11 @@ func (d *Data) GetRecipe01ByProductCode(filename, countryID, productCode string)
}
d.recipeMap[filename] = RecipeWithTimeStamps{
Recipe: d.currentRecipe,
Recipe: d.CurrentRecipe,
TimeStamps: time.Now().Unix(),
}
for _, v := range d.currentRecipe[countryID].Recipe01 {
for _, v := range d.CurrentRecipe[countryID].Recipe01 {
if v.ProductCode == productCode {
// d.taoLogger.Log.Debug("GetRecipe01ByProductCode", zap.Any("productCode", productCode), zap.Any("result", v))
return v, nil
@ -438,21 +504,21 @@ func (d *Data) GetMaterialSetting(countryID, filename string) []models.MaterialS
if countryID == "" {
// copy(result, d.currentRecipe[countryID].MaterialSetting)
return d.currentRecipe[countryID].MaterialSetting
return d.CurrentRecipe[countryID].MaterialSetting
}
if !strings.Contains(filename, "tmp") {
if filename == "" || filename == d.CurrentFile[countryID] {
// copy(result, d.currentRecipe[countryID].MaterialSetting)
// d.taoLogger.Log.Debug("GetMaterialSetting", zap.Any("result", result))
return d.currentRecipe[countryID].MaterialSetting
return d.CurrentRecipe[countryID].MaterialSetting
}
if recipe, ok := d.recipeMap[filename]; ok {
copy(result, recipe.Recipe[countryID].MaterialSetting)
d.CurrentFile[countryID] = filename
// d.CurrentCountryID[countryID] = countryID
return d.currentRecipe[countryID].MaterialSetting
return d.CurrentRecipe[countryID].MaterialSetting
}
}
@ -464,17 +530,17 @@ func (d *Data) GetMaterialSetting(countryID, filename string) []models.MaterialS
// d.CurrentFile[countryID] = filename
// d.CurrentCountryID[countryID] = countryID
recipe, err := helpers.ReadRecipeFile(countryID, filename)
recipe := d.GetRecipe(countryID, filename)
if err != nil {
d.taoLogger.Log.Error("GetMaterialSetting: Error when read recipe file, Return default recipe", zap.Error(err))
copy(result, d.currentRecipe[countryID].MaterialSetting)
return d.currentRecipe[countryID].MaterialSetting
}
// if err != nil {
// d.taoLogger.Log.Error("GetMaterialSetting: Error when read recipe file, Return default recipe", zap.Error(err))
// copy(result, d.CurrentRecipe[countryID].MaterialSetting)
// return d.CurrentRecipe[countryID].MaterialSetting
// }
// d.taoLogger.Log.Debug("GetMaterialSetting", zap.Any("recipe", recipe.MaterialSetting))
d.currentRecipe[countryID] = recipe
d.CurrentRecipe[countryID] = recipe
// save to map
if len(d.recipeMap) > 5 { // limit keep in memory 5 version
@ -491,7 +557,7 @@ func (d *Data) GetMaterialSetting(countryID, filename string) []models.MaterialS
}
d.recipeMap[filename] = RecipeWithTimeStamps{
Recipe: d.currentRecipe,
Recipe: d.CurrentRecipe,
TimeStamps: time.Now().Unix(),
}
@ -501,29 +567,26 @@ func (d *Data) GetMaterialSetting(countryID, filename string) []models.MaterialS
func (d *Data) GetAllToppingGroups(countryID, filename string) []models.ToppingGroup {
if countryID == "" {
return d.currentRecipe[countryID].Topping.ToppingGroup
return d.CurrentRecipe[countryID].Topping.ToppingGroup
}
if !strings.Contains(filename, ".tmp") {
if filename == "" || filename == d.CurrentFile[countryID] {
return d.currentRecipe[countryID].Topping.ToppingGroup
return d.CurrentRecipe[countryID].Topping.ToppingGroup
}
if _, ok := d.recipeMap[countryID]; ok {
d.CurrentFile[countryID] = filename
return d.currentRecipe[countryID].Topping.ToppingGroup
return d.CurrentRecipe[countryID].Topping.ToppingGroup
}
}
if filename == "default" {
filename = d.CurrentFile[countryID]
}
recipe, err := helpers.ReadRecipeFile(countryID, filename)
if err != nil {
return d.currentRecipe[countryID].Topping.ToppingGroup
}
recipe := d.GetRecipe(countryID, filename)
d.currentRecipe[countryID] = recipe
d.CurrentRecipe[countryID] = recipe
if len(d.recipeMap) > 5 { // limit keep in memory 5 version
// remove oldest version
@ -539,7 +602,7 @@ func (d *Data) GetAllToppingGroups(countryID, filename string) []models.ToppingG
}
d.recipeMap[filename] = RecipeWithTimeStamps{
Recipe: d.currentRecipe,
Recipe: d.CurrentRecipe,
TimeStamps: time.Now().Unix(),
}
@ -549,17 +612,17 @@ func (d *Data) GetAllToppingGroups(countryID, filename string) []models.ToppingG
func (d *Data) GetToppingsList(countryID, filename string) []models.ToppingList {
// do return default
if countryID == "" {
return d.currentRecipe[countryID].Topping.ToppingList
return d.CurrentRecipe[countryID].Topping.ToppingList
}
// handle temporary file
if !strings.Contains(filename, ".tmp") {
if filename == "" || filename == d.CurrentFile[countryID] {
return d.currentRecipe[countryID].Topping.ToppingList
return d.CurrentRecipe[countryID].Topping.ToppingList
}
if _, ok := d.recipeMap[countryID]; ok {
d.CurrentFile[countryID] = filename
return d.currentRecipe[countryID].Topping.ToppingList
return d.CurrentRecipe[countryID].Topping.ToppingList
}
}
@ -567,10 +630,7 @@ func (d *Data) GetToppingsList(countryID, filename string) []models.ToppingList
filename = d.CurrentFile[countryID]
}
recipe, err := helpers.ReadRecipeFile(countryID, filename)
if err != nil {
return d.currentRecipe[countryID].Topping.ToppingList
}
recipe := d.GetRecipe(countryID, filename)
if len(d.recipeMap) > 5 { // limit keep in memory 5 version
// remove oldest version
@ -586,7 +646,7 @@ func (d *Data) GetToppingsList(countryID, filename string) []models.ToppingList
}
d.recipeMap[filename] = RecipeWithTimeStamps{
Recipe: d.currentRecipe,
Recipe: d.CurrentRecipe,
TimeStamps: time.Now().Unix(),
}
@ -597,7 +657,7 @@ func (d *Data) GetMaterialCode(ids []uint64, countryID, filename string) []model
var result []models.MaterialCode
if filename == "" || filename == d.CurrentFile[countryID] {
result = d.currentRecipe[countryID].MaterialCode
result = d.CurrentRecipe[countryID].MaterialCode
} else if recipe, ok := d.recipeMap[filename]; ok {
d.CurrentFile[countryID] = filename
return recipe.Recipe[countryID].MaterialCode
@ -609,14 +669,14 @@ func (d *Data) GetMaterialCode(ids []uint64, countryID, filename string) []model
// d.CurrentFile[countryID] = filename
// d.CurrentCountryID[countryID] = countryID
recipe, err := helpers.ReadRecipeFile(countryID, filename)
recipe := d.GetRecipe(countryID, filename)
if err != nil {
d.taoLogger.Log.Error("GetMaterialCode: Error when read recipe file, Return default recipe", zap.Error(err))
return d.currentRecipe[countryID].MaterialCode
}
// if err != nil {
// d.taoLogger.Log.Error("GetMaterialCode: Error when read recipe file, Return default recipe", zap.Error(err))
// return d.CurrentRecipe[countryID].MaterialCode
// }
d.currentRecipe[countryID] = recipe
d.CurrentRecipe[countryID] = recipe
// save to map
if len(d.recipeMap) > 5 { // limit keep in memory 5 version
@ -633,11 +693,11 @@ func (d *Data) GetMaterialCode(ids []uint64, countryID, filename string) []model
}
d.recipeMap[filename] = RecipeWithTimeStamps{
Recipe: d.currentRecipe,
Recipe: d.CurrentRecipe,
TimeStamps: time.Now().Unix(),
}
result = d.currentRecipe[countryID].MaterialCode
result = d.CurrentRecipe[countryID].MaterialCode
}
if len(ids) == 0 {
@ -664,7 +724,7 @@ func (d *Data) GetMaterialCode(ids []uint64, countryID, filename string) []model
func (d *Data) GetToppings(countryID, filename string) models.Topping {
if filename == "" || filename == d.CurrentFile[countryID] {
return d.currentRecipe[countryID].Topping
return d.CurrentRecipe[countryID].Topping
} else if recipe, ok := d.recipeMap[filename]; ok {
d.CurrentFile[countryID] = filename
return recipe.Recipe[countryID].Topping
@ -676,14 +736,9 @@ func (d *Data) GetToppings(countryID, filename string) models.Topping {
// d.CurrentFile[countryID] = filename
// d.CurrentCountryID[countryID] = countryID
recipe, err := helpers.ReadRecipeFile(countryID, filename)
recipe := d.GetRecipe(countryID, filename)
if err != nil {
d.taoLogger.Log.Error("GetToppings: Error when read recipe file, Return default recipe", zap.Error(err))
return d.currentRecipe[countryID].Topping
}
d.currentRecipe[countryID] = recipe
d.CurrentRecipe[countryID] = recipe
return recipe.Topping
}