gredis.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  1. // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. // Package gredis provides convenient client for redis server.
  7. //
  8. // Redis Client.
  9. //
  10. // Redis Commands Official: https://redis.io/commands
  11. //
  12. // Redis Chinese Documentation: http://redisdoc.com/
  13. package gredis
  14. // New creates and returns a redis client.
  15. // It creates a default redis adapter of go-redis.
  16. func New(config ...*Config) (*Redis, error) {
  17. if len(config) > 0 && config[0] != nil {
  18. // Redis client with go redis implements adapter from given configuration.
  19. return &Redis{adapter: NewAdapterGoRedis(config[0])}, nil
  20. }
  21. // Redis client with go redis implements adapter from package configuration.
  22. if configFromGlobal, ok := GetConfig(); ok {
  23. return &Redis{adapter: NewAdapterGoRedis(configFromGlobal)}, nil
  24. }
  25. // Redis client with empty adapter.
  26. return &Redis{}, nil
  27. }
  28. // NewWithAdapter creates and returns a redis client with given adapter.
  29. func NewWithAdapter(adapter Adapter) *Redis {
  30. return &Redis{adapter: adapter}
  31. }