com_caches_redis.go 752 B

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