Skip to content

Commit

Permalink
fix moex caching
Browse files Browse the repository at this point in the history
  • Loading branch information
kiberdruzhinnik committed Dec 16, 2023
1 parent 8b4bfc4 commit bbe11dc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
23 changes: 15 additions & 8 deletions api/moex.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (api *MoexAPI) GetTicker(ticker string) (HistoryEntries, error) {
log.Println(err)
return HistoryEntries{}, err
}
if len(entryHistory) == 0 {
if len(entryHistory) == 0 || len(entryHistory) != PAGE_SIZE {
break
}
offset += PAGE_SIZE
Expand Down Expand Up @@ -167,9 +167,10 @@ func (api *MoexAPI) getSecurityHistoryOffsetFromCache(key string) (HistoryEntrie
return moexHistory, nil
}

func (api *MoexAPI) setSecurityHistoryOffsetToCache(key string, value HistoryEntries) error {
log.Printf("Saving history data to cache for %s\n", key)
return api.Redis.Client.Set(api.Redis.Context, key, value, 0).Err()
func (api *MoexAPI) setSecurityHistoryOffsetToCache(key string,
value HistoryEntries, duration time.Duration) error {
log.Printf("Saving history data to cache for %s for %d seconds\n", key, uint64(duration.Seconds()))
return api.Redis.Client.Set(api.Redis.Context, key, value, duration).Err()
}

func (api *MoexAPI) getSecurityHistoryOffset(ticker string,
Expand Down Expand Up @@ -237,11 +238,17 @@ func (api *MoexAPI) getSecurityHistoryOffset(ticker string,
}

if api.Redis.Client != nil {
var duration time.Duration
if len(moexHistory)%PAGE_SIZE == 0 {
err = api.setSecurityHistoryOffsetToCache(cacheKey, moexHistory)
if err != nil {
return HistoryEntries{}, err
}
// forever
duration = time.Duration(0)

} else {
duration = time.Hour * 3
}
err = api.setSecurityHistoryOffsetToCache(cacheKey, moexHistory, duration)
if err != nil {
return HistoryEntries{}, err
}
}

Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ func init() {
redisClient = client
}

loggingEnabled := os.Getenv("EXCHANGE_API_VERBOSE")
if len(loggingEnabled) == 0 {
loggingDisabled := os.Getenv("GIN_MODE") == "release"
if loggingDisabled {
log.SetOutput(io.Discard)
} else {
log.Println("Verbose logging enabled")
Expand Down

0 comments on commit bbe11dc

Please sign in to comment.