代码拉取完成,页面将自动刷新
package dao
import (
"context"
"errors"
"fmt"
"gitee.com/h79/goutils/common/result"
"gitee.com/h79/goutils/common/stringutil"
"gitee.com/h79/goutils/dao/db"
"gitee.com/h79/goutils/dao/option"
"gitee.com/h79/goutils/dao/redis"
"gitee.com/h79/goutils/dao/util"
"gitee.com/h79/goutils/dao/wrapper"
"gitee.com/h79/goutils/perf"
goredis "github.com/go-redis/redis/v8"
"github.com/olivere/elastic/v7"
"gorm.io/gorm"
"time"
)
// Dao https://gorm.io/docs/query.html
type Dao struct {
Sql db.SqlDatabase
Redis redis.Client
Elastic db.EsDatabase
}
var errZero = fmt.Errorf("length is zero")
func (d *Dao) DBClient(opts ...option.Option) (*gorm.DB, error) {
if d.Sql == nil {
return nil, result.RErrNotSupport
}
var name, _ = option.NameKeyExist(opts...)
sql, err := d.Sql.Get(name)
if err != nil {
return nil, result.RErrNil
}
return sql.Db(), nil
}
func (d *Dao) RedisClient(opts ...option.Option) (*goredis.Client, error) {
if d.Redis == nil {
return nil, result.RErrNotSupport
}
var name, _ = option.NameKeyExist(opts...)
rds, err := d.Redis.Get(name)
if err != nil {
return nil, err
}
return rds.Rds(), nil
}
func (d *Dao) ClusterRedisClient(opts ...option.Option) (*goredis.ClusterClient, error) {
if d.Redis == nil {
return nil, result.RErrNotSupport
}
var name, _ = option.NameKeyExist(opts...)
rds, err := d.Redis.Get(name)
if err != nil {
return nil, err
}
return rds.Cluster(), nil
}
func (d *Dao) SentinelRedisClient(opts ...option.Option) (*goredis.SentinelClient, error) {
if d.Redis == nil {
return nil, result.RErrNotSupport
}
var name, _ = option.NameKeyExist(opts...)
rds, err := d.Redis.Get(name)
if err != nil {
return nil, err
}
return rds.Sentinel(), nil
}
func (d *Dao) ElasticClient(opts ...option.Option) (*elastic.Client, error) {
if d.Elastic == nil {
return nil, result.RErrNotSupport
}
var name, _ = option.NameKeyExist(opts...)
es, err := d.Elastic.Get(name)
return es, err
}
func (d *Dao) UseRedis(opts ...option.Option) CurrentRedis {
client, _ := d.RedisClient(opts...)
t, _ := option.TimeOutExist(opts...)
w := option.AlarmExist(opts...)
return CurrentRedis{Client: client, timeout: t, warn: w}
}
func (d *Dao) UseDB(opts ...option.Option) CurrentDb {
client, _ := d.DBClient(opts...)
return CurrentDb{DB: client}
}
func (d *Dao) UseElastic(opts ...option.Option) CurrentElastic {
client, _ := d.ElasticClient(opts...)
return CurrentElastic{Client: client}
}
func (d *Dao) Condition(ctx context.Context, data interface{}, call db.ConditionCallback, opts ...option.Option) result.Result {
sql, err := d.DBClient(opts...)
if err != nil {
return result.Error(result.ErrDbInternal, fmt.Sprintf("the db non existed"))
}
tx := call(ctx, data, sql)
if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
return result.Error(result.ErrNotFound, tx.Error.Error())
}
if tx.Error != nil {
return result.Error(result.ErrDbInternal, tx.Error.Error())
}
return result.Succeed()
}
func (d *Dao) IsNotFound(db *gorm.DB) int {
return wrapper.IsNotFound(db)
}
type CurrentElastic struct {
*elastic.Client
}
func (d *CurrentElastic) OK() bool {
return d.Client != nil
}
type CurrentDb struct {
*gorm.DB
}
func (d *CurrentDb) OK() bool {
return d.DB != nil
}
type CurrentRedis struct {
*goredis.Client
timeout int64
warn bool
}
func (d *CurrentRedis) Warning(r *perf.RunTime, err error) {
dif := r.End()
if dif == 0 || !d.warn {
return
}
util.Alarm(result.ErrDbInternal, "Redis", fmt.Sprintf("run too time long=> %d,start=> %d", dif, r.Start()), err)
}
func (d *CurrentRedis) OK() bool {
return d.Client != nil
}
func (d *CurrentRedis) HSet(ctx context.Context, key, field string, value interface{}) (int64, error) {
if d.Client == nil {
return 0, result.RErrNil
}
if len(field) <= 0 {
return 0, errZero
}
var err error
var rt = perf.Begin(d.timeout)
defer d.Warning(rt, nil)
ret, err := d.Client.HSet(ctx, key, field, value).Result()
return ret, err
}
func (d *CurrentRedis) HSetNX(ctx context.Context, key, field string, value interface{}) (bool, error) {
if d.Client == nil {
return false, result.RErrNil
}
if len(field) <= 0 {
return false, errZero
}
var err error
var rt = perf.Begin(d.timeout)
defer d.Warning(rt, err)
ret, err := d.Client.HSetNX(ctx, key, field, value).Result()
return ret, err
}
func (d *CurrentRedis) HGet(ctx context.Context, key, field string) (string, error) {
if d.Client == nil {
return "", result.RErrNil
}
var err error
var rt = perf.Begin(d.timeout)
defer d.Warning(rt, err)
ret, err := d.Client.HGet(ctx, key, field).Result()
return ret, err
}
func (d *CurrentRedis) HGetAll(ctx context.Context, key string) (map[string]string, error) {
if d.Client == nil {
return nil, result.RErrNil
}
var err error
var rt = perf.Begin(d.timeout)
defer d.Warning(rt, err)
ret, err := d.Client.HGetAll(ctx, key).Result()
return ret, err
}
func (d *CurrentRedis) HGetInt(ctx context.Context, key, field string) (int, error) {
if d.Client == nil {
return 0, result.RErrNil
}
var err error
var rt = perf.Begin(d.timeout)
defer d.Warning(rt, err)
ret, err := d.Client.HGet(ctx, key, field).Result()
if err != nil {
return 0, err
}
return stringutil.StringToInt(ret), nil
}
func (d *CurrentRedis) HDel(ctx context.Context, key, field string) (int64, error) {
if d.Client == nil {
return 0, result.RErrNil
}
var err error
var rt = perf.Begin(d.timeout)
defer d.Warning(rt, err)
ret, err := d.Client.HDel(ctx, key, field).Result()
return ret, err
}
func (d *CurrentRedis) HInc(ctx context.Context, key, field string, value int64) (int64, error) {
if d.Client == nil {
return 0, result.RErrNil
}
if len(field) <= 0 {
return 0, errZero
}
var err error
var rt = perf.Begin(d.timeout)
defer d.Warning(rt, err)
ret, err := d.Client.HIncrBy(ctx, key, field, value).Result()
return ret, err
}
func (d *CurrentRedis) Set(ctx context.Context, key string, value interface{}, expiration time.Duration) (string, error) {
if d.Client == nil {
return "", result.RErrNil
}
if len(key) <= 0 {
return "", errZero
}
var err error
var rt = perf.Begin(d.timeout)
defer d.Warning(rt, err)
ret, err := d.Client.Set(ctx, key, value, expiration).Result()
return ret, err
}
func (d *CurrentRedis) SetNX(ctx context.Context, key string, value interface{}, expiration time.Duration) (bool, error) {
if d.Client == nil {
return false, result.RErrNil
}
if len(key) <= 0 {
return false, errZero
}
var err error
var rt = perf.Begin(d.timeout)
defer d.Warning(rt, err)
ret, err := d.Client.SetNX(ctx, key, value, expiration).Result()
return ret, err
}
func (d *CurrentRedis) SetEX(ctx context.Context, key string, value interface{}, expiration time.Duration) (string, error) {
if d.Client == nil {
return "", result.RErrNil
}
if len(key) <= 0 {
return "", errZero
}
var err error
var rt = perf.Begin(d.timeout)
defer d.Warning(rt, err)
ret, err := d.Client.SetEX(ctx, key, value, expiration).Result()
return ret, err
}
func (d *CurrentRedis) Get(ctx context.Context, key string) (string, error) {
if d.Client == nil {
return "", result.RErrNil
}
var err error
var rt = perf.Begin(d.timeout)
defer d.Warning(rt, err)
ret, err := d.Client.Get(ctx, key).Result()
return ret, err
}
func (d *CurrentRedis) Inc(ctx context.Context, key string, value int64) (int64, error) {
if d.Client == nil {
return 0, result.RErrNil
}
if len(key) <= 0 {
return 0, errZero
}
var err error
var rt = perf.Begin(d.timeout)
defer d.Warning(rt, err)
ret, err := d.Client.IncrBy(ctx, key, value).Result()
return ret, err
}
func (d *CurrentRedis) GetInt(ctx context.Context, key string) (int, error) {
if d.Client == nil {
return 0, result.RErrNil
}
var err error
var rt = perf.Begin(d.timeout)
defer d.Warning(rt, err)
ret, err := d.Client.Get(ctx, key).Result()
if err != nil {
return 0, err
}
return stringutil.StringToInt(ret), nil
}
// ExpireAt 在某个时间点过期
func (d *CurrentRedis) ExpireAt(ctx context.Context, key string, time time.Time) {
if d.Client == nil {
return
}
var err error
var rt = perf.Begin(d.timeout)
defer d.Warning(rt, err)
_, err = d.Client.ExpireAt(ctx, key, time).Result()
}
// Expire 过多久失效
func (d *CurrentRedis) Expire(ctx context.Context, key string, time time.Duration) {
if d.Client == nil {
return
}
var err error
var rt = perf.Begin(d.timeout)
defer d.Warning(rt, err)
_, err = d.Client.Expire(ctx, key, time).Result()
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。