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
339e1d59
Commit
339e1d59
authored
Aug 10, 2022
by
zhengqiuyun86
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
初始化
parent
ad9ce240
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
144 additions
and
144 deletions
+144
-144
conf.go
conf/conf.go
+5
-5
EsClient.go
es/EsClient.go
+12
-8
Response.go
getWay/Response.go
+1
-1
go.mod
go.mod
+33
-4
go.sum
go.sum
+0
-0
main.go
main.go
+13
-0
MysqlClient.go
mysqlrym/MysqlClient.go
+9
-5
Redis.go
redis/Redis.go
+0
-121
RedisClient.go
redis/RedisClient.go
+71
-0
No files found.
conf/conf.go
View file @
339e1d59
...
@@ -16,7 +16,7 @@ type Config struct {
...
@@ -16,7 +16,7 @@ type Config struct {
//EsServerHttp
//EsServerHttp
EsServerHttp
string
EsServerHttp
string
//mysql
//mysql
rym
MysqlConf
MysqlConf
MysqlConf
MysqlConf
}
}
...
@@ -41,17 +41,17 @@ var Env = ""
...
@@ -41,17 +41,17 @@ var Env = ""
//log conf
//log conf
var
LogLevel
=
"DEBUG"
var
LogLevel
=
"DEBUG"
var
LogColorful
=
fals
e
//日志是否开启彩色打印
var
LogColorful
=
tru
e
//日志是否开启彩色打印
var
LogResponseLength
=
10
var
LogResponseLength
=
10
var
LogShowSql
=
true
var
LogShowSql
=
true
//Redis conf
//Redis conf
var
RedisAddr
=
"
to-chengdu-office.168cad.top:50001
"
var
RedisAddr
=
""
var
RedisPwd
=
"
testPassevnsoo567
"
var
RedisPwd
=
""
var
RedisDb
=
0
var
RedisDb
=
0
//ES conf
//ES conf
var
EsServerHttp
=
"
http://192.168.1.124:9200
"
var
EsServerHttp
=
""
//Mysql conf
//Mysql conf
type
MysqlConf
struct
{
type
MysqlConf
struct
{
...
...
es/Es
Db
.go
→
es/Es
Client
.go
View file @
339e1d59
...
@@ -4,13 +4,16 @@ import (
...
@@ -4,13 +4,16 @@ import (
"context"
"context"
"fmt"
"fmt"
"git.168cad.top/zhengqiuyun/rym-util/conf"
"git.168cad.top/zhengqiuyun/rym-util/conf"
"git.168cad.top/zhengqiuyun/rym-util/exception"
"git.168cad.top/zhengqiuyun/rym-util/log"
"git.168cad.top/zhengqiuyun/rym-util/log"
"github.com/olivere/elastic/v7"
"github.com/olivere/elastic/v7"
_log
"log"
_log
"log"
"os"
"os"
)
)
func
TestLog
()
{
log
.
Info
(
"Es 测试"
)
}
var
Client
*
elastic
.
Client
var
Client
*
elastic
.
Client
//初始化
//初始化
...
@@ -20,12 +23,13 @@ func init() {
...
@@ -20,12 +23,13 @@ func init() {
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
{
exception
.
ThrowsErrS
(
fmt
.
Sprintf
(
"ES连接失败:%s"
,
err
.
Error
()))
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
{
if
err
!=
nil
{
panic
(
err
)
panic
(
err
)
}
log
.
Info
(
fmt
.
Sprintf
(
"Elasticsearch connect %d"
,
code
))
log
.
Info
(
fmt
.
Sprintf
(
"Elasticsearch version %s"
,
info
.
Version
.
Number
))
}
}
log
.
Info
(
fmt
.
Sprintf
(
"Elasticsearch connect %d"
,
code
))
log
.
Info
(
fmt
.
Sprintf
(
"Elasticsearch version %s"
,
info
.
Version
.
Number
))
}
}
getWay/Response.go
View file @
339e1d59
...
@@ -6,7 +6,7 @@ import (
...
@@ -6,7 +6,7 @@ import (
"git.168cad.top/zhengqiuyun/rym-util/conf"
"git.168cad.top/zhengqiuyun/rym-util/conf"
"git.168cad.top/zhengqiuyun/rym-util/es"
"git.168cad.top/zhengqiuyun/rym-util/es"
"git.168cad.top/zhengqiuyun/rym-util/log"
"git.168cad.top/zhengqiuyun/rym-util/log"
db
"git.168cad.top/zhengqiuyun/rym-util/mysql"
db
"git.168cad.top/zhengqiuyun/rym-util/mysql
rym
"
"git.168cad.top/zhengqiuyun/rym-util/redis"
"git.168cad.top/zhengqiuyun/rym-util/redis"
"git.168cad.top/zhengqiuyun/rym-util/util"
"git.168cad.top/zhengqiuyun/rym-util/util"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin"
...
...
go.mod
View file @
339e1d59
...
@@ -3,9 +3,39 @@ module git.168cad.top/zhengqiuyun/rym-util
...
@@ -3,9 +3,39 @@ module git.168cad.top/zhengqiuyun/rym-util
go 1.17
go 1.17
require (
require (
github.com/gin-gonic/gin v1.7.4
github.com/gin-gonic/gin v1.8.1
github.com/garyburd/redigo v1.6.3
github.com/go-playground/validator/v10 v10.11.0
github.com/go-redis/redis v6.15.9+incompatible
github.com/olivere/elastic/v7 v7.0.32
github.com/olivere/elastic/v7 v7.0.32
gorm.io/driver/mysql v1.2.2
gorm.io/driver/mysql v1.2.2
gorm.io/gorm v1.22.4
gorm.io/gorm v1.22.4
)
)
\ No newline at end of file
require (
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/go-sql-driver/mysql v1.6.0 // indirect
github.com/goccy/go-json v0.9.7 // indirect
github.com/google/go-cmp v0.5.8 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.3 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/onsi/ginkgo v1.16.5 // indirect
github.com/onsi/gomega v1.20.0 // indirect
github.com/pelletier/go-toml/v2 v2.0.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/ugorji/go/codec v1.2.7 // indirect
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 // indirect
golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4 // indirect
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150 // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
go.sum
View file @
339e1d59
This diff is collapsed.
Click to expand it.
main.go
0 → 100644
View file @
339e1d59
package
main
import
(
"git.168cad.top/zhengqiuyun/rym-util/es"
"git.168cad.top/zhengqiuyun/rym-util/mysqlrym"
"git.168cad.top/zhengqiuyun/rym-util/redis"
)
func
main
()
{
mysqlrym
.
TestLog
()
es
.
TestLog
()
redis
.
TestLog
()
}
mysql
/MysqlDb
.go
→
mysql
rym/MysqlClient
.go
View file @
339e1d59
package
db
package
mysqlrym
import
(
import
(
"fmt"
"fmt"
"git.168cad.top/zhengqiuyun/rym-util/conf"
"git.168cad.top/zhengqiuyun/rym-util/conf"
"git.168cad.top/zhengqiuyun/rym-util/exception"
"git.168cad.top/zhengqiuyun/rym-util/log"
"git.168cad.top/zhengqiuyun/rym-util/log"
"git.168cad.top/zhengqiuyun/rym-util/util"
"git.168cad.top/zhengqiuyun/rym-util/util"
"gorm.io/driver/mysql"
"gorm.io/driver/mysql"
...
@@ -17,6 +16,10 @@ import (
...
@@ -17,6 +16,10 @@ import (
_log
"log"
_log
"log"
)
)
func
TestLog
()
{
log
.
Info
(
"Mysql 测试"
)
}
var
customerDb
*
gorm
.
DB
var
customerDb
*
gorm
.
DB
var
customerDbMap
sync
.
Map
var
customerDbMap
sync
.
Map
...
@@ -28,7 +31,7 @@ func SetTx() {
...
@@ -28,7 +31,7 @@ func SetTx() {
gidNeedTX
.
Store
(
gid
,
1
)
gidNeedTX
.
Store
(
gid
,
1
)
}
}
func
Get
Pg
Db
()
*
gorm
.
DB
{
func
GetDb
()
*
gorm
.
DB
{
return
getDb
(
customerDb
,
&
customerDbMap
)
return
getDb
(
customerDb
,
&
customerDbMap
)
}
}
...
@@ -109,7 +112,8 @@ func init() {
...
@@ -109,7 +112,8 @@ func init() {
Logger
:
newLogger
,
Logger
:
newLogger
,
})
})
if
err
!=
nil
{
if
err
!=
nil
{
exception
.
ThrowsErr
(
err
)
log
.
Error
(
fmt
.
Sprintf
(
"%s connect error:%s"
,
"Mysql server"
,
err
.
Error
()))
}
else
{
log
.
Info
(
fmt
.
Sprintf
(
"%s connect success"
,
"Mysql server"
))
}
}
log
.
Info
(
fmt
.
Sprintf
(
"%s connect success"
,
"Mysql server"
))
}
}
redis/Redis.go
deleted
100644 → 0
View file @
ad9ce240
package
redis
import
(
"fmt"
"git.168cad.top/zhengqiuyun/rym-util/conf"
"git.168cad.top/zhengqiuyun/rym-util/const/YorN"
"git.168cad.top/zhengqiuyun/rym-util/log"
"github.com/garyburd/redigo/redis"
)
var
pool
*
redis
.
Pool
//创建redis连接池
func
init
()
{
pool
=
&
redis
.
Pool
{
//实例化一个连接池
MaxIdle
:
10
,
//最初的连接数量
MaxActive
:
0
,
//连接池最大连接数量,不确定可以用0(0表示自动定义),按需分配
IdleTimeout
:
300
,
//连接关闭时间 300秒 (300秒不使用自动关闭)
Dial
:
func
()
(
redis
.
Conn
,
error
)
{
//要连接的redis数据库
c
,
err
:=
redis
.
Dial
(
"tcp"
,
conf
.
RedisAddr
)
if
err
!=
nil
{
return
nil
,
err
}
if
_
,
err
:=
c
.
Do
(
"AUTH"
,
conf
.
RedisPwd
);
err
!=
nil
{
c
.
Close
()
return
nil
,
err
}
if
_
,
err
:=
c
.
Do
(
"SELECT"
,
conf
.
RedisDb
);
err
!=
nil
{
c
.
Close
()
return
nil
,
err
}
return
c
,
nil
},
}
}
func
Incr
(
key
string
)
int64
{
c
:=
pool
.
Get
()
//从连接池,取一个链接
defer
c
.
Close
()
//函数运行结束 ,把连接放回连接池
r
,
err
:=
redis
.
Int64
(
c
.
Do
(
"INCR"
,
key
))
if
err
!=
nil
{
log
.
Error
(
err
.
Error
())
}
return
r
}
func
IncrBy
(
key
string
,
n
int
)
int64
{
c
:=
pool
.
Get
()
//从连接池,取一个链接
defer
c
.
Close
()
//函数运行结束 ,把连接放回连接池
r
,
err
:=
redis
.
Int64
(
c
.
Do
(
"INCRBY"
,
key
,
n
))
if
err
!=
nil
{
log
.
Error
(
err
.
Error
())
}
return
r
}
func
DecrBy
(
key
string
,
n
int
)
int64
{
c
:=
pool
.
Get
()
//从连接池,取一个链接
defer
c
.
Close
()
//函数运行结束 ,把连接放回连接池
r
,
err
:=
redis
.
Int64
(
c
.
Do
(
"DECRBY"
,
key
,
n
))
if
err
!=
nil
{
log
.
Error
(
err
.
Error
())
}
return
r
}
func
Exists
(
key
string
)
bool
{
c
:=
pool
.
Get
()
//从连接池,取一个链接
defer
c
.
Close
()
//函数运行结束 ,把连接放回连接池
r
,
err
:=
redis
.
Int64
(
c
.
Do
(
"EXISTS"
,
key
))
if
err
!=
nil
{
return
false
}
return
r
>
0
}
//SwitchOpen 在redis中存0或1,1代表OK
func
SwitchOpen
(
key
string
)
bool
{
r
:=
GetString
(
key
)
log
.
Debug
(
fmt
.
Sprintf
(
"缓存[%s]数据为:%s"
,
key
,
r
))
return
r
==
YorN
.
Y
}
func
Get
(
key
string
)
[]
byte
{
c
:=
pool
.
Get
()
//从连接池,取一个链接
defer
c
.
Close
()
//函数运行结束 ,把连接放回连接池
r
,
err
:=
redis
.
Bytes
(
c
.
Do
(
"GET"
,
key
))
if
err
!=
nil
{
return
nil
}
return
r
}
func
GetString
(
key
string
)
string
{
c
:=
pool
.
Get
()
//从连接池,取一个链接
defer
c
.
Close
()
//函数运行结束 ,把连接放回连接池
r
,
err
:=
redis
.
String
(
c
.
Do
(
"GET"
,
key
))
if
err
!=
nil
{
return
""
}
return
r
}
func
Set
(
k
string
,
v
string
)
{
c
:=
pool
.
Get
()
//从连接池,取一个链接
defer
c
.
Close
()
//函数运行结束 ,把连接放回连接池
r
,
err
:=
c
.
Do
(
"SET"
,
k
,
v
)
if
err
!=
nil
{
log
.
Error
(
err
.
Error
())
}
log
.
Debug
(
fmt
.
Sprintf
(
"设置缓存成功:%s"
,
r
))
}
func
Expire
(
k
string
,
t
int
)
{
c
:=
pool
.
Get
()
//从连接池,取一个链接
defer
c
.
Close
()
r
,
err
:=
c
.
Do
(
"EXISTS"
,
k
,
t
)
if
err
!=
nil
{
log
.
Error
(
err
.
Error
())
}
log
.
Debug
(
fmt
.
Sprintf
(
"设置缓存过期成功:%d"
,
r
))
}
redis/RedisClient.go
0 → 100644
View file @
339e1d59
package
redis
import
(
"fmt"
"git.168cad.top/zhengqiuyun/rym-util/conf"
"git.168cad.top/zhengqiuyun/rym-util/const/YorN"
"git.168cad.top/zhengqiuyun/rym-util/log"
"github.com/go-redis/redis"
"time"
)
func
TestLog
()
{
log
.
Info
(
"Redis 测试"
)
}
var
redisClient
*
redis
.
Client
func
init
()
{
redisClient
=
redis
.
NewClient
(
&
redis
.
Options
{
Addr
:
conf
.
RedisAddr
,
Password
:
conf
.
RedisPwd
,
DB
:
conf
.
RedisDb
,
// redis一共16个库,指定其中一个库即可
})
_
,
err
:=
redisClient
.
Ping
()
.
Result
()
if
err
!=
nil
{
log
.
Error
(
fmt
.
Sprintf
(
"Redis connect error :%s"
,
err
.
Error
()))
}
return
}
func
Incr
(
key
string
)
int64
{
r
:=
redisClient
.
Incr
(
key
)
return
r
.
Val
()
}
func
IncrBy
(
key
string
,
n
int64
)
int64
{
r
:=
redisClient
.
IncrBy
(
key
,
n
)
return
r
.
Val
()
}
func
DecrBy
(
key
string
,
n
int64
)
int64
{
r
:=
redisClient
.
DecrBy
(
key
,
n
)
return
r
.
Val
()
}
func
Exists
(
key
string
)
bool
{
r
:=
redisClient
.
Exists
(
key
)
return
r
.
Val
()
>
0
}
//SwitchOpen 在redis中存0或1,1代表OK
func
SwitchOpen
(
key
string
)
bool
{
r
:=
GetString
(
key
)
log
.
Debug
(
fmt
.
Sprintf
(
"缓存[%s]数据为:%s"
,
key
,
r
))
return
r
==
YorN
.
Y
}
func
GetString
(
key
string
)
string
{
r
:=
redisClient
.
Get
(
key
)
return
r
.
Val
()
}
func
Set
(
k
string
,
v
string
)
{
redisClient
.
Set
(
k
,
v
,
0
)
log
.
Debug
(
fmt
.
Sprintf
(
"设置缓存成功:%s %s"
,
k
,
v
))
}
func
Expire
(
k
string
,
t
int64
)
{
redisClient
.
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