status-go/services/wallet/market/market_cache.go
Sean Hagstrom 385933a60d
feat: fallback to cache for prices and token market values when calling GetWalletToken (#5832)
This change adapts the market manager to cache the token-market-cap, and modifies the existing `GetWalletToken` function to use the token-price and token-market cache data. Additionally, we choose to use the cached price and market data for roughly 60 seconds when calling the `GetWalletToken` function.
2024-09-20 12:24:43 +01:00

36 lines
658 B
Go

package market
import (
"sync"
)
type MarketCache[T any] struct {
store T
lock sync.RWMutex
}
func NewCache[T any](store T) *MarketCache[T] {
var cache MarketCache[T]
cache.store = store
return &cache
}
func Read[T any, R any](cache *MarketCache[T], reader func(store T) R) R {
cache.lock.RLock()
defer cache.lock.RUnlock()
return reader(cache.store)
}
func Write[T any](cache *MarketCache[T], writer func(store T) T) *MarketCache[T] {
cache.lock.Lock()
defer cache.lock.Unlock()
cache.store = writer(cache.store)
return cache
}
func (cache *MarketCache[T]) Get() T {
cache.lock.RLock()
defer cache.lock.RUnlock()
return cache.store
}