diff --git a/postgres/postgres.go b/postgres/postgres.go index 3250b187..3175906d 100644 --- a/postgres/postgres.go +++ b/postgres/postgres.go @@ -129,12 +129,12 @@ func New(config ...Config) *Storage { return store } -// Get value by key -func (s *Storage) Get(key string) ([]byte, error) { +// GetWithContext gets value by key with context +func (s *Storage) GetWithContext(ctx context.Context, key string) ([]byte, error) { if len(key) <= 0 { return nil, nil } - row := s.db.QueryRow(context.Background(), s.sqlSelect, key) + row := s.db.QueryRow(ctx, s.sqlSelect, key) // Add db response to data var ( data []byte @@ -155,8 +155,13 @@ func (s *Storage) Get(key string) ([]byte, error) { return data, nil } -// Set key with value -func (s *Storage) Set(key string, val []byte, exp time.Duration) error { +// Get gets value by key +func (s *Storage) Get(key string) ([]byte, error) { + return s.GetWithContext(context.Background(), key) +} + +// SetWithContext sets key with value +func (s *Storage) SetWithContext(ctx context.Context, key string, val []byte, exp time.Duration) error { // Ain't Nobody Got Time For That if len(key) <= 0 || len(val) <= 0 { return nil @@ -165,26 +170,41 @@ func (s *Storage) Set(key string, val []byte, exp time.Duration) error { if exp != 0 { expSeconds = time.Now().Add(exp).Unix() } - _, err := s.db.Exec(context.Background(), s.sqlInsert, key, val, expSeconds, val, expSeconds) + _, err := s.db.Exec(ctx, s.sqlInsert, key, val, expSeconds, val, expSeconds) return err } -// Delete entry by key -func (s *Storage) Delete(key string) error { +// Set sets key with value +func (s *Storage) Set(key string, val []byte, exp time.Duration) error { + return s.SetWithContext(context.Background(), key, val, exp) +} + +// DeleteWithContext deletes entry by key +func (s *Storage) DeleteWithContext(ctx context.Context, key string) error { // Ain't Nobody Got Time For That if len(key) <= 0 { return nil } - _, err := s.db.Exec(context.Background(), s.sqlDelete, key) + _, err := s.db.Exec(ctx, s.sqlDelete, key) return err } -// Reset all entries, including unexpired -func (s *Storage) Reset() error { - _, err := s.db.Exec(context.Background(), s.sqlReset) +// Delete deletes entry by key +func (s *Storage) Delete(key string) error { + return s.DeleteWithContext(context.Background(), key) +} + +// ResetWithContext resets all entries with context, including unexpired ones +func (s *Storage) ResetWithContext(ctx context.Context) error { + _, err := s.db.Exec(ctx, s.sqlReset) return err } +// Reset resets all entries, including unexpired ones +func (s *Storage) Reset() error { + return s.ResetWithContext(context.Background()) +} + // Close the database func (s *Storage) Close() error { s.done <- struct{}{} diff --git a/postgres/postgres_test.go b/postgres/postgres_test.go index 25cb7f6e..55c79db5 100644 --- a/postgres/postgres_test.go +++ b/postgres/postgres_test.go @@ -239,6 +239,19 @@ func Test_Postgres_Set(t *testing.T) { require.NoError(t, err) } +func Test_Postgres_SetWithContext(t *testing.T) { + var ( + key = "john" + val = []byte("doe") + ) + + ctx, cancel := context.WithCancel(context.Background()) + cancel() + + err := testStore.SetWithContext(ctx, key, val, 0) + require.ErrorIs(t, err, context.Canceled) +} + func Test_Postgres_Set_Override(t *testing.T) { var ( key = "john" @@ -272,6 +285,23 @@ func Test_Postgres_Get(t *testing.T) { require.Equal(t, val, result) } +func Test_Postgres_GetWithContext(t *testing.T) { + var ( + key = "john" + val = []byte("doe") + ) + + err := testStore.Set(key, val, 0) + require.NoError(t, err) + + ctx, cancel := context.WithCancel(context.Background()) + cancel() + + result, err := testStore.GetWithContext(ctx, key) + require.ErrorIs(t, err, context.Canceled) + require.Zero(t, len(result)) +} + func Test_Postgres_Set_Expiration(t *testing.T) { var ( key = "john" @@ -332,6 +362,22 @@ func Test_Postgres_Delete(t *testing.T) { require.Zero(t, len(result)) } +func Test_Postgres_DeleteWithContext(t *testing.T) { + var ( + key = "john" + val = []byte("doe") + ) + + err := testStore.Set(key, val, 0) + require.NoError(t, err) + + ctx, cancel := context.WithCancel(context.Background()) + cancel() + + err = testStore.DeleteWithContext(ctx, key) + require.ErrorIs(t, err, context.Canceled) +} + func Test_Postgres_Reset(t *testing.T) { val := []byte("doe") @@ -356,6 +402,30 @@ func Test_Postgres_Reset(t *testing.T) { require.Zero(t, len(result)) } +func Test_Postgres_ResetWithContext(t *testing.T) { + val := []byte("doe") + + err := testStore.Set("john1", val, 0) + require.NoError(t, err) + + err = testStore.Set("john2", val, 0) + require.NoError(t, err) + + ctx, cancel := context.WithCancel(context.Background()) + cancel() + + err = testStore.ResetWithContext(ctx) + require.ErrorIs(t, err, context.Canceled) + + result, err := testStore.Get("john1") + require.NoError(t, err) + require.NotZero(t, len(result)) + + result, err = testStore.Get("john2") + require.NoError(t, err) + require.NotZero(t, len(result)) +} + func Test_Postgres_GC(t *testing.T) { testVal := []byte("doe")