Skip to content

Commit

Permalink
add expires to peek method
Browse files Browse the repository at this point in the history
  • Loading branch information
phuslu committed Feb 22, 2024
1 parent fbf3222 commit 68178d0
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
4 changes: 2 additions & 2 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ func (c *Cache[K, V]) GetOrLoad(key K) (value V, err error, ok bool) {
return
}

// Peek returns value for key, but does not modify its recency.
func (c *Cache[K, V]) Peek(key K) (value V, ok bool) {
// Peek returns value and expires nanoseconds for key, but does not modify its recency.
func (c *Cache[K, V]) Peek(key K) (value V, expires int64, ok bool) {
hash := uint32(c.hasher(key))
return c.shards[hash&c.mask].Peek(hash, key)
}
Expand Down
14 changes: 7 additions & 7 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,26 +185,26 @@ func TestCachePeek(t *testing.T) {
cache := New[int, int](64)

cache.Set(10, 10, 0)
cache.Set(20, 20, 0)
if v, ok := cache.Peek(10); !ok || v != 10 {
t.Errorf("10 should be set to 10: %v,", v)
cache.Set(20, 20, time.Hour)
if v, expires, ok := cache.Peek(10); !ok || v != 10 || expires != 0 {
t.Errorf("10 should be set to 10: %v, %v", v, expires)
}

if v, ok := cache.Peek(20); !ok || v != 20 {
if v, expires, ok := cache.Peek(20); !ok || v != 20 || expires == 0 {
t.Errorf("20 should be set to 20: %v,", v)
}

if v, ok := cache.Peek(30); ok || v != 0 {
if v, expires, ok := cache.Peek(30); ok || v != 0 || expires != 0 {
t.Errorf("30 should be set to 0: %v,", v)
}

for k := 3; k < 1024; k++ {
cache.Set(k, k, 0)
}
if v, ok := cache.Peek(10); ok || v == 10 {
if v, _, ok := cache.Peek(10); ok || v == 10 {
t.Errorf("%v should not have updated recent-ness of 10", v)
}
if v, ok := cache.Peek(30); ok || v != 0 {
if v, _, ok := cache.Peek(30); ok || v != 0 {
t.Errorf("%v should have updated recent-ness of 30", v)
}
}
Expand Down
4 changes: 2 additions & 2 deletions clock.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
// always use `atomic.LoadUint32(&clock)` for accessing clock value.
var clock uint32

func clocking() {
const clockBase = 1704067200 // 2024-01-01T00:00:00Z
const clockBase = 1704067200 // 2024-01-01T00:00:00Z

func clocking() {
if atomic.LoadUint32(&clock) > 0 {
return
}
Expand Down
5 changes: 4 additions & 1 deletion shard.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,14 @@ func (s *shard[K, V]) Get(hash uint32, key K) (value V, ok bool) {
return
}

func (s *shard[K, V]) Peek(hash uint32, key K) (value V, ok bool) {
func (s *shard[K, V]) Peek(hash uint32, key K) (value V, expires int64, ok bool) {
s.mu.Lock()

if index, exists := s.table_Get(hash, key); exists {
value = s.list[index].value
if e := s.list[index].expires; e > 0 {
expires = (int64(e) + clockBase) * int64(time.Second)
}
ok = true
}

Expand Down

0 comments on commit 68178d0

Please sign in to comment.