-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/stdevAntiD2ta_t295_Slow_load_tim…
…es' into stdevAntiD2ta_t295_Slow_load_times
- Loading branch information
Showing
11 changed files
with
116 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package util | ||
|
||
import ( | ||
"github.com/fibercrypto/fibercryptowallet/src/errors" | ||
"time" | ||
) | ||
|
||
// BalanceSnapshot act as account balances cache | ||
type BalanceSnapshot struct { | ||
cache map[string]uint64 | ||
lastUpdate int64 | ||
updateInterval int64 | ||
} | ||
|
||
const defaultUpdateInterval = 10000000 | ||
|
||
// NewBalanceSnapshot new balance snapshot with validity timeout | ||
func NewBalanceSnapshot(updInterval int64) *BalanceSnapshot { | ||
if updInterval <= 0 { | ||
updInterval = defaultUpdateInterval | ||
} | ||
return &BalanceSnapshot{ | ||
cache: make(map[string]uint64), | ||
lastUpdate: 0, | ||
updateInterval: updInterval, | ||
} | ||
} | ||
|
||
// SetCoins update balance for a given coin token | ||
func (bs *BalanceSnapshot) SetCoins(ticker string, coins uint64) { | ||
bs.cache[ticker] = coins | ||
bs.lastUpdate = time.Now().UnixNano() | ||
} | ||
|
||
// GetCoins retrieve balance for a given coin ticker | ||
func (bs *BalanceSnapshot) GetCoins(ticker string) (uint64, error) { | ||
if coins, hasCoins := bs.cache[ticker]; hasCoins { | ||
return coins, nil | ||
} | ||
return 0, errors.ErrKeyNotFound | ||
} | ||
|
||
func (bs *BalanceSnapshot) IsUpdated() bool { | ||
return time.Now().UnixNano()-bs.lastUpdate < bs.updateInterval | ||
} |