上外网前准备

This commit is contained in:
flswld
2023-02-03 19:19:18 +08:00
parent dc6c0840fc
commit 34b87fadfc
10 changed files with 120 additions and 14 deletions

View File

@@ -12,7 +12,7 @@ import (
"go.mongodb.org/mongo-driver/mongo"
)
func (d *Dao) GetNextAccountId() (uint64, error) {
func (d *Dao) _GetNextAccountId() (uint64, error) {
db := d.db.Collection("account_id_counter")
find := db.FindOne(context.TODO(), bson.D{{"_id", "default"}})
item := new(model.AccountIDCounter)
@@ -50,7 +50,7 @@ func (d *Dao) GetNextAccountId() (uint64, error) {
return item.AccountID, nil
}
func (d *Dao) GetNextYuanShenUid() (uint64, error) {
func (d *Dao) _GetNextYuanShenUid() (uint64, error) {
db := d.db.Collection("player_id_counter")
find := db.FindOne(context.TODO(), bson.D{{"_id", "default"}})
item := new(model.PlayerIDCounter)

View File

@@ -0,0 +1,46 @@
package dao
import (
"context"
)
const RedisPlayerKeyPrefix = "HK4E"
const (
AccountIdRedisKey = "AccountId"
AccountIdBegin uint64 = 1
YuanShenUidRedisKey = "YuanShenUid"
YuanShenUidBegin uint64 = 100000001
)
func (d *Dao) GetNextAccountId() (uint64, error) {
return d.redisInc(RedisPlayerKeyPrefix + ":" + AccountIdRedisKey)
}
func (d *Dao) GetNextYuanShenUid() (uint64, error) {
return d.redisInc(RedisPlayerKeyPrefix + ":" + YuanShenUidRedisKey)
}
func (d *Dao) redisInc(keyName string) (uint64, error) {
exist, err := d.redis.Exists(context.TODO(), keyName).Result()
if err != nil {
return 0, err
}
if exist == 0 {
var value uint64 = 0
if keyName == AccountIdRedisKey {
value = AccountIdBegin
} else if keyName == YuanShenUidRedisKey {
value = YuanShenUidBegin
}
err := d.redis.Set(context.TODO(), keyName, value, 0).Err()
if err != nil {
return 0, err
}
}
id, err := d.redis.Incr(context.TODO(), keyName).Result()
if err != nil {
return 0, err
}
return uint64(id), nil
}

View File

@@ -6,6 +6,7 @@ import (
"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"
@@ -14,6 +15,7 @@ import (
type Dao struct {
mongo *mongo.Client
db *mongo.Database
redis *redis.Client
}
func NewDao() (r *Dao) {
@@ -31,6 +33,18 @@ func NewDao() (r *Dao) {
}
r.mongo = client
r.db = client.Database("dispatch_hk4e")
r.redis = redis.NewClient(&redis.Options{
Addr: config.CONF.Redis.Addr,
Password: config.CONF.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
}
return r
}
@@ -39,4 +53,8 @@ func (d *Dao) CloseDao() {
if err != nil {
logger.Error("mongo close error: %v", err)
}
err = d.redis.Close()
if err != nil {
logger.Error("redis close error: %v", err)
}
}