You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.1 KiB

3 years ago
3 years ago
  1. package global
  2. import (
  3. "sync"
  4. "github.com/flipped-aurora/gin-vue-admin/server/utils/timer"
  5. "github.com/songzhibin97/gkit/cache/local_cache"
  6. "golang.org/x/sync/singleflight"
  7. "go.uber.org/zap"
  8. "github.com/flipped-aurora/gin-vue-admin/server/config"
  9. "github.com/go-redis/redis/v8"
  10. "github.com/spf13/viper"
  11. "gorm.io/gorm"
  12. )
  13. var (
  14. GVA_DB *gorm.DB
  15. GVA_DBList map[string]*gorm.DB
  16. GVA_REDIS *redis.Client
  17. GVA_CONFIG config.Server
  18. GVA_VP *viper.Viper
  19. //GVA_LOG *oplogging.Logger
  20. GVA_LOG *zap.Logger
  21. GVA_Timer timer.Timer = timer.NewTimerTask()
  22. GVA_Concurrency_Control = &singleflight.Group{}
  23. BlackCache local_cache.Cache
  24. lock sync.RWMutex
  25. )
  26. // GetGlobalDBByDBName 通过名称获取db list中的db
  27. func GetGlobalDBByDBName(dbname string) *gorm.DB {
  28. lock.RLock()
  29. defer lock.RUnlock()
  30. return GVA_DBList[dbname]
  31. }
  32. // MustGetGlobalDBByDBName 通过名称获取db 如果不存在则panic
  33. func MustGetGlobalDBByDBName(dbname string) *gorm.DB {
  34. lock.RLock()
  35. defer lock.RUnlock()
  36. db, ok := GVA_DBList[dbname]
  37. if !ok || db == nil {
  38. panic("db no init")
  39. }
  40. return db
  41. }