2020-12-04 18:30:03 +00:00
|
|
|
package cache
|
2020-11-30 21:38:54 +00:00
|
|
|
|
|
|
|
import (
|
2020-12-04 18:30:03 +00:00
|
|
|
oracleEmitter "github.com/Secured-Finance/dione/contracts/oracleemitter"
|
2020-11-30 21:38:54 +00:00
|
|
|
"github.com/VictoriaMetrics/fastcache"
|
|
|
|
"github.com/fxamacker/cbor/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// in megabytes
|
|
|
|
DefaultEventLogCacheCapacity = 32000000
|
|
|
|
)
|
|
|
|
|
|
|
|
type EventLogCache struct {
|
|
|
|
cache *fastcache.Cache
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewEventLogCache() *EventLogCache {
|
|
|
|
return &EventLogCache{
|
|
|
|
cache: fastcache.New(DefaultEventLogCacheCapacity),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (elc *EventLogCache) Store(key string, event interface{}) error {
|
|
|
|
mRes, err := cbor.Marshal(event)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
elc.cache.SetBig([]byte(key), mRes)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-04 18:30:03 +00:00
|
|
|
func (elc *EventLogCache) GetOracleRequestEvent(key string) (*oracleEmitter.OracleEmitterNewOracleRequest, error) {
|
2020-11-30 21:38:54 +00:00
|
|
|
var mData []byte
|
2020-12-04 18:30:03 +00:00
|
|
|
mData = elc.cache.GetBig(mData, []byte(key))
|
2020-11-30 21:38:54 +00:00
|
|
|
|
2020-12-04 18:30:03 +00:00
|
|
|
var event *oracleEmitter.OracleEmitterNewOracleRequest
|
2020-11-30 21:38:54 +00:00
|
|
|
err := cbor.Unmarshal(mData, &event)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return event, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (elc *EventLogCache) Delete(key string) {
|
|
|
|
elc.cache.Del([]byte(key))
|
|
|
|
}
|