Commit cb5e7729 by zhengqiuyun86

初始化

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