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
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
28 additions
and
58 deletions
+28
-58
EsClient.go
es/EsClient.go
+7
-3
EsLogger.go
es/EsLogger.go
+5
-5
MysqlClient.go
mysqlrym/MysqlClient.go
+4
-42
RedisClient.go
redis/RedisClient.go
+12
-8
No files found.
es/EsClient.go
View file @
cb5e7729
...
...
@@ -14,17 +14,17 @@ func TestLog() {
log
.
Info
(
"Es 测试"
)
}
var
C
lient
*
elastic
.
Client
var
c
lient
*
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
)
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
{
log
.
Error
(
fmt
.
Sprintf
(
"Elasticsearch connect error:%s"
,
err
.
Error
()))
}
else
{
info
,
code
,
err
:=
C
lient
.
Ping
(
host
)
.
Do
(
context
.
Background
())
info
,
code
,
err
:=
c
lient
.
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
}
es/EsLogger.go
View file @
cb5e7729
...
...
@@ -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
)
...
...
mysqlrym/MysqlClient.go
View file @
cb5e7729
...
...
@@ -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
()
{
...
...
redis/RedisClient.go
View file @
cb5e7729
...
...
@@ -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
))
}
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