Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
R
rym-util
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
zhengqiuyun
rym-util
Commits
cb5e7729
Commit
cb5e7729
authored
Aug 15, 2022
by
zhengqiuyun86
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
初始化
parent
ee1c6e89
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
32 additions
and
62 deletions
+32
-62
EsClient.go
es/EsClient.go
+7
-3
EsLogger.go
es/EsLogger.go
+5
-5
MysqlClient.go
mysqlrym/MysqlClient.go
+8
-46
RedisClient.go
redis/RedisClient.go
+12
-8
No files found.
es/EsClient.go
View file @
cb5e7729
...
@@ -14,17 +14,17 @@ func TestLog() {
...
@@ -14,17 +14,17 @@ func TestLog() {
log
.
Info
(
"Es 测试"
)
log
.
Info
(
"Es 测试"
)
}
}
var
C
lient
*
elastic
.
Client
var
c
lient
*
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
)
C
lient
,
err
=
elastic
.
NewClient
(
elastic
.
SetErrorLog
(
errorLog
),
elastic
.
SetURL
(
host
))
c
lient
,
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
:=
C
lient
.
Ping
(
host
)
.
Do
(
context
.
Background
())
info
,
code
,
err
:=
c
lient
.
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
}
es/EsLogger.go
View file @
cb5e7729
...
@@ -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
)
...
...
mysqlrym/MysqlClient.go
View file @
cb5e7729
...
@@ -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
conf
.
LogShowSql
{
if
ok
{
db
=
db
.
Debug
()
return
cacheTx
.
(
*
gorm
.
DB
)
}
else
{
if
conf
.
LogShowSql
{
db
=
db
.
Debug
()
}
tx
:=
db
.
Begin
()
dbMap
.
Store
(
gid
,
tx
)
return
tx
}
}
tx
:=
db
.
Begin
()
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
()
{
...
...
redis/RedisClient.go
View file @
cb5e7729
...
@@ -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
))
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment