mirror of
https://github.com/gofiber/storage.git
synced 2025-10-28 02:31:26 +08:00
Handle rueidis startup check failures
This commit is contained in:
@@ -185,6 +185,8 @@ type Config struct {
|
|||||||
Reset bool
|
Reset bool
|
||||||
|
|
||||||
// DisableStartupCheck skips the initial connection validation during New.
|
// DisableStartupCheck skips the initial connection validation during New.
|
||||||
|
// When true and client creation fails, New returns a Storage whose
|
||||||
|
// operations surface the initialization error instead of panicking.
|
||||||
//
|
//
|
||||||
// Optional. Default is false
|
// Optional. Default is false
|
||||||
DisableStartupCheck bool
|
DisableStartupCheck bool
|
||||||
|
|||||||
@@ -96,6 +96,8 @@ type Config struct {
|
|||||||
Reset bool
|
Reset bool
|
||||||
|
|
||||||
// DisableStartupCheck skips the initial connection validation during New.
|
// DisableStartupCheck skips the initial connection validation during New.
|
||||||
|
// When true and the client cannot be created, New returns a Storage whose
|
||||||
|
// operations will report the initialization error instead of panicking.
|
||||||
//
|
//
|
||||||
// Optional. Default is false
|
// Optional. Default is false
|
||||||
DisableStartupCheck bool
|
DisableStartupCheck bool
|
||||||
|
|||||||
@@ -11,7 +11,8 @@ var cacheTTL = time.Second
|
|||||||
|
|
||||||
// Storage interface that is implemented by storage providers
|
// Storage interface that is implemented by storage providers
|
||||||
type Storage struct {
|
type Storage struct {
|
||||||
db rueidis.Client
|
db rueidis.Client
|
||||||
|
initErr error
|
||||||
}
|
}
|
||||||
|
|
||||||
// New creates a new rueidis storage
|
// New creates a new rueidis storage
|
||||||
@@ -63,6 +64,10 @@ func New(config ...Config) *Storage {
|
|||||||
AlwaysPipelining: cfg.AlwaysPipelining,
|
AlwaysPipelining: cfg.AlwaysPipelining,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if cfg.DisableStartupCheck {
|
||||||
|
return &Storage{initErr: err}
|
||||||
|
}
|
||||||
|
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -88,6 +93,9 @@ func New(config ...Config) *Storage {
|
|||||||
|
|
||||||
// GetWithContext gets value by key with context
|
// GetWithContext gets value by key with context
|
||||||
func (s *Storage) GetWithContext(ctx context.Context, key string) ([]byte, error) {
|
func (s *Storage) GetWithContext(ctx context.Context, key string) ([]byte, error) {
|
||||||
|
if s.db == nil {
|
||||||
|
return nil, s.initErr
|
||||||
|
}
|
||||||
if len(key) <= 0 {
|
if len(key) <= 0 {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
@@ -105,6 +113,9 @@ func (s *Storage) Get(key string) ([]byte, error) {
|
|||||||
|
|
||||||
// SetWithContext sets key with value with context
|
// SetWithContext sets key with value with context
|
||||||
func (s *Storage) SetWithContext(ctx context.Context, key string, val []byte, exp time.Duration) error {
|
func (s *Storage) SetWithContext(ctx context.Context, key string, val []byte, exp time.Duration) error {
|
||||||
|
if s.db == nil {
|
||||||
|
return s.initErr
|
||||||
|
}
|
||||||
if len(key) <= 0 || len(val) <= 0 {
|
if len(key) <= 0 || len(val) <= 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -122,6 +133,9 @@ func (s *Storage) Set(key string, val []byte, exp time.Duration) error {
|
|||||||
|
|
||||||
// DeleteWithContext deletes key by key with context
|
// DeleteWithContext deletes key by key with context
|
||||||
func (s *Storage) DeleteWithContext(ctx context.Context, key string) error {
|
func (s *Storage) DeleteWithContext(ctx context.Context, key string) error {
|
||||||
|
if s.db == nil {
|
||||||
|
return s.initErr
|
||||||
|
}
|
||||||
if len(key) <= 0 {
|
if len(key) <= 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -135,6 +149,9 @@ func (s *Storage) Delete(key string) error {
|
|||||||
|
|
||||||
// ResetWithContext resets all keys with context
|
// ResetWithContext resets all keys with context
|
||||||
func (s *Storage) ResetWithContext(ctx context.Context) error {
|
func (s *Storage) ResetWithContext(ctx context.Context) error {
|
||||||
|
if s.db == nil {
|
||||||
|
return s.initErr
|
||||||
|
}
|
||||||
return s.db.Do(ctx, s.db.B().Flushdb().Build()).Error()
|
return s.db.Do(ctx, s.db.B().Flushdb().Build()).Error()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user