gf/database/gredis/gredis.go

41 lines
1.1 KiB
Go
Raw Normal View History

// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
2018-04-18 18:05:46 +08:00
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
2018-04-18 18:05:46 +08:00
// Package gredis provides convenient client for redis server.
2019-01-16 13:35:16 +08:00
//
2019-05-08 21:03:04 +08:00
// Redis Client.
2019-05-14 22:02:09 +08:00
//
2019-05-08 21:03:04 +08:00
// Redis Commands Official: https://redis.io/commands
2019-05-14 22:02:09 +08:00
//
2019-05-08 21:03:04 +08:00
// Redis Chinese Documentation: http://redisdoc.com/
2018-04-18 18:05:46 +08:00
package gredis
import (
2021-09-23 19:12:02 +08:00
"github.com/gogf/gf/errors/gcode"
"github.com/gogf/gf/errors/gerror"
2018-04-18 18:05:46 +08:00
)
2021-09-26 23:22:32 +08:00
// New creates and returns a redis client.
// It creates a default redis adapter of go-redis.
2021-09-23 19:12:02 +08:00
func New(config ...*Config) (*Redis, error) {
if len(config) > 0 {
2021-09-26 23:22:32 +08:00
return &Redis{adapter: NewAdapterGoRedis(config[0])}, nil
}
2021-09-23 19:12:02 +08:00
configFromGlobal, ok := GetConfig()
if !ok {
return nil, gerror.NewCode(
gcode.CodeMissingConfiguration,
`configuration not found for creating Redis client`,
)
2019-06-19 09:06:52 +08:00
}
2021-09-26 23:22:32 +08:00
return &Redis{adapter: NewAdapterGoRedis(configFromGlobal)}, nil
2018-04-18 18:05:46 +08:00
}
2021-09-26 23:22:32 +08:00
// NewWithAdapter creates and returns a redis client with given adapter.
2021-09-23 19:12:02 +08:00
func NewWithAdapter(adapter Adapter) *Redis {
return &Redis{adapter: adapter}
2021-01-25 21:59:44 +08:00
}