mirror of
https://github.com/gofiber/storage.git
synced 2025-10-03 07:46:42 +08:00
fix: time.Time.IsZero returning false negative
This commit is contained in:
@@ -2,6 +2,8 @@ package clickhouse
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -26,17 +28,15 @@ func New(configuration Config) (*Storage, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx := driver.Context(context.Background(), driver.WithParameters(driver.Parameters{
|
ctx := context.Background()
|
||||||
"table": configuration.Table,
|
|
||||||
}))
|
queryWithEngine := fmt.Sprintf(createTableString, engine)
|
||||||
|
if err := conn.Exec(ctx, queryWithEngine, driver.Named("table", configuration.Table)); err != nil {
|
||||||
|
return &Storage{}, err
|
||||||
|
}
|
||||||
|
|
||||||
if configuration.Clean {
|
if configuration.Clean {
|
||||||
queryWithEngine := fmt.Sprintf(createTableString, engine)
|
if err := conn.Exec(ctx, resetDataString, driver.Named("table", configuration.Table)); err != nil {
|
||||||
if err := conn.Exec(ctx, queryWithEngine); err != nil {
|
|
||||||
return &Storage{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := conn.Exec(ctx, resetDataString); err != nil {
|
|
||||||
return &Storage{}, err
|
return &Storage{}, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -63,59 +63,63 @@ func (s *Storage) Set(key string, value []byte, expiration time.Duration) error
|
|||||||
exp = time.Now().Add(expiration).UTC()
|
exp = time.Now().Add(expiration).UTC()
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx := driver.Context(s.context, driver.WithParameters(driver.Parameters{
|
return s.
|
||||||
"key": key,
|
|
||||||
"value": string(value),
|
|
||||||
"expiration": exp.Format("2006-01-02 15:04:05"),
|
|
||||||
"table": s.table,
|
|
||||||
}))
|
|
||||||
|
|
||||||
err := s.
|
|
||||||
session.
|
session.
|
||||||
Exec(
|
Exec(
|
||||||
ctx,
|
s.context,
|
||||||
insertDataString,
|
insertDataString,
|
||||||
|
driver.Named("table", s.table),
|
||||||
|
driver.Named("key", key),
|
||||||
|
driver.Named("value", string(value)),
|
||||||
|
driver.Named("expiration", exp.Format("2006-01-02 15:04:05")),
|
||||||
)
|
)
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Storage) Get(key string) ([]byte, error) {
|
func (s *Storage) Get(key string) ([]byte, error) {
|
||||||
var resultSlice []schema
|
if len(key) == 0 {
|
||||||
|
return []byte{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var result schema
|
||||||
|
|
||||||
|
row := s.session.QueryRow(
|
||||||
|
s.context,
|
||||||
|
selectDataString,
|
||||||
|
driver.Named("table", s.table),
|
||||||
|
driver.Named("key", key),
|
||||||
|
)
|
||||||
|
if row.Err() != nil {
|
||||||
|
return []byte{}, row.Err()
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := row.ScanStruct(&result); err != nil {
|
||||||
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
|
return []byte{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
ctx := driver.Context(s.context, driver.WithParameters(driver.Parameters{
|
|
||||||
"key": key,
|
|
||||||
"table": s.table,
|
|
||||||
}))
|
|
||||||
err := s.session.Select(ctx, &resultSlice, selectDataString)
|
|
||||||
if err != nil {
|
|
||||||
return []byte{}, err
|
return []byte{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(resultSlice) == 0 {
|
// The result.Expiration.IsZero() was returning a false value even when the time was
|
||||||
|
// set to be the zero value of the time.Time struct (Jan 1st 1970, 00:00:00 UTC)
|
||||||
|
// so we had to change the comparision
|
||||||
|
if !time.Unix(0, 0).Equal(result.Expiration) && result.Expiration.Before(time.Now().UTC()) {
|
||||||
return []byte{}, nil
|
return []byte{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
result := resultSlice[0]
|
return []byte(result.Value), nil
|
||||||
|
|
||||||
if !result.Expiration.IsZero() && result.Expiration.Before(time.Now().UTC()) {
|
|
||||||
return []byte{}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return []byte(result.Value), err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Storage) Delete(key string) error {
|
func (s *Storage) Delete(key string) error {
|
||||||
ctx := driver.Context(s.context, driver.WithParameters(driver.Parameters{
|
if len(key) == 0 {
|
||||||
"table": s.table,
|
return nil
|
||||||
"key": key,
|
}
|
||||||
}))
|
|
||||||
|
|
||||||
return s.session.Exec(ctx, deleteDataString)
|
return s.session.Exec(s.context, deleteDataString, driver.Named("table", s.table), driver.Named("key", key))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Storage) Reset() error {
|
func (s *Storage) Reset() error {
|
||||||
return s.session.Exec(s.context, resetDataString)
|
return s.session.Exec(s.context, resetDataString, driver.Named("table", s.table))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Storage) Close() error {
|
func (s *Storage) Close() error {
|
||||||
|
@@ -47,7 +47,7 @@ func Test_Set(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_SetWithExp(t *testing.T) {
|
func Test_Set_With_Exp(t *testing.T) {
|
||||||
client, err := getTestConnection(t, Config{
|
client, err := getTestConnection(t, Config{
|
||||||
Host: "127.0.0.1",
|
Host: "127.0.0.1",
|
||||||
Port: 9001,
|
Port: 9001,
|
||||||
@@ -83,7 +83,7 @@ func Test_Get(t *testing.T) {
|
|||||||
assert.Equal(t, "somevalue", string(value))
|
assert.Equal(t, "somevalue", string(value))
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_GetWithExp(t *testing.T) {
|
func Test_Get_With_Exp(t *testing.T) {
|
||||||
client, err := getTestConnection(t, Config{
|
client, err := getTestConnection(t, Config{
|
||||||
Host: "127.0.0.1",
|
Host: "127.0.0.1",
|
||||||
Port: 9001,
|
Port: 9001,
|
||||||
@@ -94,7 +94,7 @@ func Test_GetWithExp(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
defer client.Close()
|
defer client.Close()
|
||||||
|
|
||||||
err = client.Set("getsomekeywithexp", []byte("somevalue"), time.Second*5)
|
err = client.Set("getsomekeywithexp", []byte("somevalue"), time.Second*2)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
value, err := client.Get("getsomekeywithexp")
|
value, err := client.Get("getsomekeywithexp")
|
||||||
@@ -103,7 +103,7 @@ func Test_GetWithExp(t *testing.T) {
|
|||||||
assert.NotNil(t, value)
|
assert.NotNil(t, value)
|
||||||
assert.Equal(t, "somevalue", string(value))
|
assert.Equal(t, "somevalue", string(value))
|
||||||
|
|
||||||
time.Sleep(time.Second * 10)
|
time.Sleep(time.Second * 5)
|
||||||
|
|
||||||
value, err = client.Get("getsomekeywithexp")
|
value, err = client.Get("getsomekeywithexp")
|
||||||
|
|
||||||
|
@@ -4,6 +4,7 @@ import (
|
|||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
driver "github.com/ClickHouse/clickhouse-go/v2"
|
driver "github.com/ClickHouse/clickhouse-go/v2"
|
||||||
@@ -64,7 +65,7 @@ func defaultConfig(configuration Config) (driver.Options, ClickhouseEngine, erro
|
|||||||
}
|
}
|
||||||
|
|
||||||
if configuration.Engine == "" {
|
if configuration.Engine == "" {
|
||||||
configuration.Engine = "Memory"
|
configuration.Engine = Memory
|
||||||
}
|
}
|
||||||
|
|
||||||
config := driver.Options{
|
config := driver.Options{
|
||||||
@@ -84,7 +85,7 @@ func defaultConfig(configuration Config) (driver.Options, ClickhouseEngine, erro
|
|||||||
}
|
}
|
||||||
|
|
||||||
if configuration.Debug && config.Debugf == nil {
|
if configuration.Debug && config.Debugf == nil {
|
||||||
config.Debugf = func(format string, v ...any) { fmt.Printf(format, v...) }
|
config.Debugf = log.Printf
|
||||||
}
|
}
|
||||||
|
|
||||||
return config, configuration.Engine, nil
|
return config, configuration.Engine, nil
|
||||||
|
@@ -1,5 +1,7 @@
|
|||||||
github.com/ClickHouse/ch-go v0.61.5 h1:zwR8QbYI0tsMiEcze/uIMK+Tz1D3XZXLdNrlaOpeEI4=
|
github.com/ClickHouse/ch-go v0.61.5 h1:zwR8QbYI0tsMiEcze/uIMK+Tz1D3XZXLdNrlaOpeEI4=
|
||||||
github.com/ClickHouse/ch-go v0.61.5/go.mod h1:s1LJW/F/LcFs5HJnuogFMta50kKDO0lf9zzfrbl0RQg=
|
github.com/ClickHouse/ch-go v0.61.5/go.mod h1:s1LJW/F/LcFs5HJnuogFMta50kKDO0lf9zzfrbl0RQg=
|
||||||
|
github.com/ClickHouse/clickhouse-go/v2 v2.24.0 h1:L/n/pVVpk95KtkHOiKuSnO7cu2ckeW4gICbbOh5qs74=
|
||||||
|
github.com/ClickHouse/clickhouse-go/v2 v2.24.0/go.mod h1:iDTViXk2Fgvf1jn2dbJd1ys+fBkdD1UMRnXlwmhijhQ=
|
||||||
github.com/ClickHouse/clickhouse-go/v2 v2.26.0 h1:j4/y6NYaCcFkJwN/TU700ebW+nmsIy34RmUAAcZKy9w=
|
github.com/ClickHouse/clickhouse-go/v2 v2.26.0 h1:j4/y6NYaCcFkJwN/TU700ebW+nmsIy34RmUAAcZKy9w=
|
||||||
github.com/ClickHouse/clickhouse-go/v2 v2.26.0/go.mod h1:iDTViXk2Fgvf1jn2dbJd1ys+fBkdD1UMRnXlwmhijhQ=
|
github.com/ClickHouse/clickhouse-go/v2 v2.26.0/go.mod h1:iDTViXk2Fgvf1jn2dbJd1ys+fBkdD1UMRnXlwmhijhQ=
|
||||||
github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M=
|
github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M=
|
||||||
|
Reference in New Issue
Block a user