Commit cb5e7729 by zhengqiuyun86

初始化

parent ee1c6e89
...@@ -14,17 +14,17 @@ func TestLog() { ...@@ -14,17 +14,17 @@ func TestLog() {
log.Info("Es 测试") log.Info("Es 测试")
} }
var Client *elastic.Client var client *elastic.Client
func Init() { func Init() {
host := fmt.Sprintf("http://%s:%d", conf.Es.ServerHost, conf.Es.ServerPort) host := fmt.Sprintf("http://%s:%d", conf.Es.ServerHost, conf.Es.ServerPort)
var err error var err error
errorLog := _log.New(os.Stdout, "APP", _log.LstdFlags) errorLog := _log.New(os.Stdout, "APP", _log.LstdFlags)
Client, err = elastic.NewClient(elastic.SetErrorLog(errorLog), elastic.SetURL(host)) client, err = elastic.NewClient(elastic.SetErrorLog(errorLog), elastic.SetURL(host))
if err != nil { if err != nil {
log.Error(fmt.Sprintf("Elasticsearch connect error:%s", err.Error())) log.Error(fmt.Sprintf("Elasticsearch connect error:%s", err.Error()))
} else { } else {
info, code, err := Client.Ping(host).Do(context.Background()) info, code, err := client.Ping(host).Do(context.Background())
if err != nil { if err != nil {
panic(err) panic(err)
} }
...@@ -32,3 +32,7 @@ func Init() { ...@@ -32,3 +32,7 @@ func Init() {
log.Info(fmt.Sprintf("Elasticsearch version %s", info.Version.Number)) log.Info(fmt.Sprintf("Elasticsearch version %s", info.Version.Number))
} }
} }
func getRedisClient() *elastic.Client {
return client
}
...@@ -35,7 +35,7 @@ type ServerLogModel struct { ...@@ -35,7 +35,7 @@ type ServerLogModel struct {
func InsertEsLog(logInfo interface{}) { func InsertEsLog(logInfo interface{}) {
indexName := getCurrentIndex() indexName := getCurrentIndex()
put, err := Client.Index(). put, err := getRedisClient().Index().
Index(indexName). Index(indexName).
BodyJson(logInfo). BodyJson(logInfo).
Do(context.Background()) Do(context.Background())
...@@ -59,7 +59,7 @@ func GroupField(indexName, field string, size, minCount int, excludeValues []str ...@@ -59,7 +59,7 @@ func GroupField(indexName, field string, size, minCount int, excludeValues []str
queryByte, _ := json.Marshal(source) queryByte, _ := json.Marshal(source)
log.Info(fmt.Sprintf("查询条件:%s", string(queryByte))) log.Info(fmt.Sprintf("查询条件:%s", string(queryByte)))
} }
res, err := Client.Search(indexName). res, err := getRedisClient().Search(indexName).
Aggregation("group_field", aggregationQuery). Aggregation("group_field", aggregationQuery).
From(0). From(0).
Size(0). Size(0).
...@@ -82,7 +82,7 @@ func QueryLogs(indexName, filed, value string, from, size int) interface{} { ...@@ -82,7 +82,7 @@ func QueryLogs(indexName, filed, value string, from, size int) interface{} {
size = 10 size = 10
} }
query := elastic.NewBoolQuery().Must(elastic.NewTermQuery(filed, value)) query := elastic.NewBoolQuery().Must(elastic.NewTermQuery(filed, value))
res, err := Client.Search(indexName).Query(query). res, err := getRedisClient().Search(indexName).Query(query).
From(from). From(from).
Size(size). Size(size).
Do(context.Background()) Do(context.Background())
...@@ -99,7 +99,7 @@ type IndexInfo struct { ...@@ -99,7 +99,7 @@ type IndexInfo struct {
} }
func AllIndex() interface{} { func AllIndex() interface{} {
res, err := Client.CatIndices().Do(context.Background()) res, err := getRedisClient().CatIndices().Do(context.Background())
if err != nil { if err != nil {
exception.ThrowsErr(err) exception.ThrowsErr(err)
} }
...@@ -118,7 +118,7 @@ func AllIndex() interface{} { ...@@ -118,7 +118,7 @@ func AllIndex() interface{} {
} }
func DeleteIndex(indexName string) interface{} { func DeleteIndex(indexName string) interface{} {
res, err := Client.DeleteIndex(indexName). res, err := getRedisClient().DeleteIndex(indexName).
Do(context.Background()) Do(context.Background())
if err != nil { if err != nil {
exception.ThrowsErr(err) exception.ThrowsErr(err)
......
...@@ -22,8 +22,6 @@ func TestLog() { ...@@ -22,8 +22,6 @@ func TestLog() {
var customerDb *gorm.DB var customerDb *gorm.DB
var customerDbMap sync.Map
var gidNeedTX sync.Map var gidNeedTX sync.Map
func SetTx() { func SetTx() {
...@@ -32,24 +30,18 @@ func SetTx() { ...@@ -32,24 +30,18 @@ func SetTx() {
} }
func GetDb() *gorm.DB { func GetDb() *gorm.DB {
return getDb(customerDb, &customerDbMap) return getDb(customerDb)
} }
func getDb(db *gorm.DB, dbMap *sync.Map) *gorm.DB { func getDb(db *gorm.DB) *gorm.DB {
gid := util.GetGID() gid := util.GetGID()
_, needTx := gidNeedTX.Load(gid) _, needTx := gidNeedTX.Load(gid)
if needTx { if needTx {
cacheTx, ok := dbMap.Load(gid)
if ok {
return cacheTx.(*gorm.DB)
} else {
if conf.LogShowSql { if conf.LogShowSql {
db = db.Debug() db = db.Debug()
} }
tx := db.Begin() tx := db.Begin()
dbMap.Store(gid, tx)
return tx return tx
}
} else { } else {
if conf.LogShowSql { if conf.LogShowSql {
db = db.Debug() db = db.Debug()
...@@ -59,41 +51,11 @@ func getDb(db *gorm.DB, dbMap *sync.Map) *gorm.DB { ...@@ -59,41 +51,11 @@ func getDb(db *gorm.DB, dbMap *sync.Map) *gorm.DB {
} }
func Rollback() { func Rollback() {
gid := util.GetGID() customerDb.Rollback()
_, needTx := gidNeedTX.Load(gid)
if needTx {
rollbackTx(gid, &customerDbMap)
gidNeedTX.Delete(gid)
}
} }
func Commit() { func Commit() {
gid := util.GetGID() customerDb.Commit()
_, needTx := gidNeedTX.Load(gid)
if needTx {
commitTx(gid, &customerDbMap)
gidNeedTX.Delete(gid)
}
}
func rollbackTx(gid uint64, dbMap *sync.Map) {
cacheTx, ok := dbMap.Load(gid)
if ok {
tx := cacheTx.(*gorm.DB)
tx.Rollback()
dbMap.Delete(gid)
}
}
func commitTx(gid uint64, dbMap *sync.Map) {
cacheTx, ok := dbMap.Load(gid)
if ok {
tx := cacheTx.(*gorm.DB)
tx.Commit()
dbMap.Delete(gid)
}
} }
func Init() { func Init() {
......
...@@ -30,23 +30,27 @@ func Init() { ...@@ -30,23 +30,27 @@ func Init() {
return return
} }
func getDb() *redis.Client {
return redisClient
}
func Incr(key string) int64 { func Incr(key string) int64 {
r := redisClient.Incr(key) r := getDb().Incr(key)
return r.Val() return r.Val()
} }
func IncrBy(key string, n int64) int64 { func IncrBy(key string, n int64) int64 {
r := redisClient.IncrBy(key, n) r := getDb().IncrBy(key, n)
return r.Val() return r.Val()
} }
func DecrBy(key string, n int64) int64 { func DecrBy(key string, n int64) int64 {
r := redisClient.DecrBy(key, n) r := getDb().DecrBy(key, n)
return r.Val() return r.Val()
} }
func Exists(key string) bool { func Exists(key string) bool {
r := redisClient.Exists(key) r := getDb().Exists(key)
return r.Val() > 0 return r.Val() > 0
} }
...@@ -58,12 +62,12 @@ func SwitchOpen(key string) bool { ...@@ -58,12 +62,12 @@ func SwitchOpen(key string) bool {
} }
func GetString(key string) string { func GetString(key string) string {
r := redisClient.Get(key) r := getDb().Get(key)
return r.Val() return r.Val()
} }
func Get(key string) []byte { func Get(key string) []byte {
r := redisClient.Get(key) r := getDb().Get(key)
rByte, err := r.Bytes() rByte, err := r.Bytes()
if err != nil { if err != nil {
log.Error(fmt.Sprintf("获取缓存错误:%s", err.Error())) log.Error(fmt.Sprintf("获取缓存错误:%s", err.Error()))
...@@ -72,11 +76,11 @@ func Get(key string) []byte { ...@@ -72,11 +76,11 @@ func Get(key string) []byte {
} }
func Set(k string, v string) { func Set(k string, v string) {
redisClient.Set(k, v, 0) getDb().Set(k, v, 0)
log.Debug(fmt.Sprintf("设置缓存成功:%s %s", k, v)) log.Debug(fmt.Sprintf("设置缓存成功:%s %s", k, v))
} }
func Expire(k string, t int64) { func Expire(k string, t int64) {
redisClient.Expire(k, time.Duration(t)) getDb().Expire(k, time.Duration(t))
log.Debug(fmt.Sprintf("设置缓存过期成功:%s %d", k, t)) log.Debug(fmt.Sprintf("设置缓存过期成功:%s %d", k, t))
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment