Add redis. Prep for new save file
This commit is contained in:
parent
6c22be7d7c
commit
0604a3b77f
5 changed files with 295 additions and 87 deletions
|
|
@ -27,10 +27,11 @@ type Data struct {
|
||||||
CurrentCountryID map[string]string
|
CurrentCountryID map[string]string
|
||||||
DefaultCountryMap []DefaultByCountry
|
DefaultCountryMap []DefaultByCountry
|
||||||
AllRecipeFiles map[string][]helpers.RecipePath
|
AllRecipeFiles map[string][]helpers.RecipePath
|
||||||
currentRecipe map[string]*models.Recipe
|
CurrentRecipe map[string]*models.Recipe
|
||||||
recipeMap map[string]RecipeWithTimeStamps
|
recipeMap map[string]RecipeWithTimeStamps
|
||||||
Countries []helpers.CountryName
|
Countries []helpers.CountryName
|
||||||
taoLogger *logger.TaoLogger
|
taoLogger *logger.TaoLogger
|
||||||
|
redisClient *RedisCli
|
||||||
}
|
}
|
||||||
|
|
||||||
type DefaultByCountry struct {
|
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)
|
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)
|
log.Panic("Error when read default recipe file for each country:", v.CountryShortName, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
redisClient.SetToKey(v2.Name, defaultRecipe)
|
||||||
|
|
||||||
currentDefaultFileForEachCountry[v.CountryShortName] = defaultRecipe
|
currentDefaultFileForEachCountry[v.CountryShortName] = defaultRecipe
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
@ -169,7 +172,7 @@ func NewData(taoLogger *logger.TaoLogger) *Data {
|
||||||
CurrentFile: currentFileMap,
|
CurrentFile: currentFileMap,
|
||||||
CurrentCountryID: CurrentCountryIDMap,
|
CurrentCountryID: CurrentCountryIDMap,
|
||||||
AllRecipeFiles: allRecipeFiles,
|
AllRecipeFiles: allRecipeFiles,
|
||||||
currentRecipe: currentDefaultFileForEachCountry,
|
CurrentRecipe: currentDefaultFileForEachCountry,
|
||||||
recipeMap: map[string]RecipeWithTimeStamps{
|
recipeMap: map[string]RecipeWithTimeStamps{
|
||||||
defaultFile: {
|
defaultFile: {
|
||||||
Recipe: currentDefaultFileForEachCountry,
|
Recipe: currentDefaultFileForEachCountry,
|
||||||
|
|
@ -179,6 +182,7 @@ func NewData(taoLogger *logger.TaoLogger) *Data {
|
||||||
Countries: countries,
|
Countries: countries,
|
||||||
taoLogger: taoLogger,
|
taoLogger: taoLogger,
|
||||||
DefaultCountryMap: defaultForEachCountry,
|
DefaultCountryMap: defaultForEachCountry,
|
||||||
|
redisClient: redisClient,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -189,17 +193,47 @@ func (d *Data) GetRecipe(countryID, filename string) *models.Recipe {
|
||||||
// TODO: concat submenu into recipe
|
// TODO: concat submenu into recipe
|
||||||
|
|
||||||
if countryID == "" {
|
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] {
|
if filename == "" {
|
||||||
return d.currentRecipe[countryID]
|
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.CurrentFile[countryID] = filename
|
||||||
// d.CurrentCountryID[countryID] = countryID
|
// 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
|
// change current version and read new recipe
|
||||||
|
|
@ -208,17 +242,49 @@ func (d *Data) GetRecipe(countryID, filename string) *models.Recipe {
|
||||||
filename = d.CurrentFile[countryID]
|
filename = d.CurrentFile[countryID]
|
||||||
}
|
}
|
||||||
|
|
||||||
// d.CurrentFile[countryID] = filename
|
// var recipe *models.Recipe = &models.Recipe{}
|
||||||
d.taoLogger.Log.Debug("GetRecipe", zap.String("filename", filename), zap.String("countryID", countryID))
|
|
||||||
// d.CurrentCountryID[countryID] = countryID
|
// 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)
|
recipe, err := helpers.ReadRecipeFile(countryID, filename)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
d.taoLogger.Log.Error("GetRecipe: Error when read recipe file, Return default recipe", zap.Error(err))
|
d.taoLogger.Log.Debug("GetRecipe", zap.Any("ReadRecipeError -> return default", err))
|
||||||
return d.currentRecipe[countryID]
|
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
|
// save to map
|
||||||
if len(d.recipeMap) > 5 { // limit keep in memory 5 version
|
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{
|
d.recipeMap[filename] = RecipeWithTimeStamps{
|
||||||
Recipe: d.currentRecipe,
|
Recipe: d.CurrentRecipe,
|
||||||
TimeStamps: time.Now().Unix(),
|
TimeStamps: time.Now().Unix(),
|
||||||
}
|
}
|
||||||
|
|
||||||
return d.currentRecipe[countryID]
|
return d.CurrentRecipe[countryID]
|
||||||
}
|
}
|
||||||
|
|
||||||
// func (d *Data) GetRecipe01() []models.Recipe01 {
|
// 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::CurrentFile", d.CurrentFile)
|
||||||
// fmt.Println("GetRecipe01ByProductCode.ReadCurrent::CurrentCountryID", d.CurrentCountryID)
|
// 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 {
|
if v.ProductCode == productCode {
|
||||||
return v, nil
|
return v, nil
|
||||||
} else if len(v.SubMenu) > 0 {
|
} 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 {
|
// if err != nil {
|
||||||
d.taoLogger.Log.Error("GetRecipe01ByProductCode: Error when read recipe file, Return default recipe", zap.Error(err))
|
// d.taoLogger.Log.Error("GetRecipe01ByProductCode: Error when read recipe file, Return default recipe", zap.Error(err))
|
||||||
for _, v := range d.currentRecipe[countryID].Recipe01 {
|
// for _, v := range d.CurrentRecipe[countryID].Recipe01 {
|
||||||
if v.ProductCode == productCode {
|
// if v.ProductCode == productCode {
|
||||||
return v, fmt.Errorf("[DEFAULT]-ERR")
|
// return v, fmt.Errorf("[DEFAULT]-ERR")
|
||||||
} else if len(v.SubMenu) > 0 {
|
// } else if len(v.SubMenu) > 0 {
|
||||||
for _, subMenu := range v.SubMenu {
|
// for _, subMenu := range v.SubMenu {
|
||||||
if subMenu.ProductCode == productCode {
|
// if subMenu.ProductCode == productCode {
|
||||||
return subMenu, fmt.Errorf("[DEFAULT]-ERR")
|
// return subMenu, fmt.Errorf("[DEFAULT]-ERR")
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
d.taoLogger.Log.Debug("GetRecipe01ByProductCode", zap.Any("productCode", productCode), zap.Any("version", recipe.MachineSetting.ConfigNumber))
|
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
|
// save to map
|
||||||
if len(d.recipeMap) > 5 { // limit keep in memory 5 version
|
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{
|
d.recipeMap[filename] = RecipeWithTimeStamps{
|
||||||
Recipe: d.currentRecipe,
|
Recipe: d.CurrentRecipe,
|
||||||
TimeStamps: time.Now().Unix(),
|
TimeStamps: time.Now().Unix(),
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, v := range d.currentRecipe[countryID].Recipe01 {
|
for _, v := range d.CurrentRecipe[countryID].Recipe01 {
|
||||||
if v.ProductCode == productCode {
|
if v.ProductCode == productCode {
|
||||||
// d.taoLogger.Log.Debug("GetRecipe01ByProductCode", zap.Any("productCode", productCode), zap.Any("result", v))
|
// d.taoLogger.Log.Debug("GetRecipe01ByProductCode", zap.Any("productCode", productCode), zap.Any("result", v))
|
||||||
return v, nil
|
return v, nil
|
||||||
|
|
@ -438,21 +504,21 @@ func (d *Data) GetMaterialSetting(countryID, filename string) []models.MaterialS
|
||||||
|
|
||||||
if countryID == "" {
|
if countryID == "" {
|
||||||
// copy(result, d.currentRecipe[countryID].MaterialSetting)
|
// copy(result, d.currentRecipe[countryID].MaterialSetting)
|
||||||
return d.currentRecipe[countryID].MaterialSetting
|
return d.CurrentRecipe[countryID].MaterialSetting
|
||||||
}
|
}
|
||||||
|
|
||||||
if !strings.Contains(filename, "tmp") {
|
if !strings.Contains(filename, "tmp") {
|
||||||
if filename == "" || filename == d.CurrentFile[countryID] {
|
if filename == "" || filename == d.CurrentFile[countryID] {
|
||||||
// copy(result, d.currentRecipe[countryID].MaterialSetting)
|
// copy(result, d.currentRecipe[countryID].MaterialSetting)
|
||||||
// d.taoLogger.Log.Debug("GetMaterialSetting", zap.Any("result", result))
|
// 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 {
|
if recipe, ok := d.recipeMap[filename]; ok {
|
||||||
copy(result, recipe.Recipe[countryID].MaterialSetting)
|
copy(result, recipe.Recipe[countryID].MaterialSetting)
|
||||||
d.CurrentFile[countryID] = filename
|
d.CurrentFile[countryID] = filename
|
||||||
// d.CurrentCountryID[countryID] = countryID
|
// 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.CurrentFile[countryID] = filename
|
||||||
// d.CurrentCountryID[countryID] = countryID
|
// d.CurrentCountryID[countryID] = countryID
|
||||||
recipe, err := helpers.ReadRecipeFile(countryID, filename)
|
recipe := d.GetRecipe(countryID, filename)
|
||||||
|
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
d.taoLogger.Log.Error("GetMaterialSetting: Error when read recipe file, Return default recipe", zap.Error(err))
|
// d.taoLogger.Log.Error("GetMaterialSetting: Error when read recipe file, Return default recipe", zap.Error(err))
|
||||||
copy(result, d.currentRecipe[countryID].MaterialSetting)
|
// copy(result, d.CurrentRecipe[countryID].MaterialSetting)
|
||||||
return d.currentRecipe[countryID].MaterialSetting
|
// return d.CurrentRecipe[countryID].MaterialSetting
|
||||||
}
|
// }
|
||||||
|
|
||||||
// d.taoLogger.Log.Debug("GetMaterialSetting", zap.Any("recipe", recipe.MaterialSetting))
|
// d.taoLogger.Log.Debug("GetMaterialSetting", zap.Any("recipe", recipe.MaterialSetting))
|
||||||
|
|
||||||
d.currentRecipe[countryID] = recipe
|
d.CurrentRecipe[countryID] = recipe
|
||||||
|
|
||||||
// save to map
|
// save to map
|
||||||
if len(d.recipeMap) > 5 { // limit keep in memory 5 version
|
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{
|
d.recipeMap[filename] = RecipeWithTimeStamps{
|
||||||
Recipe: d.currentRecipe,
|
Recipe: d.CurrentRecipe,
|
||||||
TimeStamps: time.Now().Unix(),
|
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 {
|
func (d *Data) GetAllToppingGroups(countryID, filename string) []models.ToppingGroup {
|
||||||
if countryID == "" {
|
if countryID == "" {
|
||||||
return d.currentRecipe[countryID].Topping.ToppingGroup
|
return d.CurrentRecipe[countryID].Topping.ToppingGroup
|
||||||
}
|
}
|
||||||
|
|
||||||
if !strings.Contains(filename, ".tmp") {
|
if !strings.Contains(filename, ".tmp") {
|
||||||
if filename == "" || filename == d.CurrentFile[countryID] {
|
if filename == "" || filename == d.CurrentFile[countryID] {
|
||||||
return d.currentRecipe[countryID].Topping.ToppingGroup
|
return d.CurrentRecipe[countryID].Topping.ToppingGroup
|
||||||
}
|
}
|
||||||
if _, ok := d.recipeMap[countryID]; ok {
|
if _, ok := d.recipeMap[countryID]; ok {
|
||||||
d.CurrentFile[countryID] = filename
|
d.CurrentFile[countryID] = filename
|
||||||
|
|
||||||
return d.currentRecipe[countryID].Topping.ToppingGroup
|
return d.CurrentRecipe[countryID].Topping.ToppingGroup
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if filename == "default" {
|
if filename == "default" {
|
||||||
filename = d.CurrentFile[countryID]
|
filename = d.CurrentFile[countryID]
|
||||||
}
|
}
|
||||||
recipe, err := helpers.ReadRecipeFile(countryID, filename)
|
recipe := d.GetRecipe(countryID, filename)
|
||||||
if err != nil {
|
|
||||||
return d.currentRecipe[countryID].Topping.ToppingGroup
|
|
||||||
}
|
|
||||||
|
|
||||||
d.currentRecipe[countryID] = recipe
|
d.CurrentRecipe[countryID] = recipe
|
||||||
|
|
||||||
if len(d.recipeMap) > 5 { // limit keep in memory 5 version
|
if len(d.recipeMap) > 5 { // limit keep in memory 5 version
|
||||||
// remove oldest version
|
// remove oldest version
|
||||||
|
|
@ -539,7 +602,7 @@ func (d *Data) GetAllToppingGroups(countryID, filename string) []models.ToppingG
|
||||||
}
|
}
|
||||||
|
|
||||||
d.recipeMap[filename] = RecipeWithTimeStamps{
|
d.recipeMap[filename] = RecipeWithTimeStamps{
|
||||||
Recipe: d.currentRecipe,
|
Recipe: d.CurrentRecipe,
|
||||||
TimeStamps: time.Now().Unix(),
|
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 {
|
func (d *Data) GetToppingsList(countryID, filename string) []models.ToppingList {
|
||||||
// do return default
|
// do return default
|
||||||
if countryID == "" {
|
if countryID == "" {
|
||||||
return d.currentRecipe[countryID].Topping.ToppingList
|
return d.CurrentRecipe[countryID].Topping.ToppingList
|
||||||
}
|
}
|
||||||
|
|
||||||
// handle temporary file
|
// handle temporary file
|
||||||
if !strings.Contains(filename, ".tmp") {
|
if !strings.Contains(filename, ".tmp") {
|
||||||
if filename == "" || filename == d.CurrentFile[countryID] {
|
if filename == "" || filename == d.CurrentFile[countryID] {
|
||||||
return d.currentRecipe[countryID].Topping.ToppingList
|
return d.CurrentRecipe[countryID].Topping.ToppingList
|
||||||
}
|
}
|
||||||
if _, ok := d.recipeMap[countryID]; ok {
|
if _, ok := d.recipeMap[countryID]; ok {
|
||||||
d.CurrentFile[countryID] = filename
|
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]
|
filename = d.CurrentFile[countryID]
|
||||||
}
|
}
|
||||||
|
|
||||||
recipe, err := helpers.ReadRecipeFile(countryID, filename)
|
recipe := d.GetRecipe(countryID, filename)
|
||||||
if err != nil {
|
|
||||||
return d.currentRecipe[countryID].Topping.ToppingList
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(d.recipeMap) > 5 { // limit keep in memory 5 version
|
if len(d.recipeMap) > 5 { // limit keep in memory 5 version
|
||||||
// remove oldest version
|
// remove oldest version
|
||||||
|
|
@ -586,7 +646,7 @@ func (d *Data) GetToppingsList(countryID, filename string) []models.ToppingList
|
||||||
}
|
}
|
||||||
|
|
||||||
d.recipeMap[filename] = RecipeWithTimeStamps{
|
d.recipeMap[filename] = RecipeWithTimeStamps{
|
||||||
Recipe: d.currentRecipe,
|
Recipe: d.CurrentRecipe,
|
||||||
TimeStamps: time.Now().Unix(),
|
TimeStamps: time.Now().Unix(),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -597,7 +657,7 @@ func (d *Data) GetMaterialCode(ids []uint64, countryID, filename string) []model
|
||||||
var result []models.MaterialCode
|
var result []models.MaterialCode
|
||||||
|
|
||||||
if filename == "" || filename == d.CurrentFile[countryID] {
|
if filename == "" || filename == d.CurrentFile[countryID] {
|
||||||
result = d.currentRecipe[countryID].MaterialCode
|
result = d.CurrentRecipe[countryID].MaterialCode
|
||||||
} else if recipe, ok := d.recipeMap[filename]; ok {
|
} else if recipe, ok := d.recipeMap[filename]; ok {
|
||||||
d.CurrentFile[countryID] = filename
|
d.CurrentFile[countryID] = filename
|
||||||
return recipe.Recipe[countryID].MaterialCode
|
return recipe.Recipe[countryID].MaterialCode
|
||||||
|
|
@ -609,14 +669,14 @@ func (d *Data) GetMaterialCode(ids []uint64, countryID, filename string) []model
|
||||||
|
|
||||||
// d.CurrentFile[countryID] = filename
|
// d.CurrentFile[countryID] = filename
|
||||||
// d.CurrentCountryID[countryID] = countryID
|
// d.CurrentCountryID[countryID] = countryID
|
||||||
recipe, err := helpers.ReadRecipeFile(countryID, filename)
|
recipe := d.GetRecipe(countryID, filename)
|
||||||
|
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
d.taoLogger.Log.Error("GetMaterialCode: Error when read recipe file, Return default recipe", zap.Error(err))
|
// d.taoLogger.Log.Error("GetMaterialCode: Error when read recipe file, Return default recipe", zap.Error(err))
|
||||||
return d.currentRecipe[countryID].MaterialCode
|
// return d.CurrentRecipe[countryID].MaterialCode
|
||||||
}
|
// }
|
||||||
|
|
||||||
d.currentRecipe[countryID] = recipe
|
d.CurrentRecipe[countryID] = recipe
|
||||||
|
|
||||||
// save to map
|
// save to map
|
||||||
if len(d.recipeMap) > 5 { // limit keep in memory 5 version
|
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{
|
d.recipeMap[filename] = RecipeWithTimeStamps{
|
||||||
Recipe: d.currentRecipe,
|
Recipe: d.CurrentRecipe,
|
||||||
TimeStamps: time.Now().Unix(),
|
TimeStamps: time.Now().Unix(),
|
||||||
}
|
}
|
||||||
|
|
||||||
result = d.currentRecipe[countryID].MaterialCode
|
result = d.CurrentRecipe[countryID].MaterialCode
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(ids) == 0 {
|
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 {
|
func (d *Data) GetToppings(countryID, filename string) models.Topping {
|
||||||
|
|
||||||
if filename == "" || filename == d.CurrentFile[countryID] {
|
if filename == "" || filename == d.CurrentFile[countryID] {
|
||||||
return d.currentRecipe[countryID].Topping
|
return d.CurrentRecipe[countryID].Topping
|
||||||
} else if recipe, ok := d.recipeMap[filename]; ok {
|
} else if recipe, ok := d.recipeMap[filename]; ok {
|
||||||
d.CurrentFile[countryID] = filename
|
d.CurrentFile[countryID] = filename
|
||||||
return recipe.Recipe[countryID].Topping
|
return recipe.Recipe[countryID].Topping
|
||||||
|
|
@ -676,14 +736,9 @@ func (d *Data) GetToppings(countryID, filename string) models.Topping {
|
||||||
|
|
||||||
// d.CurrentFile[countryID] = filename
|
// d.CurrentFile[countryID] = filename
|
||||||
// d.CurrentCountryID[countryID] = countryID
|
// d.CurrentCountryID[countryID] = countryID
|
||||||
recipe, err := helpers.ReadRecipeFile(countryID, filename)
|
recipe := d.GetRecipe(countryID, filename)
|
||||||
|
|
||||||
if err != nil {
|
d.CurrentRecipe[countryID] = recipe
|
||||||
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
|
|
||||||
|
|
||||||
return recipe.Topping
|
return recipe.Topping
|
||||||
}
|
}
|
||||||
|
|
|
||||||
137
server/data/redis.go
Normal file
137
server/data/redis.go
Normal file
|
|
@ -0,0 +1,137 @@
|
||||||
|
package data
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/redis/go-redis/v9"
|
||||||
|
)
|
||||||
|
|
||||||
|
type RedisCli struct {
|
||||||
|
Client *redis.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewRedisClient(address, password string) *RedisCli {
|
||||||
|
|
||||||
|
// option, err := redis.ParseURL("redis://" + username + ":" + password + "@localhost:6379/" + db)
|
||||||
|
|
||||||
|
options := redis.Options{
|
||||||
|
Addr: address,
|
||||||
|
Password: password,
|
||||||
|
DB: 0,
|
||||||
|
}
|
||||||
|
|
||||||
|
client := redis.NewClient(&options)
|
||||||
|
|
||||||
|
if err := client.Ping(context.Background()); err != nil {
|
||||||
|
fmt.Println("trying localhost ...", err)
|
||||||
|
client = redis.NewClient(&redis.Options{
|
||||||
|
Addr: "localhost:6379",
|
||||||
|
Password: password,
|
||||||
|
DB: 0,
|
||||||
|
})
|
||||||
|
|
||||||
|
if err_local := client.Ping(context.Background()); err_local != nil {
|
||||||
|
fmt.Println("> result ====> ", err_local)
|
||||||
|
// do as warning
|
||||||
|
} else {
|
||||||
|
fmt.Println("\n> Localhost Redis OK!\n")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
fmt.Println("\n> Redis OK! \n")
|
||||||
|
}
|
||||||
|
|
||||||
|
return &RedisCli{
|
||||||
|
Client: client,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *RedisCli) HealthCheck() error {
|
||||||
|
return r.Client.Ping(context.Background()).Err()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *RedisCli) GetKeyTo(source string, dest interface{}) error {
|
||||||
|
fmt.Println("Redis> GET ", source)
|
||||||
|
|
||||||
|
// if cannot pass healthcheck, return err
|
||||||
|
if err := r.HealthCheck(); err != nil {
|
||||||
|
fmt.Println("HS> GET error ", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
saved, err := r.Client.Get(context.Background(), source).Result()
|
||||||
|
|
||||||
|
// chcek EOF
|
||||||
|
// fmt.Println("GET last char ", saved[len(saved)-1:])
|
||||||
|
|
||||||
|
if saved == "" || err != nil {
|
||||||
|
fmt.Println("GET error (empty on error)|", err, "|while saved=", saved)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// fmt.Println("GET ", saved)
|
||||||
|
|
||||||
|
// if err != nil {
|
||||||
|
// fmt.Println("GET error ", err)
|
||||||
|
// return err
|
||||||
|
// }
|
||||||
|
err = json.NewDecoder(bytes.NewBufferString(saved)).Decode(dest)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("GET error ", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *RedisCli) SetToKey(key string, value interface{}) error {
|
||||||
|
fmt.Println("Redis> SET ", key)
|
||||||
|
|
||||||
|
// if cannot pass healthcheck, return err
|
||||||
|
if err := r.HealthCheck(); err != nil {
|
||||||
|
fmt.Println("HS> SET error ", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
saved, err := json.Marshal(value)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("SET error ", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// late error
|
||||||
|
err = r.Client.Set(context.Background(), key, saved, redis.KeepTTL).Err()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("error on SET ", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *RedisCli) ExpireKey(key string) error {
|
||||||
|
|
||||||
|
// if cannot pass healthcheck, return err
|
||||||
|
if err := r.HealthCheck(); err != nil {
|
||||||
|
fmt.Println("HS> EXPIRE error ", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return r.Client.Expire(context.Background(), key, 0).Err()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *RedisCli) SetKeyTimeout(key string, value interface{}, timeout int) error {
|
||||||
|
|
||||||
|
// if cannot pass healthcheck, return err
|
||||||
|
if err := r.HealthCheck(); err != nil {
|
||||||
|
fmt.Println("HS> EXPIRE error ", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err := r.Client.Expire(context.Background(), key, time.Duration(timeout)*time.Second).Err()
|
||||||
|
fmt.Println("error on EXPIRE ", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
@ -48,6 +48,12 @@ func (mr *MaterialRouter) GetFullMaterialDetail(w http.ResponseWriter, r *http.R
|
||||||
matSettings := mr.data.GetMaterialSetting(country, filename)
|
matSettings := mr.data.GetMaterialSetting(country, filename)
|
||||||
matCodes := mr.data.GetRecipe(country, filename).MaterialCode
|
matCodes := mr.data.GetRecipe(country, filename).MaterialCode
|
||||||
|
|
||||||
|
if len(matSettings) == 0 {
|
||||||
|
// mr.taoLogger.Log.Error("MaterialRouter.GetFullMaterialDetail", zap.Error(err))
|
||||||
|
http.Error(w, "Material not found", http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// combine
|
// combine
|
||||||
materialDetails := []map[string]interface{}{}
|
materialDetails := []map[string]interface{}{}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -30,6 +30,7 @@ type RecipeRouter struct {
|
||||||
sheetService sheet.SheetService
|
sheetService sheet.SheetService
|
||||||
recipeService recipe.RecipeService
|
recipeService recipe.RecipeService
|
||||||
taoLogger *logger.TaoLogger
|
taoLogger *logger.TaoLogger
|
||||||
|
cache_db *data.RedisCli
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|
@ -37,12 +38,13 @@ var (
|
||||||
updateMutex = sync.Mutex{}
|
updateMutex = sync.Mutex{}
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewRecipeRouter(data *data.Data, recipeService recipe.RecipeService, sheetService sheet.SheetService, taoLogger *logger.TaoLogger) *RecipeRouter {
|
func NewRecipeRouter(data *data.Data, recipeService recipe.RecipeService, sheetService sheet.SheetService, taoLogger *logger.TaoLogger, cache *data.RedisCli) *RecipeRouter {
|
||||||
return &RecipeRouter{
|
return &RecipeRouter{
|
||||||
data,
|
data,
|
||||||
sheetService,
|
sheetService,
|
||||||
recipeService,
|
recipeService,
|
||||||
taoLogger,
|
taoLogger,
|
||||||
|
cache,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -460,6 +462,7 @@ func (rr *RecipeRouter) updateRecipe(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
// gen hash
|
// gen hash
|
||||||
commit_hash, err := data.HashCommit(8)
|
commit_hash, err := data.HashCommit(8)
|
||||||
|
rr.cache_db.SetToKey(commit_hash, targetRecipe)
|
||||||
|
|
||||||
commit := data.CommitLog{
|
commit := data.CommitLog{
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,6 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/jmoiron/sqlx"
|
"github.com/jmoiron/sqlx"
|
||||||
"github.com/redis/go-redis/v9"
|
|
||||||
|
|
||||||
"github.com/go-chi/chi/v5"
|
"github.com/go-chi/chi/v5"
|
||||||
"github.com/go-chi/cors"
|
"github.com/go-chi/cors"
|
||||||
|
|
@ -31,7 +30,7 @@ type Server struct {
|
||||||
server *http.Server
|
server *http.Server
|
||||||
data *data.Data
|
data *data.Data
|
||||||
database *sqlx.DB
|
database *sqlx.DB
|
||||||
cache_db *redis.Client
|
cache_db *data.RedisCli
|
||||||
cfg *config.ServerConfig
|
cfg *config.ServerConfig
|
||||||
oauth oauth.OAuthService
|
oauth oauth.OAuthService
|
||||||
taoLogger *logger.TaoLogger
|
taoLogger *logger.TaoLogger
|
||||||
|
|
@ -42,13 +41,15 @@ func NewServer(cfg *config.ServerConfig, oauthService oauth.OAuthService) *Serve
|
||||||
taoLogger := logger.NewTaoLogger(cfg)
|
taoLogger := logger.NewTaoLogger(cfg)
|
||||||
taoLogger.Log = taoLogger.Log.Named("Server")
|
taoLogger.Log = taoLogger.Log.Named("Server")
|
||||||
|
|
||||||
|
redisClient := data.NewRedisClient("redis:6379", "")
|
||||||
|
|
||||||
return &Server{
|
return &Server{
|
||||||
server: &http.Server{Addr: fmt.Sprintf(":%d", cfg.ServerPort)},
|
server: &http.Server{Addr: fmt.Sprintf(":%d", cfg.ServerPort)},
|
||||||
data: data.NewData(taoLogger),
|
data: data.NewData(taoLogger, redisClient),
|
||||||
database: data.NewSqliteDatabase(),
|
database: data.NewSqliteDatabase(),
|
||||||
cache_db: data.NewRedisClient("redis:6379", "", ""),
|
cache_db: redisClient,
|
||||||
cfg: cfg,
|
cfg: cfg,
|
||||||
oauth: oauth.NewOAuthService(serverCfg),
|
oauth: oauth.NewOAuthService(cfg),
|
||||||
taoLogger: taoLogger,
|
taoLogger: taoLogger,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -101,6 +102,12 @@ func (s *Server) createHandler() {
|
||||||
ar.Route(r)
|
ar.Route(r)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Initial redis
|
||||||
|
for k, v := range s.data.CurrentRecipe {
|
||||||
|
s.taoLogger.Log.Debug("Caching", zap.Any("Recipe", k))
|
||||||
|
s.cache_db.SetToKey(k, v)
|
||||||
|
}
|
||||||
|
|
||||||
// Protected Group
|
// Protected Group
|
||||||
r.Group(func(r chi.Router) {
|
r.Group(func(r chi.Router) {
|
||||||
|
|
||||||
|
|
@ -116,7 +123,7 @@ func (s *Server) createHandler() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Recipe Router
|
// Recipe Router
|
||||||
rr := routers.NewRecipeRouter(s.data, recipeService, sheetService, s.taoLogger)
|
rr := routers.NewRecipeRouter(s.data, recipeService, sheetService, s.taoLogger, s.cache_db)
|
||||||
rr.Route(r)
|
rr.Route(r)
|
||||||
|
|
||||||
// Material Router
|
// Material Router
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue