make it faster
This commit is contained in:
parent
728b92228b
commit
196cc8e843
2 changed files with 104 additions and 14 deletions
|
|
@ -1,4 +1,4 @@
|
||||||
export const environment = {
|
export const environment = {
|
||||||
production: true,
|
production: true,
|
||||||
api: '',
|
api: 'https://recipe.taobin.io:8090/api/',
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"recipe-manager/models"
|
"recipe-manager/models"
|
||||||
"sort"
|
"sort"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func readFile(version string) *models.Recipe {
|
func readFile(version string) *models.Recipe {
|
||||||
|
|
@ -32,10 +33,16 @@ func readFile(version string) *models.Recipe {
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type RecipeWithTimeStamps struct {
|
||||||
|
Recipe models.Recipe
|
||||||
|
TimeStamps int64
|
||||||
|
}
|
||||||
|
|
||||||
type Data struct {
|
type Data struct {
|
||||||
CurrentVersion string
|
CurrentVersion string
|
||||||
AllVersions []string
|
AllVersions []string
|
||||||
recipe *models.Recipe
|
currentRecipe *models.Recipe
|
||||||
|
recipeMap map[string]RecipeWithTimeStamps
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewData() *Data {
|
func NewData() *Data {
|
||||||
|
|
@ -65,42 +72,102 @@ func NewData() *Data {
|
||||||
files[i] = filepath.Base(files[i])
|
files[i] = filepath.Base(files[i])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
defaultVersion := "coffeethai02_580.json"
|
||||||
|
defaultRecipe := readFile(defaultVersion)
|
||||||
|
|
||||||
return &Data{
|
return &Data{
|
||||||
CurrentVersion: "coffeethai02_580.json",
|
CurrentVersion: defaultVersion,
|
||||||
AllVersions: files,
|
AllVersions: files,
|
||||||
recipe: readFile("coffeethai02_580.json"),
|
currentRecipe: defaultRecipe,
|
||||||
|
recipeMap: map[string]RecipeWithTimeStamps{
|
||||||
|
defaultVersion: {
|
||||||
|
Recipe: *defaultRecipe,
|
||||||
|
TimeStamps: time.Now().Unix(),
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Data) GetRecipe(version string) models.Recipe {
|
func (d *Data) GetRecipe(version string) models.Recipe {
|
||||||
|
|
||||||
if version == "" || version == d.CurrentVersion {
|
if version == "" || version == d.CurrentVersion {
|
||||||
return *d.recipe
|
return *d.currentRecipe
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println("Change recipe to version:", version)
|
log.Println("Change recipe to version:", version)
|
||||||
|
|
||||||
|
if recipe, ok := d.recipeMap[version]; ok {
|
||||||
|
d.CurrentVersion = version
|
||||||
|
return recipe.Recipe
|
||||||
|
}
|
||||||
|
|
||||||
// change current version and read new recipe
|
// change current version and read new recipe
|
||||||
d.CurrentVersion = version
|
d.CurrentVersion = version
|
||||||
d.recipe = readFile(version)
|
d.currentRecipe = readFile(version)
|
||||||
return *d.recipe
|
|
||||||
|
// save to map
|
||||||
|
if len(d.recipeMap) > 5 { // limit keep in memory 5 version
|
||||||
|
// remove oldest version
|
||||||
|
var oldestVersion string
|
||||||
|
var oldestTime int64
|
||||||
|
for k, v := range d.recipeMap {
|
||||||
|
if oldestTime == 0 || v.TimeStamps < oldestTime {
|
||||||
|
oldestTime = v.TimeStamps
|
||||||
|
oldestVersion = k
|
||||||
|
}
|
||||||
|
}
|
||||||
|
delete(d.recipeMap, oldestVersion)
|
||||||
|
}
|
||||||
|
|
||||||
|
d.recipeMap[version] = RecipeWithTimeStamps{
|
||||||
|
Recipe: *d.currentRecipe,
|
||||||
|
TimeStamps: time.Now().Unix(),
|
||||||
|
}
|
||||||
|
|
||||||
|
return *d.currentRecipe
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Data) GetRecipe01() []models.Recipe01 {
|
func (d *Data) GetRecipe01() []models.Recipe01 {
|
||||||
return d.recipe.Recipe01
|
return d.currentRecipe.Recipe01
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Data) GetMaterialSetting(version string) []models.MaterialSetting {
|
func (d *Data) GetMaterialSetting(version string) []models.MaterialSetting {
|
||||||
result := make([]models.MaterialSetting, 0)
|
result := make([]models.MaterialSetting, 0)
|
||||||
|
|
||||||
if version == "" || version == d.CurrentVersion {
|
if version == "" || version == d.CurrentVersion {
|
||||||
copy(result, d.recipe.MaterialSetting)
|
copy(result, d.currentRecipe.MaterialSetting)
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
if recipe, ok := d.recipeMap[version]; ok {
|
||||||
|
copy(result, recipe.Recipe.MaterialSetting)
|
||||||
|
d.CurrentVersion = version
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
d.CurrentVersion = version
|
d.CurrentVersion = version
|
||||||
d.recipe = readFile(version)
|
d.currentRecipe = readFile(version)
|
||||||
copy(result, d.recipe.MaterialSetting)
|
|
||||||
|
// save to map
|
||||||
|
if len(d.recipeMap) > 5 { // limit keep in memory 5 version
|
||||||
|
// remove oldest version
|
||||||
|
var oldestVersion string
|
||||||
|
var oldestTime int64
|
||||||
|
for k, v := range d.recipeMap {
|
||||||
|
if oldestTime == 0 || v.TimeStamps < oldestTime {
|
||||||
|
oldestTime = v.TimeStamps
|
||||||
|
oldestVersion = k
|
||||||
|
}
|
||||||
|
}
|
||||||
|
delete(d.recipeMap, oldestVersion)
|
||||||
|
}
|
||||||
|
|
||||||
|
d.recipeMap[version] = RecipeWithTimeStamps{
|
||||||
|
Recipe: *d.currentRecipe,
|
||||||
|
TimeStamps: time.Now().Unix(),
|
||||||
|
}
|
||||||
|
|
||||||
|
copy(result, d.currentRecipe.MaterialSetting)
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -108,11 +175,34 @@ func (d *Data) GetMaterialCode(ids []uint64, version string) []models.MaterialCo
|
||||||
var result []models.MaterialCode
|
var result []models.MaterialCode
|
||||||
|
|
||||||
if version == "" || version == d.CurrentVersion {
|
if version == "" || version == d.CurrentVersion {
|
||||||
result = d.recipe.MaterialCode
|
result = d.currentRecipe.MaterialCode
|
||||||
|
} else if recipe, ok := d.recipeMap[version]; ok {
|
||||||
|
d.CurrentVersion = version
|
||||||
|
return recipe.Recipe.MaterialCode
|
||||||
} else {
|
} else {
|
||||||
d.CurrentVersion = version
|
d.CurrentVersion = version
|
||||||
d.recipe = readFile(version)
|
d.currentRecipe = readFile(version)
|
||||||
result = d.recipe.MaterialCode
|
|
||||||
|
// save to map
|
||||||
|
if len(d.recipeMap) > 5 { // limit keep in memory 5 version
|
||||||
|
// remove oldest version
|
||||||
|
var oldestVersion string
|
||||||
|
var oldestTime int64
|
||||||
|
for k, v := range d.recipeMap {
|
||||||
|
if oldestTime == 0 || v.TimeStamps < oldestTime {
|
||||||
|
oldestTime = v.TimeStamps
|
||||||
|
oldestVersion = k
|
||||||
|
}
|
||||||
|
}
|
||||||
|
delete(d.recipeMap, oldestVersion)
|
||||||
|
}
|
||||||
|
|
||||||
|
d.recipeMap[version] = RecipeWithTimeStamps{
|
||||||
|
Recipe: *d.currentRecipe,
|
||||||
|
TimeStamps: time.Now().Unix(),
|
||||||
|
}
|
||||||
|
|
||||||
|
result = d.currentRecipe.MaterialCode
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(ids) == 0 {
|
if len(ids) == 0 {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue