Files
mayfly-go/server/internal/redis/application/redis.go

128 lines
3.3 KiB
Go
Raw Normal View History

package application
import (
"context"
2022-09-09 18:26:08 +08:00
"mayfly-go/internal/redis/domain/entity"
"mayfly-go/internal/redis/domain/repository"
"mayfly-go/internal/redis/rdm"
"mayfly-go/pkg/base"
"mayfly-go/pkg/errorx"
"mayfly-go/pkg/model"
2022-09-29 13:14:50 +08:00
"strconv"
"strings"
)
type Redis interface {
base.App[*entity.Redis]
// 分页获取机器脚本信息列表
GetPageList(condition *entity.RedisQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error)
2022-10-26 20:49:29 +08:00
Count(condition *entity.RedisQuery) int64
2021-09-11 14:04:09 +08:00
// 测试连接
TestConn(re *entity.Redis) error
Save(ctx context.Context, re *entity.Redis) error
// 删除数据库信息
Delete(ctx context.Context, id uint64) error
// 获取数据库连接实例
2022-09-29 13:14:50 +08:00
// id: 数据库实例id
// db: 库号
GetRedisConn(id uint64, db int) (*rdm.RedisConn, error)
}
2022-09-09 18:26:08 +08:00
func newRedisApp(redisRepo repository.Redis) Redis {
return &redisAppImpl{
base.AppImpl[*entity.Redis, repository.Redis]{Repo: redisRepo},
2022-09-09 18:26:08 +08:00
}
}
2022-09-09 18:26:08 +08:00
type redisAppImpl struct {
base.AppImpl[*entity.Redis, repository.Redis]
}
// 分页获取redis列表
func (r *redisAppImpl) GetPageList(condition *entity.RedisQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
return r.GetRepo().GetRedisList(condition, pageParam, toEntity, orderBy...)
}
2022-10-26 20:49:29 +08:00
func (r *redisAppImpl) Count(condition *entity.RedisQuery) int64 {
return r.GetRepo().Count(condition)
2021-09-11 14:04:09 +08:00
}
func (r *redisAppImpl) TestConn(re *entity.Redis) error {
db := 0
if re.Db != "" {
db, _ = strconv.Atoi(strings.Split(re.Db, ",")[0])
}
rc, err := re.ToRedisInfo(db).Conn()
if err != nil {
return err
}
rc.Close()
return nil
}
func (r *redisAppImpl) Save(ctx context.Context, re *entity.Redis) error {
// 查找是否存在该库
2022-09-29 13:14:50 +08:00
oldRedis := &entity.Redis{Host: re.Host}
2023-03-17 09:46:41 +08:00
if re.SshTunnelMachineId > 0 {
oldRedis.SshTunnelMachineId = re.SshTunnelMachineId
}
err := r.GetBy(oldRedis)
if re.Id == 0 {
if err == nil {
return errorx.NewBiz("该实例已存在")
}
re.PwdEncrypt()
return r.Insert(ctx, re)
}
// 如果存在该库,则校验修改的库是否为该库
if err == nil && oldRedis.Id != re.Id {
return errorx.NewBiz("该实例已存在")
}
// 如果修改了redis实例的库信息则关闭旧库的连接
if oldRedis.Db != re.Db || oldRedis.SshTunnelMachineId != re.SshTunnelMachineId {
for _, dbStr := range strings.Split(oldRedis.Db, ",") {
db, _ := strconv.Atoi(dbStr)
rdm.CloseConn(re.Id, db)
}
}
re.PwdEncrypt()
return r.UpdateById(ctx, re)
}
// 删除Redis信息
func (r *redisAppImpl) Delete(ctx context.Context, id uint64) error {
re, err := r.GetById(new(entity.Redis), id)
if err != nil {
return errorx.NewBiz("该redis信息不存在")
}
2022-09-29 13:14:50 +08:00
// 如果存在连接,则关闭所有库连接信息
for _, dbStr := range strings.Split(re.Db, ",") {
db, _ := strconv.Atoi(dbStr)
rdm.CloseConn(re.Id, db)
2022-09-29 13:14:50 +08:00
}
return r.DeleteById(ctx, id)
}
// 获取数据库连接实例
func (r *redisAppImpl) GetRedisConn(id uint64, db int) (*rdm.RedisConn, error) {
return rdm.GetRedisConn(id, db, func() (*rdm.RedisInfo, error) {
// 缓存不存在则回调获取redis信息
re, err := r.GetById(new(entity.Redis), id)
if err != nil {
return nil, errorx.NewBiz("redis信息不存在")
}
re.PwdDecrypt()
return re.ToRedisInfo(db), nil
})
}