forked from saniales/golang-crypto-trading-bot
/
cache.go
100 lines (86 loc) · 2.94 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package exchanges
import (
"sync"
"github.com/mcwarner5/BlockBot8000/environment"
)
// SummaryCache represents a local summary cache for every exchange. To allow dinamic polling from multiple sources (REST + Websocket)
type SummaryCache struct {
mutex *sync.RWMutex
internal map[*environment.Market]*environment.MarketSummary
}
// NewSummaryCache creates a new SummaryCache Object
func NewSummaryCache() *SummaryCache {
return &SummaryCache{
mutex: &sync.RWMutex{},
internal: make(map[*environment.Market]*environment.MarketSummary),
}
}
// Set sets a value for the specified key.
func (sc *SummaryCache) Set(market *environment.Market, summary *environment.MarketSummary) *environment.MarketSummary {
sc.mutex.Lock()
old := sc.internal[market]
sc.internal[market] = summary
sc.mutex.Unlock()
return old
}
// Get gets the value for the specified key.
func (sc *SummaryCache) Get(market *environment.Market) (*environment.MarketSummary, bool) {
sc.mutex.RLock()
ret, isSet := sc.internal[market]
sc.mutex.RUnlock()
return ret, isSet
}
// CandlesCache represents a local candles cache for every exchange. To allow dinamic polling from multiple sources (REST + Websocket)
type CandlesCache struct {
mutex *sync.RWMutex
internal map[*environment.Market][]environment.CandleStick
}
// NewCandlesCache creates a new CandlesCache Object
func NewCandlesCache() *CandlesCache {
return &CandlesCache{
mutex: &sync.RWMutex{},
internal: make(map[*environment.Market][]environment.CandleStick),
}
}
// Set sets a value for the specified key.
func (cc *CandlesCache) Set(market *environment.Market, candles []environment.CandleStick) []environment.CandleStick {
cc.mutex.Lock()
old := cc.internal[market]
cc.internal[market] = candles
cc.mutex.Unlock()
return old
}
// Get gets the value for the specified key.
func (cc *CandlesCache) Get(market *environment.Market) ([]environment.CandleStick, bool) {
cc.mutex.RLock()
ret, isSet := cc.internal[market]
cc.mutex.RUnlock()
return ret, isSet
}
// OrderbookCache represents a local orderbook cache for every exchange. To allow dinamic polling from multiple sources (REST + Websocket)
type OrderbookCache struct {
mutex *sync.RWMutex
internal map[*environment.Market]*environment.OrderBook
}
// NewOrderbookCache creates a new OrderbookCache Object
func NewOrderbookCache() *OrderbookCache {
return &OrderbookCache{
mutex: &sync.RWMutex{},
internal: make(map[*environment.Market]*environment.OrderBook),
}
}
// Set sets a value for the specified key.
func (cc *OrderbookCache) Set(market *environment.Market, book *environment.OrderBook) *environment.OrderBook {
cc.mutex.Lock()
old := cc.internal[market]
cc.internal[market] = book
cc.mutex.Unlock()
return old
}
// Get gets the value for the specified key.
func (cc *OrderbookCache) Get(market *environment.Market) (*environment.OrderBook, bool) {
cc.mutex.RLock()
ret, isSet := cc.internal[market]
cc.mutex.RUnlock()
return ret, isSet
}