rediscli.go 968 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package rediscli
  2. import (
  3. "encoding/hex"
  4. "fmt"
  5. "mtp2_if/config"
  6. "mtp2_if/packet"
  7. "github.com/go-redis/redis"
  8. )
  9. var RedisCli *redis.Client
  10. func InitRedisCli() error {
  11. // 尝试解密
  12. var dbPwd []byte
  13. ciphertext, _ := hex.DecodeString(config.SerCfg.GetRedisPwd())
  14. if len(ciphertext) > 8 {
  15. ciphertext = ciphertext[4 : len(ciphertext)-8]
  16. dbPwd, _ = packet.Decrypt(ciphertext, packet.AESKey, true)
  17. if dbPwd == nil {
  18. dbPwd = []byte(config.SerCfg.GetRedisPwd())
  19. }
  20. } else {
  21. dbPwd = []byte(config.SerCfg.GetRedisPwd())
  22. }
  23. RedisCli = redis.NewClient(&redis.Options{
  24. Addr: config.SerCfg.GetRedisAddress(),
  25. Password: string(dbPwd),
  26. DB: config.SerCfg.GetRedisDbNum(),
  27. PoolSize: config.SerCfg.GetRedisConnNum(),
  28. })
  29. pong, err := RedisCli.Ping().Result()
  30. if err != nil {
  31. fmt.Println(pong, err)
  32. return err
  33. }
  34. return nil
  35. }
  36. func GetRedisClient() *redis.Client {
  37. return RedisCli
  38. }
  39. func CloseReidsClient() {
  40. RedisCli.Close()
  41. }