-
Notifications
You must be signed in to change notification settings - Fork 182
/
cache.go
52 lines (44 loc) · 1.48 KB
/
cache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package websocket
import (
"sync"
"github.com/tendermint/tendermint/libs/log"
sdk "github.com/cosmos/cosmos-sdk/types"
pushservice "github.com/okex/okexchain/x/stream/pushservice/types"
"github.com/okex/okexchain/x/stream/types"
)
type cache struct {
depthBooksMap map[string]pushservice.BookRes
lock sync.RWMutex
}
var (
singletonCache *cache
once sync.Once
)
func InitialCache(ctx sdk.Context, orderKeeper types.OrderKeeper, dexKeeper types.DexKeeper, logger log.Logger) {
once.Do(func() {
size := 200
tokenPairs := dexKeeper.GetTokenPairs(ctx)
logger.Debug("initial websocket cache", "tokenPairs", tokenPairs)
depthBooksMap := make(map[string]pushservice.BookRes, len(tokenPairs))
for _, tokenPair := range tokenPairs {
depthBook := orderKeeper.GetDepthBookCopy(tokenPair.Name())
bookRes := pushservice.ConvertBookRes(tokenPair.Name(), orderKeeper, depthBook, size)
depthBooksMap[tokenPair.Name()] = bookRes
}
logger.Debug("initial websocket cache", "depthbook", depthBooksMap)
singletonCache = &cache{
depthBooksMap: depthBooksMap,
}
})
}
func GetDepthBookFromCache(product string) (depthBook pushservice.BookRes, ok bool) {
singletonCache.lock.RLock()
defer singletonCache.lock.RUnlock()
depthBook, ok = singletonCache.depthBooksMap[product]
return
}
func UpdateDepthBookCache(product string, bookRes pushservice.BookRes) {
singletonCache.lock.Lock()
defer singletonCache.lock.Unlock()
singletonCache.depthBooksMap[product] = bookRes
}