diff --git a/memcache/memcache_test.go b/memcache/memcache_test.go index 7042c6ee..a3100896 100644 --- a/memcache/memcache_test.go +++ b/memcache/memcache_test.go @@ -39,14 +39,13 @@ func Test_Memcache_Set_Expiration(t *testing.T) { var ( key = "john" val = []byte("doe") - exp = 500 * time.Millisecond + exp = 1 * time.Second ) err := testStore.Set(key, val, exp) utils.AssertEqual(t, nil, err) - time.Sleep(1 * time.Second) - + time.Sleep(1100 * time.Millisecond) } func Test_Memcache_Get_Expired(t *testing.T) { diff --git a/memory/memory_test.go b/memory/memory_test.go index ee38757f..2adb1fcc 100644 --- a/memory/memory_test.go +++ b/memory/memory_test.go @@ -39,14 +39,13 @@ func Test_Memory_Set_Expiration(t *testing.T) { var ( key = "john" val = []byte("doe") - exp = 500 * time.Millisecond + exp = 1 * time.Second ) err := testStore.Set(key, val, exp) utils.AssertEqual(t, nil, err) - time.Sleep(1 * time.Second) - + time.Sleep(1100 * time.Millisecond) } func Test_Memory_Get_Expired(t *testing.T) { @@ -61,7 +60,6 @@ func Test_Memory_Get_Expired(t *testing.T) { func Test_Memory_Get_NotExist(t *testing.T) { - result, err := testStore.Get("notexist") utils.AssertEqual(t, ErrNotExist, err) utils.AssertEqual(t, true, len(result) == 0) diff --git a/mongodb/mongodb_test.go b/mongodb/mongodb_test.go index 16763424..b2f2395f 100644 --- a/mongodb/mongodb_test.go +++ b/mongodb/mongodb_test.go @@ -41,14 +41,13 @@ func Test_MongoDB_Set_Expiration(t *testing.T) { var ( key = "john" val = []byte("doe") - exp = 500 * time.Millisecond + exp = 1 * time.Second ) err := testStore.Set(key, val, exp) utils.AssertEqual(t, nil, err) - time.Sleep(1 * time.Second) - + time.Sleep(1100 * time.Millisecond) } func Test_MongoDB_Get_Expired(t *testing.T) { diff --git a/mysql/mysql.go b/mysql/mysql.go index 11b66173..5d39be46 100644 --- a/mysql/mysql.go +++ b/mysql/mysql.go @@ -76,7 +76,7 @@ func New(config ...Config) *Storage { gcInterval: cfg.GCInterval, db: db, sqlSelect: fmt.Sprintf(`SELECT data, exp FROM %s WHERE id=?;`, cfg.Table), - sqlInsert: fmt.Sprintf("INSERT INTO %s (id, data, exp) VALUES (?,?,?)", cfg.Table), + sqlInsert: fmt.Sprintf("INSERT OR REPLACE INTO %s (id, data, exp) VALUES (?,?,?)", cfg.Table), sqlDelete: fmt.Sprintf("DELETE FROM %s WHERE id=?", cfg.Table), sqlClear: fmt.Sprintf("DELETE FROM %s;", cfg.Table), sqlGC: fmt.Sprintf("DELETE FROM %s WHERE exp <= ?", cfg.Table), diff --git a/mysql/mysql_test.go b/mysql/mysql_test.go index c3d1f8de..4958e909 100644 --- a/mysql/mysql_test.go +++ b/mysql/mysql_test.go @@ -45,14 +45,13 @@ func Test_MYSQL_Set_Expiration(t *testing.T) { var ( key = "john" val = []byte("doe") - exp = 500 * time.Millisecond + exp = 1 * time.Second ) err := testStore.Set(key, val, exp) utils.AssertEqual(t, nil, err) - time.Sleep(1 * time.Second) - + time.Sleep(1100 * time.Millisecond) } func Test_MYSQL_Get_Expired(t *testing.T) { diff --git a/postgres/postgres.go b/postgres/postgres.go index b3a6fcc8..de7ee7d3 100644 --- a/postgres/postgres.go +++ b/postgres/postgres.go @@ -99,7 +99,7 @@ func New(config ...Config) *Storage { db: db, gcInterval: cfg.GCInterval, sqlSelect: fmt.Sprintf(`SELECT data, exp FROM %s WHERE key=$1;`, cfg.Table), - sqlInsert: fmt.Sprintf("INSERT INTO %s (key, data, exp) VALUES ($1, $2, $3)", cfg.Table), + sqlInsert: fmt.Sprintf("INSERT OR REPLACE INTO %s (key, data, exp) VALUES ($1, $2, $3)", cfg.Table), sqlDelete: fmt.Sprintf("DELETE FROM %s WHERE key=$1", cfg.Table), sqlClear: fmt.Sprintf("DELETE FROM %s;", cfg.Table), sqlGC: fmt.Sprintf("DELETE FROM %s WHERE exp <= $1", cfg.Table), diff --git a/postgres/postgres_test.go b/postgres/postgres_test.go index e166a69f..9b2064b3 100644 --- a/postgres/postgres_test.go +++ b/postgres/postgres_test.go @@ -45,14 +45,13 @@ func Test_Postgres_Set_Expiration(t *testing.T) { var ( key = "john" val = []byte("doe") - exp = 500 * time.Millisecond + exp = 1 * time.Second ) - err := testStore.(key, val, exp) + err := testStore.Set(key, val, exp) utils.AssertEqual(t, nil, err) - time.Sleep(1 * time.Second) - + time.Sleep(1100 * time.Millisecond) } func Test_Postgres_Get_Expired(t *testing.T) { diff --git a/redis/redis_test.go b/redis/redis_test.go index b22efec7..92e34300 100644 --- a/redis/redis_test.go +++ b/redis/redis_test.go @@ -41,14 +41,13 @@ func Test_Redis_Set_Expiration(t *testing.T) { var ( key = "john" val = []byte("doe") - exp = 500 * time.Millisecond + exp = 1 * time.Second ) err := testStore.Set(key, val, exp) utils.AssertEqual(t, nil, err) - time.Sleep(1 * time.Second) - + time.Sleep(1100 * time.Millisecond) } func Test_Redis_Get_Expired(t *testing.T) { diff --git a/sqlite3/fiber.sqlite3 b/sqlite3/fiber.sqlite3 new file mode 100644 index 00000000..df1ff749 Binary files /dev/null and b/sqlite3/fiber.sqlite3 differ diff --git a/sqlite3/sqlite3.go b/sqlite3/sqlite3.go index f51ca6e6..dab712db 100644 --- a/sqlite3/sqlite3.go +++ b/sqlite3/sqlite3.go @@ -81,7 +81,7 @@ func New(config ...Config) *Storage { db: db, gcInterval: cfg.GCInterval, sqlSelect: fmt.Sprintf(`SELECT data, exp FROM %s WHERE key=?;`, cfg.Table), - sqlInsert: fmt.Sprintf("INSERT INTO %s (key, data, exp) VALUES (?,?,?)", cfg.Table), + sqlInsert: fmt.Sprintf("INSERT OR REPLACE INTO %s (key, data, exp) VALUES (?,?,?)", cfg.Table), sqlDelete: fmt.Sprintf("DELETE FROM %s WHERE key=?", cfg.Table), sqlClear: fmt.Sprintf("DELETE FROM %s;", cfg.Table), sqlGC: fmt.Sprintf("DELETE FROM %s WHERE exp <= ?", cfg.Table), @@ -96,7 +96,6 @@ func New(config ...Config) *Storage { // Get value by key func (s *Storage) Get(key string) ([]byte, error) { row := s.db.QueryRow(s.sqlSelect, key) - // Add db response to data var ( data = []byte{} @@ -108,10 +107,9 @@ func (s *Storage) Get(key string) ([]byte, error) { } return nil, err } - // If the expiration time has already passed, then return nil if exp != 0 && exp <= time.Now().Unix() { - return nil, nil + return nil, ErrNotExist } return data, nil diff --git a/sqlite3/sqlite3_test.go b/sqlite3/sqlite3_test.go index 0b00bb6a..07c72b84 100644 --- a/sqlite3/sqlite3_test.go +++ b/sqlite3/sqlite3_test.go @@ -42,14 +42,13 @@ func Test_SQLite3_Set_Expiration(t *testing.T) { var ( key = "john" val = []byte("doe") - exp = 500 * time.Millisecond + exp = 1 * time.Second ) err := testStore.Set(key, val, exp) utils.AssertEqual(t, nil, err) - time.Sleep(1 * time.Second) - + time.Sleep(1100 * time.Millisecond) } func Test_SQLite3_Get_Expired(t *testing.T) {