fix: 修复构建配置和运行时错误处理
This commit is contained in:
@@ -30,22 +30,28 @@ func NewRabbitMQ(cfg *config.RabbitMQConfig) (*RabbitMQ, error) {
|
||||
}
|
||||
ch, err := conn.Channel()
|
||||
if err != nil {
|
||||
_ = conn.Close()
|
||||
return nil, err
|
||||
}
|
||||
return &RabbitMQ{Conn: conn, Ch: ch}, nil
|
||||
}
|
||||
|
||||
func (r *RabbitMQ) Close() error {
|
||||
if r == nil || r.Ch == nil || r.Conn == nil {
|
||||
if r == nil {
|
||||
return nil
|
||||
}
|
||||
if err := r.Ch.Close(); err != nil {
|
||||
return err
|
||||
var closeErr error
|
||||
if r.Ch != nil {
|
||||
if err := r.Ch.Close(); err != nil {
|
||||
closeErr = err
|
||||
}
|
||||
}
|
||||
if err := r.Conn.Close(); err != nil {
|
||||
return err
|
||||
if r.Conn != nil {
|
||||
if err := r.Conn.Close(); closeErr == nil && err != nil {
|
||||
closeErr = err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
return closeErr
|
||||
}
|
||||
|
||||
func (r *RabbitMQ) DeclareTopic(exchange string, queue string, bindingKey string) error {
|
||||
|
||||
@@ -4,11 +4,12 @@ import (
|
||||
jwt "feedsystem_video_go/internal/middleware/jwt"
|
||||
rediscache "feedsystem_video_go/internal/middleware/redis"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type KeyFunc func(*gin.Context) (string, bool)
|
||||
|
||||
@@ -2,21 +2,34 @@ package redis
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
func (c *Client) GetBytes(ctx context.Context, key string) ([]byte, error) {
|
||||
if c == nil || c.rdb == nil {
|
||||
return nil, errors.New("redis client not initialized")
|
||||
}
|
||||
return c.rdb.Get(ctx, key).Bytes()
|
||||
}
|
||||
|
||||
func (c *Client) SetBytes(ctx context.Context, key string, value []byte, ttl time.Duration) error {
|
||||
if c == nil || c.rdb == nil {
|
||||
return errors.New("redis client not initialized")
|
||||
}
|
||||
return c.rdb.Set(ctx, key, value, ttl).Err()
|
||||
}
|
||||
|
||||
func (c *Client) Del(ctx context.Context, key string) error {
|
||||
if c == nil || c.rdb == nil {
|
||||
return errors.New("redis client not initialized")
|
||||
}
|
||||
return c.rdb.Del(ctx, key).Err()
|
||||
}
|
||||
|
||||
func (c *Client) MGet(cacheCtx context.Context, cacheKeys ...string) ([]interface{}, error) {
|
||||
if c == nil || c.rdb == nil {
|
||||
return nil, errors.New("redis client not initialized")
|
||||
}
|
||||
return c.rdb.MGet(cacheCtx, cacheKeys...).Result()
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"feedsystem_video_go/internal/config"
|
||||
"fmt"
|
||||
"strconv"
|
||||
@@ -41,7 +42,7 @@ func (c *Client) Close() error {
|
||||
|
||||
func (c *Client) Ping(ctx context.Context) error {
|
||||
if c == nil || c.rdb == nil {
|
||||
return nil
|
||||
return errors.New("redis client not initialized")
|
||||
}
|
||||
return c.rdb.Ping(ctx).Err()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user