package services import ( "app/commons/core/exchange" "app/commons/core/redisclient" "fmt" "github.com/shopspring/decimal" "strings" "time" ) // tips: // 内存中获取常用数据 // 通用于接口数据 对外部返回数据可持久化到redis 重启服务不受影响 // 比如 币种单价 外部系统信息等等 // todo: 获取币种单价 func (s *CommonService) GetSymbolPriceFromRedis(symbol string) (decimal.Decimal, error) { price := decimal.Zero cacheKey := fmt.Sprintf("symbol:price:%s", strings.ToUpper(symbol)) cacheTime := time.Second * 5 err := redisclient.GetJsonDataFromCacheOrDB(cacheKey, cacheTime, &price, func() (interface{}, error) { return exchange.GetCurrentSymbolUsdPrice(symbol) }) return price, err }