Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
187 changes: 0 additions & 187 deletions experiment_results.md

This file was deleted.

27 changes: 27 additions & 0 deletions memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package fido

import (
"iter"
"sync"
"time"

Expand Down Expand Up @@ -138,6 +139,32 @@ func (c *Cache[K, V]) Flush() int {
return c.memory.flush()
}

// Range returns an iterator over all non-expired key-value pairs.
// Iteration order is undefined. Safe for concurrent use.
// Changes during iteration may or may not be reflected.
func (c *Cache[K, V]) Range() iter.Seq2[K, V] {
return func(yield func(K, V) bool) {
//nolint:gosec // G115: Unix seconds fit in uint32 until year 2106
now := uint32(time.Now().Unix())
c.memory.entries.Range(func(key K, e *entry[K, V]) bool {
// Skip expired entries.
expiry := e.expirySec.Load()
if expiry != 0 && expiry < now {
return true
}

// Load value with seqlock.
v, ok := e.loadValue()
if !ok {
return true
}

// Yield to caller.
return yield(key, v)
})
}
}

type config struct {
size int
defaultTTL time.Duration
Expand Down
Loading
Loading