redisclientmanager.go 673 B

1234567891011121314151617181920212223242526272829303132333435
  1. package redis
  2. import (
  3. "io"
  4. red "github.com/go-redis/redis"
  5. "github.com/tal-tech/go-zero/core/syncx"
  6. )
  7. const (
  8. defaultDatabase = 0
  9. maxRetries = 3
  10. idleConns = 8
  11. )
  12. var clientManager = syncx.NewResourceManager()
  13. func getClient(server, pass string) (*red.Client, error) {
  14. val, err := clientManager.GetResource(server, func() (io.Closer, error) {
  15. store := red.NewClient(&red.Options{
  16. Addr: server,
  17. Password: pass,
  18. DB: defaultDatabase,
  19. MaxRetries: maxRetries,
  20. MinIdleConns: idleConns,
  21. })
  22. store.WrapProcess(process)
  23. return store, nil
  24. })
  25. if err != nil {
  26. return nil, err
  27. }
  28. return val.(*red.Client), nil
  29. }