Skip to content

Commit

Permalink
Merge pull request #2 from AliiAhmadi/main
Browse files Browse the repository at this point in the history
+ avoid repeating assignment in 'SetWithTimeout' function by using 'S…
  • Loading branch information
knbr13 authored Mar 31, 2024
2 parents 0f79609 + 12c526c commit 24b45df
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down

0 comments on commit 24b45df

Please sign in to comment.