From 65eda42f9dea140397446fca8225f8ed68428955 Mon Sep 17 00:00:00 2001 From: phuslu Date: Tue, 2 Jan 2024 00:04:24 +0800 Subject: [PATCH] more accuracy for timestamp --- shard.go | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/shard.go b/shard.go index fdecace..6df4c20 100644 --- a/shard.go +++ b/shard.go @@ -20,7 +20,7 @@ func (s *shard[K, V]) Get(hash uint32, key K) (value V, ok bool) { s.mu.Lock() if index, exists := s.table.Get(hash, key); exists { - if timestamp := s.list.nodes[index].expires; timestamp == 0 || atomic.LoadInt64(&now) < timestamp { + if now := s.list.nodes[index].expires; now == 0 || atomic.LoadInt64(&clock) < now { // s.list.MoveToFront(index) if s.list.nodes[0].next != index { s.list.move(index, 0) @@ -62,7 +62,7 @@ func (s *shard[K, V]) Set(hash uint32, hashfun func(K) uint32, key K, value V, t s.list.MoveToFront(index) node.value = value if ttl > 0 { - node.expires = atomic.LoadInt64(&now) + int64(ttl) + node.expires = atomic.LoadInt64(&clock) + int64(ttl) } prev = previousValue replaced = true @@ -77,7 +77,7 @@ func (s *shard[K, V]) Set(hash uint32, hashfun func(K) uint32, key K, value V, t node.key = key node.value = value if ttl > 0 { - node.expires = atomic.LoadInt64(&now) + int64(ttl) + node.expires = atomic.LoadInt64(&clock) + int64(ttl) } s.table.Set(hash, key, index) s.list.MoveToFront(index) @@ -122,14 +122,18 @@ func newshard[K comparable, V any](size int) *shard[K, V] { return s } -var now int64 +var clock int64 func init() { - atomic.StoreInt64(&now, time.Now().UnixNano()) + atomic.StoreInt64(&clock, time.Now().UnixNano()) go func() { for { - time.Sleep(time.Second) - atomic.StoreInt64(&now, time.Now().UnixNano()) + for i := 0; i < 9; i++ { + time.Sleep(100 * time.Millisecond) + atomic.AddInt64(&clock, int64(100*time.Millisecond)) + } + time.Sleep(100 * time.Millisecond) + atomic.StoreInt64(&clock, time.Now().UnixNano()) } }() }