49 lines
1.1 KiB
Go
49 lines
1.1 KiB
Go
package oracle
|
|
|
|
import (
|
|
"sync"
|
|
)
|
|
|
|
// Store mantém os últimos dados válidos dos oráculos e um pequeno histórico
|
|
|
|
type Store struct {
|
|
mu sync.RWMutex
|
|
latest map[string]*OracleData
|
|
history map[string][]*OracleData
|
|
maxHist int
|
|
}
|
|
|
|
// NewStore cria uma nova instância de armazenamento de oráculos
|
|
func NewStore(maxHistory int) *Store {
|
|
return &Store{
|
|
latest: make(map[string]*OracleData),
|
|
history: make(map[string][]*OracleData),
|
|
maxHist: maxHistory,
|
|
}
|
|
}
|
|
|
|
// Save registra um novo dado e atualiza o histórico
|
|
func (s *Store) Save(data *OracleData) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
|
|
s.latest[data.Key] = data
|
|
s.history[data.Key] = append(s.history[data.Key], data)
|
|
if len(s.history[data.Key]) > s.maxHist {
|
|
s.history[data.Key] = s.history[data.Key][len(s.history[data.Key])-s.maxHist:]
|
|
}
|
|
}
|
|
|
|
// Get retorna o último dado registrado
|
|
func (s *Store) Get(key string) *OracleData {
|
|
s.mu.RLock()
|
|
defer s.mu.RUnlock()
|
|
return s.latest[key]
|
|
}
|
|
|
|
// History retorna o histórico completo de um dado
|
|
func (s *Store) History(key string) []*OracleData {
|
|
s.mu.RLock()
|
|
defer s.mu.RUnlock()
|
|
return s.history[key]
|
|
} |