Skip to content

Commit

Permalink
fix: change lock mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
hblab-ngocnd committed Aug 30, 2022
1 parent 2d45b60 commit 123b649
Showing 1 changed file with 2 additions and 4 deletions.
6 changes: 2 additions & 4 deletions usecase/dictionary.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ type DictUseCase interface {
func (u *dictUseCase) GetDict(ctx context.Context, start, pageSize int, notCache, level, pwd string) ([]models.Word, error) {
if notCache != "true" && u.cacheData != nil && u.cacheData[level] != nil && len(u.cacheData[level]) > 0 {
log.Println("use data from cache")
u.mu.Lock()
defer u.mu.Unlock()
if start > len(u.cacheData[level]) {
start = len(u.cacheData[level])
}
Expand All @@ -57,19 +55,19 @@ func (u *dictUseCase) GetDict(ctx context.Context, start, pageSize int, notCache
}
}
log.Println("use data from source")
u.mu.Lock()
defer u.mu.Unlock()
url := fmt.Sprintf("https://japanesetest4you.com/jlpt-%s-vocabulary-list/", level)
data, err := u.dictionaryService.GetDictionary(ctx, url)
if err != nil {
log.Print(err)
return nil, err
}
data = services.CompositeWordData(data, u.translateService.TranslateData(ctx, services.MakeTransDataFromWord(data)))
u.mu.Lock()
if u.cacheData == nil {
u.cacheData = make(map[string][]models.Word)
}
u.cacheData[level] = data
u.mu.Unlock()
if start > len(data) {
start = len(data)
}
Expand Down

0 comments on commit 123b649

Please sign in to comment.