mirror of
https://github.com/FlourishingWorld/hk4e.git
synced 2026-02-04 14:22:26 +08:00
61 lines
1.4 KiB
Go
61 lines
1.4 KiB
Go
package dao
|
|
|
|
import (
|
|
"context"
|
|
|
|
"hk4e/common/config"
|
|
"hk4e/pkg/logger"
|
|
|
|
"github.com/go-redis/redis/v8"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
"go.mongodb.org/mongo-driver/mongo/readpref"
|
|
)
|
|
|
|
type Dao struct {
|
|
mongo *mongo.Client
|
|
db *mongo.Database
|
|
redis *redis.Client
|
|
}
|
|
|
|
func NewDao() (r *Dao, err error) {
|
|
r = new(Dao)
|
|
clientOptions := options.Client().ApplyURI(config.GetConfig().Database.Url).SetMinPoolSize(1).SetMaxPoolSize(10)
|
|
client, err := mongo.Connect(context.TODO(), clientOptions)
|
|
if err != nil {
|
|
logger.Error("mongo connect error: %v", err)
|
|
return nil, err
|
|
}
|
|
err = client.Ping(context.TODO(), readpref.Primary())
|
|
if err != nil {
|
|
logger.Error("mongo ping error: %v", err)
|
|
return nil, err
|
|
}
|
|
r.mongo = client
|
|
r.db = client.Database("gs_hk4e")
|
|
r.redis = redis.NewClient(&redis.Options{
|
|
Addr: config.GetConfig().Redis.Addr,
|
|
Password: config.GetConfig().Redis.Password,
|
|
DB: 0,
|
|
PoolSize: 10,
|
|
MinIdleConns: 1,
|
|
})
|
|
err = r.redis.Ping(context.TODO()).Err()
|
|
if err != nil {
|
|
logger.Error("redis ping error: %v", err)
|
|
return nil, err
|
|
}
|
|
return r, nil
|
|
}
|
|
|
|
func (d *Dao) CloseDao() {
|
|
err := d.mongo.Disconnect(context.TODO())
|
|
if err != nil {
|
|
logger.Error("mongo close error: %v", err)
|
|
}
|
|
err = d.redis.Close()
|
|
if err != nil {
|
|
logger.Error("redis close error: %v", err)
|
|
}
|
|
}
|