channel.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package configure
  2. import (
  3. "fmt"
  4. "github.com/gwuhaolin/livego/utils/uid"
  5. "github.com/go-redis/redis/v7"
  6. "github.com/patrickmn/go-cache"
  7. log "github.com/sirupsen/logrus"
  8. )
  9. type RoomKeysType struct {
  10. redisCli *redis.Client
  11. localCache *cache.Cache
  12. }
  13. var RoomKeys = &RoomKeysType{
  14. localCache: cache.New(cache.NoExpiration, 0),
  15. }
  16. var saveInLocal = true
  17. func Init() {
  18. saveInLocal = len(Config.GetString("redis_addr")) == 0
  19. if saveInLocal {
  20. return
  21. }
  22. RoomKeys.redisCli = redis.NewClient(&redis.Options{
  23. Addr: Config.GetString("redis_addr"),
  24. Password: Config.GetString("redis_pwd"),
  25. DB: 0,
  26. })
  27. _, err := RoomKeys.redisCli.Ping().Result()
  28. if err != nil {
  29. log.Panic("Redis: ", err)
  30. }
  31. log.Info("Redis connected")
  32. }
  33. // set/reset a random key for channel
  34. func (r *RoomKeysType) SetKey(channel string) (key string, err error) {
  35. if !saveInLocal {
  36. for {
  37. key = uid.RandStringRunes(48)
  38. if _, err = r.redisCli.Get(key).Result(); err == redis.Nil {
  39. err = r.redisCli.Set(channel, key, 0).Err()
  40. if err != nil {
  41. return
  42. }
  43. err = r.redisCli.Set(key, channel, 0).Err()
  44. return
  45. } else if err != nil {
  46. return
  47. }
  48. }
  49. }
  50. for {
  51. key = uid.RandStringRunes(48)
  52. if _, found := r.localCache.Get(key); !found {
  53. r.localCache.SetDefault(channel, key)
  54. r.localCache.SetDefault(key, channel)
  55. break
  56. }
  57. }
  58. return
  59. }
  60. func (r *RoomKeysType) GetKey(channel string) (newKey string, err error) {
  61. if !saveInLocal {
  62. if newKey, err = r.redisCli.Get(channel).Result(); err == redis.Nil {
  63. newKey, err = r.SetKey(channel)
  64. log.Debugf("[KEY] new channel [%s]: %s", channel, newKey)
  65. return
  66. }
  67. return
  68. }
  69. var key interface{}
  70. var found bool
  71. if key, found = r.localCache.Get(channel); found {
  72. return key.(string), nil
  73. }
  74. newKey, err = r.SetKey(channel)
  75. log.Debugf("[KEY] new channel [%s]: %s", channel, newKey)
  76. return
  77. }
  78. func (r *RoomKeysType) GetChannel(key string) (channel string, err error) {
  79. if !saveInLocal {
  80. return r.redisCli.Get(key).Result()
  81. }
  82. chann, found := r.localCache.Get(key)
  83. if found {
  84. return chann.(string), nil
  85. } else {
  86. return "", fmt.Errorf("%s does not exists", key)
  87. }
  88. }
  89. func (r *RoomKeysType) DeleteChannel(channel string) bool {
  90. if !saveInLocal {
  91. return r.redisCli.Del(channel).Err() != nil
  92. }
  93. key, ok := r.localCache.Get(channel)
  94. if ok {
  95. r.localCache.Delete(channel)
  96. r.localCache.Delete(key.(string))
  97. return true
  98. }
  99. return false
  100. }
  101. func (r *RoomKeysType) DeleteKey(key string) bool {
  102. if !saveInLocal {
  103. return r.redisCli.Del(key).Err() != nil
  104. }
  105. channel, ok := r.localCache.Get(key)
  106. if ok {
  107. r.localCache.Delete(channel.(string))
  108. r.localCache.Delete(key)
  109. return true
  110. }
  111. return false
  112. }