mirror of
https://github.com/gofiber/storage.git
synced 2025-10-24 00:43:12 +08:00
Compare commits
9 Commits
dependabot
...
add-config
Author | SHA1 | Date | |
---|---|---|---|
![]() |
9951538c9e | ||
![]() |
50d99b709c | ||
![]() |
02a5e617ae | ||
![]() |
2fdcd6aba5 | ||
![]() |
c285284184 | ||
![]() |
75a86506f7 | ||
![]() |
1782bd5841 | ||
![]() |
5652ca01b7 | ||
![]() |
92da8e1d90 |
@@ -73,6 +73,11 @@ type Config struct {
|
|||||||
//
|
//
|
||||||
// Optional. Default is false
|
// Optional. Default is false
|
||||||
Reset bool
|
Reset bool
|
||||||
|
|
||||||
|
// DisableStartupCheck skips the initial connection validation during New.
|
||||||
|
//
|
||||||
|
// Optional. Default is false
|
||||||
|
DisableStartupCheck bool
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -80,5 +85,6 @@ type Config struct {
|
|||||||
```go
|
```go
|
||||||
var ConfigDefault = Config{
|
var ConfigDefault = Config{
|
||||||
Servers: "127.0.0.1:11211",
|
Servers: "127.0.0.1:11211",
|
||||||
|
DisableStartupCheck: false,
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@@ -15,6 +15,11 @@ type Config struct {
|
|||||||
// Optional. Default is false
|
// Optional. Default is false
|
||||||
Reset bool
|
Reset bool
|
||||||
|
|
||||||
|
// DisableStartupCheck skips the initial connection validation during New.
|
||||||
|
//
|
||||||
|
// Optional. Default is false
|
||||||
|
DisableStartupCheck bool
|
||||||
|
|
||||||
// The socket read/write timeout.
|
// The socket read/write timeout.
|
||||||
//
|
//
|
||||||
// Optional. Default is 100 * time.Millisecond
|
// Optional. Default is 100 * time.Millisecond
|
||||||
@@ -34,6 +39,7 @@ var ConfigDefault = Config{
|
|||||||
Servers: "127.0.0.1:11211",
|
Servers: "127.0.0.1:11211",
|
||||||
timeout: 100 * time.Millisecond,
|
timeout: 100 * time.Millisecond,
|
||||||
maxIdleConns: 2,
|
maxIdleConns: 2,
|
||||||
|
DisableStartupCheck: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helper function to set default values
|
// Helper function to set default values
|
||||||
|
@@ -30,6 +30,7 @@ func New(config ...Config) *Storage {
|
|||||||
db.Timeout = cfg.timeout
|
db.Timeout = cfg.timeout
|
||||||
db.MaxIdleConns = cfg.maxIdleConns
|
db.MaxIdleConns = cfg.maxIdleConns
|
||||||
|
|
||||||
|
if !cfg.DisableStartupCheck {
|
||||||
// Ping database to ensure a connection has been made
|
// Ping database to ensure a connection has been made
|
||||||
if err := db.Ping(); err != nil {
|
if err := db.Ping(); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
@@ -40,6 +41,7 @@ func New(config ...Config) *Storage {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Create storage
|
// Create storage
|
||||||
store := &Storage{
|
store := &Storage{
|
||||||
|
@@ -178,6 +178,16 @@ func Test_Memcache_Reset(t *testing.T) {
|
|||||||
require.Zero(t, len(result))
|
require.Zero(t, len(result))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_Memcache_New_DisableStartupCheck(t *testing.T) {
|
||||||
|
require.NotPanics(t, func() {
|
||||||
|
store := New(Config{
|
||||||
|
Servers: "127.0.0.1:11210",
|
||||||
|
DisableStartupCheck: true,
|
||||||
|
})
|
||||||
|
require.NotNil(t, store)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func Test_Memcache_Close(t *testing.T) {
|
func Test_Memcache_Close(t *testing.T) {
|
||||||
testStore := newTestStore(t)
|
testStore := newTestStore(t)
|
||||||
require.Nil(t, testStore.Close())
|
require.Nil(t, testStore.Close())
|
||||||
|
@@ -117,6 +117,11 @@ type Config struct {
|
|||||||
// Optional. Default is false
|
// Optional. Default is false
|
||||||
Reset bool
|
Reset bool
|
||||||
|
|
||||||
|
// DisableStartupCheck skips the initial connection validation during New.
|
||||||
|
//
|
||||||
|
// Optional. Default is false
|
||||||
|
DisableStartupCheck bool
|
||||||
|
|
||||||
// Time before deleting expired keys
|
// Time before deleting expired keys
|
||||||
//
|
//
|
||||||
// Optional. Default is 10 * time.Second
|
// Optional. Default is 10 * time.Second
|
||||||
@@ -135,6 +140,7 @@ var ConfigDefault = Config{
|
|||||||
Table: "fiber_storage",
|
Table: "fiber_storage",
|
||||||
SSLMode: "disable",
|
SSLMode: "disable",
|
||||||
Reset: false,
|
Reset: false,
|
||||||
|
DisableStartupCheck: false,
|
||||||
GCInterval: 10 * time.Second,
|
GCInterval: 10 * time.Second,
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@@ -61,6 +61,11 @@ type Config struct {
|
|||||||
// Optional. Default is false
|
// Optional. Default is false
|
||||||
Reset bool
|
Reset bool
|
||||||
|
|
||||||
|
// DisableStartupCheck skips the initial connection validation during New.
|
||||||
|
//
|
||||||
|
// Optional. Default is false
|
||||||
|
DisableStartupCheck bool
|
||||||
|
|
||||||
// Time before deleting expired keys
|
// Time before deleting expired keys
|
||||||
//
|
//
|
||||||
// Optional. Default is 10 * time.Second
|
// Optional. Default is 10 * time.Second
|
||||||
@@ -76,6 +81,7 @@ var ConfigDefault = Config{
|
|||||||
Table: "fiber_storage",
|
Table: "fiber_storage",
|
||||||
SSLMode: "disable",
|
SSLMode: "disable",
|
||||||
Reset: false,
|
Reset: false,
|
||||||
|
DisableStartupCheck: false,
|
||||||
GCInterval: 10 * time.Second,
|
GCInterval: 10 * time.Second,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -68,6 +68,7 @@ func New(config ...Config) *Storage {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !cfg.DisableStartupCheck {
|
||||||
// Ping database
|
// Ping database
|
||||||
if err := db.Ping(context.Background()); err != nil {
|
if err := db.Ping(context.Background()); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
@@ -108,6 +109,7 @@ func New(config ...Config) *Storage {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Create storage
|
// Create storage
|
||||||
store := &Storage{
|
store := &Storage{
|
||||||
@@ -121,7 +123,9 @@ func New(config ...Config) *Storage {
|
|||||||
sqlGC: fmt.Sprintf("DELETE FROM %s WHERE e <= $1 AND e != 0", cfg.Table),
|
sqlGC: fmt.Sprintf("DELETE FROM %s WHERE e <= $1 AND e != 0", cfg.Table),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !cfg.DisableStartupCheck {
|
||||||
store.checkSchema(cfg.Table)
|
store.checkSchema(cfg.Table)
|
||||||
|
}
|
||||||
|
|
||||||
// Start garbage collector
|
// Start garbage collector
|
||||||
go store.gcTicker()
|
go store.gcTicker()
|
||||||
|
@@ -202,6 +202,18 @@ func TestNoCreateUser(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_Postgres_New_DisableStartupCheck(t *testing.T) {
|
||||||
|
require.NotPanics(t, func() {
|
||||||
|
store := New(Config{
|
||||||
|
Host: "127.0.0.1",
|
||||||
|
Port: 65432,
|
||||||
|
DisableStartupCheck: true,
|
||||||
|
})
|
||||||
|
require.NotNil(t, store)
|
||||||
|
defer store.Close()
|
||||||
|
})
|
||||||
|
}
|
||||||
func Test_Should_Panic_On_Wrong_Schema(t *testing.T) {
|
func Test_Should_Panic_On_Wrong_Schema(t *testing.T) {
|
||||||
testStore := newTestStore(t)
|
testStore := newTestStore(t)
|
||||||
defer testStore.Close()
|
defer testStore.Close()
|
||||||
|
@@ -180,6 +180,11 @@ type Config struct {
|
|||||||
// Optional. Default is false
|
// Optional. Default is false
|
||||||
Reset bool
|
Reset bool
|
||||||
|
|
||||||
|
// DisableStartupCheck skips the initial connection validation during New.
|
||||||
|
//
|
||||||
|
// Optional. Default is false
|
||||||
|
DisableStartupCheck bool
|
||||||
|
|
||||||
// TLS Config to use. When set TLS will be negotiated.
|
// TLS Config to use. When set TLS will be negotiated.
|
||||||
//
|
//
|
||||||
// Optional. Default is nil
|
// Optional. Default is nil
|
||||||
@@ -208,6 +213,7 @@ var ConfigDefault = Config{
|
|||||||
URL: "",
|
URL: "",
|
||||||
Database: 0,
|
Database: 0,
|
||||||
Reset: false,
|
Reset: false,
|
||||||
|
DisableStartupCheck: false,
|
||||||
TLSConfig: nil,
|
TLSConfig: nil,
|
||||||
PoolSize: 10 * runtime.GOMAXPROCS(0),
|
PoolSize: 10 * runtime.GOMAXPROCS(0),
|
||||||
Addrs: []string{},
|
Addrs: []string{},
|
||||||
|
@@ -68,6 +68,11 @@ type Config struct {
|
|||||||
// Optional. Default is false
|
// Optional. Default is false
|
||||||
Reset bool
|
Reset bool
|
||||||
|
|
||||||
|
// DisableStartupCheck skips the initial connection validation during New.
|
||||||
|
//
|
||||||
|
// Optional. Default is false
|
||||||
|
DisableStartupCheck bool
|
||||||
|
|
||||||
// TLS Config to use. When set TLS will be negotiated.
|
// TLS Config to use. When set TLS will be negotiated.
|
||||||
//
|
//
|
||||||
// Optional. Default is nil
|
// Optional. Default is nil
|
||||||
@@ -94,6 +99,7 @@ var ConfigDefault = Config{
|
|||||||
URL: "",
|
URL: "",
|
||||||
Database: 0,
|
Database: 0,
|
||||||
Reset: false,
|
Reset: false,
|
||||||
|
DisableStartupCheck: false,
|
||||||
TLSConfig: nil,
|
TLSConfig: nil,
|
||||||
PoolSize: 10 * runtime.GOMAXPROCS(0),
|
PoolSize: 10 * runtime.GOMAXPROCS(0),
|
||||||
Addrs: []string{},
|
Addrs: []string{},
|
||||||
|
@@ -65,6 +65,7 @@ func New(config ...Config) *Storage {
|
|||||||
IsClusterMode: cfg.IsClusterMode,
|
IsClusterMode: cfg.IsClusterMode,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
if !cfg.DisableStartupCheck {
|
||||||
// Test connection
|
// Test connection
|
||||||
if err := db.Ping(context.Background()).Err(); err != nil {
|
if err := db.Ping(context.Background()).Err(); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
@@ -76,6 +77,7 @@ func New(config ...Config) *Storage {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Create new store
|
// Create new store
|
||||||
return &Storage{
|
return &Storage{
|
||||||
|
@@ -198,6 +198,19 @@ func Test_Redis_Delete(t *testing.T) {
|
|||||||
require.Nil(t, keys)
|
require.Nil(t, keys)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_Redis_New_DisableStartupCheck(t *testing.T) {
|
||||||
|
require.NotPanics(t, func() {
|
||||||
|
store := New(Config{
|
||||||
|
Host: "127.0.0.1",
|
||||||
|
Port: 6390,
|
||||||
|
Addrs: []string{"127.0.0.1:6390"},
|
||||||
|
DisableStartupCheck: true,
|
||||||
|
})
|
||||||
|
require.NotNil(t, store)
|
||||||
|
_ = store.Close()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func Test_Redis_DeleteWithContext(t *testing.T) {
|
func Test_Redis_DeleteWithContext(t *testing.T) {
|
||||||
var (
|
var (
|
||||||
key = "john"
|
key = "john"
|
||||||
|
@@ -184,6 +184,13 @@ type Config struct {
|
|||||||
// Optional. Default is false
|
// Optional. Default is false
|
||||||
Reset bool
|
Reset bool
|
||||||
|
|
||||||
|
// 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
|
||||||
|
DisableStartupCheck bool
|
||||||
|
|
||||||
// CacheTTL TTL
|
// CacheTTL TTL
|
||||||
//
|
//
|
||||||
// Optional. Default is time.Minute
|
// Optional. Default is time.Minute
|
||||||
@@ -210,6 +217,7 @@ var ConfigDefault = Config{
|
|||||||
DisableCache: false,
|
DisableCache: false,
|
||||||
AlwaysPipelining: true,
|
AlwaysPipelining: true,
|
||||||
Reset: false,
|
Reset: false,
|
||||||
|
DisableStartupCheck: false,
|
||||||
CacheTTL: time.Minute,
|
CacheTTL: time.Minute,
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@@ -95,6 +95,13 @@ type Config struct {
|
|||||||
// Optional. Default is false
|
// Optional. Default is false
|
||||||
Reset bool
|
Reset bool
|
||||||
|
|
||||||
|
// 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
|
||||||
|
DisableStartupCheck bool
|
||||||
|
|
||||||
// CacheTTL TTL
|
// CacheTTL TTL
|
||||||
//
|
//
|
||||||
// Optional. Default is time.Minute
|
// Optional. Default is time.Minute
|
||||||
@@ -120,6 +127,7 @@ var ConfigDefault = Config{
|
|||||||
DisableCache: false,
|
DisableCache: false,
|
||||||
AlwaysPipelining: true,
|
AlwaysPipelining: true,
|
||||||
Reset: false,
|
Reset: false,
|
||||||
|
DisableStartupCheck: false,
|
||||||
CacheTTL: time.Minute,
|
CacheTTL: time.Minute,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -194,5 +202,9 @@ func configDefault(config ...Config) Config {
|
|||||||
cfg.Reset = true
|
cfg.Reset = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if userConfig.DisableStartupCheck {
|
||||||
|
cfg.DisableStartupCheck = true
|
||||||
|
}
|
||||||
|
|
||||||
return cfg
|
return cfg
|
||||||
}
|
}
|
||||||
|
@@ -12,6 +12,7 @@ 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
|
||||||
@@ -45,7 +46,6 @@ func New(config ...Config) *Storage {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update config values accordingly and start new Client
|
|
||||||
db, err := rueidis.NewClient(rueidis.ClientOption{
|
db, err := rueidis.NewClient(rueidis.ClientOption{
|
||||||
Username: cfg.Username,
|
Username: cfg.Username,
|
||||||
Password: cfg.Password,
|
Password: cfg.Password,
|
||||||
@@ -64,9 +64,14 @@ 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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !cfg.DisableStartupCheck {
|
||||||
// Test connection
|
// Test connection
|
||||||
if err := db.Do(context.Background(), db.B().Ping().Build()).Error(); err != nil {
|
if err := db.Do(context.Background(), db.B().Ping().Build()).Error(); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
@@ -78,6 +83,7 @@ func New(config ...Config) *Storage {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Create new store
|
// Create new store
|
||||||
return &Storage{
|
return &Storage{
|
||||||
@@ -87,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
|
||||||
}
|
}
|
||||||
@@ -104,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
|
||||||
}
|
}
|
||||||
@@ -121,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
|
||||||
}
|
}
|
||||||
@@ -134,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()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -144,6 +162,10 @@ func (s *Storage) Reset() error {
|
|||||||
|
|
||||||
// Close the database
|
// Close the database
|
||||||
func (s *Storage) Close() error {
|
func (s *Storage) Close() error {
|
||||||
|
if s.db == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
s.db.Close()
|
s.db.Close()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@@ -49,6 +49,17 @@ func newTestStore(t testing.TB, opts ...testredis.Option) *Storage {
|
|||||||
return New(newConfigFromContainer(t, opts...))
|
return New(newConfigFromContainer(t, opts...))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_Rueidis_New_DisableStartupCheck(t *testing.T) {
|
||||||
|
require.NotPanics(t, func() {
|
||||||
|
store := New(Config{
|
||||||
|
InitAddress: []string{"127.0.0.1:6390"},
|
||||||
|
DisableStartupCheck: true,
|
||||||
|
})
|
||||||
|
require.NotNil(t, store)
|
||||||
|
require.NoError(t, store.Close())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func Test_Rueidis_Set(t *testing.T) {
|
func Test_Rueidis_Set(t *testing.T) {
|
||||||
var (
|
var (
|
||||||
key = "john"
|
key = "john"
|
||||||
|
Reference in New Issue
Block a user