From 12c526cb745c6ddf13416da8b2ad3c962b4d355e Mon Sep 17 00:00:00 2001 From: AliiAhmadi Date: Sun, 31 Mar 2024 14:27:03 +0330 Subject: [PATCH] + avoid repeating assignment in 'SetWithTimeout' function by using 'Set' method if timeout is invalid --- db.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/db.go b/db.go index 34a3ff1..1838bca 100644 --- a/db.go +++ b/db.go @@ -84,20 +84,18 @@ func (d *DB[K, V]) SetWithTimeout(k K, v V, timeout time.Duration) { d.Set(k, v) return } - d.mu.Lock() - defer d.mu.Unlock() if timeout > 0 { + d.mu.Lock() + defer d.mu.Unlock() + now := time.Now().Add(timeout) d.m[k] = valueWithTimeout[V]{ value: v, expireAt: &now, } } else { - d.m[k] = valueWithTimeout[V]{ - value: v, - expireAt: nil, - } + d.Set(k, v) } }