From e6da00b429e53a4d5f80e0866e30ee7334d7122d Mon Sep 17 00:00:00 2001 From: Fenny <25108519+Fenny@users.noreply.github.com> Date: Thu, 5 Nov 2020 08:15:48 +0100 Subject: [PATCH] Update postgres.go --- postgres/postgres.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/postgres/postgres.go b/postgres/postgres.go index 35b79bca..bd586a23 100644 --- a/postgres/postgres.go +++ b/postgres/postgres.go @@ -98,7 +98,7 @@ func New(config ...Config) *Storage { db: db, gcInterval: cfg.GCInterval, sqlSelect: fmt.Sprintf(`SELECT v, e FROM %s WHERE k=$1;`, cfg.Table), - sqlInsert: fmt.Sprintf(`INSERT INTO %s (k, v, e) VALUES ($1, $2, $3)`, cfg.Table), + sqlInsert: fmt.Sprintf("INSERT INTO %s (k, v, e) VALUES ($1, $2, $3) ON DUPLICATE KEY UPDATE v = $4, e = $5", cfg.Table), sqlDelete: fmt.Sprintf("DELETE FROM %s WHERE k=$1", cfg.Table), sqlClear: fmt.Sprintf("DELETE FROM %s;", cfg.Table), sqlGC: fmt.Sprintf("DELETE FROM %s WHERE e <= $1", cfg.Table), @@ -141,7 +141,12 @@ func (s *Storage) Set(key string, val []byte, exp time.Duration) error { if len(val) <= 0 { return nil } - _, err := s.db.Exec(s.sqlInsert, key, utils.UnsafeString(val), time.Now().Add(exp).Unix()) + var expSeconds int64 + if exp != 0 { + expSeconds = time.Now().Add(exp).Unix() + } + valStr := utils.UnsafeString(val) + _, err := s.db.Exec(s.sqlInsert, key, valStr, expSeconds, valStr, expSeconds) return err }